yo.lua 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. local http = http
  2. local string = string
  3. local led = require('led')
  4. local assert = assert
  5. local type = type
  6. local debug_message = debug_message
  7. module(...)
  8. function yo(yo_user, api_token)
  9. assert(type(yo_user) == 'string', 'yo_user must be a string')
  10. assert(type(api_token) == 'string', 'api_token must be a string')
  11. assert(yo_user ~= '', 'yo_user must not be empty string')
  12. assert(api_token ~= '', 'api_token must not be empty string')
  13. local content_string = "api_token=" .. api_token .. "&username=" .. string.upper(yo_user) .. '\r\n\r\n'
  14. local content_length = string.len(content_string)
  15. local succeeded = false
  16. debug_message('yo.yo: sending Yo')
  17. http.post(
  18. 'http://api.justyo.co/yo/',
  19. 'Content-Type: application/x-www-form-urlencoded\r\n' ..
  20. 'Content-length: ' .. content_length .. '\r\n',
  21. content_string,
  22. function(status_code, response_data)
  23. debug_message('yo.yo: status code: ' .. status_code)
  24. debug_message('yo.yo: response data: ' .. (response_data or 'nil'))
  25. if status_code >= 200 and status_code < 300 then
  26. succeeded = true
  27. else
  28. succeeded = false
  29. end
  30. yo_sent(succeeded)
  31. end
  32. )
  33. end
  34. function yo_sent(succeeded)
  35. if succeeded then
  36. led.q_fade_out()
  37. else
  38. led.q_triple_blink()
  39. end
  40. end