user_location_agent.rb 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. require 'securerandom'
  2. module Agents
  3. class UserLocationAgent < Agent
  4. cannot_receive_events!
  5. cannot_be_scheduled!
  6. description do
  7. <<-MD
  8. The UserLocationAgent creates events based on WebHook POSTS that contain a `latitude` and `longitude`. You can use the POSTLocation iOS app to post your location.
  9. Your POST path will be `https://#{ENV['DOMAIN']}/users/#{user.id}/update_location/:secret` where `:secret` is specified in your options.
  10. MD
  11. end
  12. event_description <<-MD
  13. Assuming you're using the iOS application, events look like this:
  14. {
  15. "latitude": "37.12345",
  16. "longitude": "-122.12345",
  17. "timestamp": "123456789.0",
  18. "altitude": "22.0",
  19. "horizontal_accuracy": "5.0",
  20. "vertical_accuracy": "3.0",
  21. "speed": "0.52595",
  22. "course": "72.0703",
  23. "device_token": "..."
  24. }
  25. MD
  26. def working?
  27. event_created_within?(2) && !recent_error_logs?
  28. end
  29. def default_options
  30. { 'secret' => SecureRandom.hex(7) }
  31. end
  32. def validate_options
  33. errors.add(:base, "secret is required and must be longer than 4 characters") unless options['secret'].present? && options['secret'].length > 4
  34. end
  35. end
  36. end