1
0

user_location_agent.rb 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. require 'securerandom'
  2. module Agents
  3. class UserLocationAgent < Agent
  4. cannot_be_scheduled!
  5. gem_dependency_check { defined?(Haversine) }
  6. description do <<-MD
  7. The User Location Agent creates events based on WebHook POSTS that contain a `latitude` and `longitude`. You can use the [POSTLocation](https://github.com/cantino/post_location) or [PostGPS](https://github.com/chriseidhof/PostGPS) iOS app to post your location to `https://#{ENV['DOMAIN']}/users/#{user.id}/update_location/:secret` where `:secret` is specified in your options.
  8. #{'## Include `haversine` in your Gemfile to use this Agent!' if dependencies_missing?}
  9. If you want to only keep more precise locations, set `max_accuracy` to the upper bound, in meters. The default name for this field is `accuracy`, but you can change this by setting a value for `accuracy_field`.
  10. If you want to require a certain distance traveled, set `min_distance` to the minimum distance, in meters. Note that GPS readings and the measurement itself aren't exact, so don't rely on this for precision filtering.
  11. MD
  12. end
  13. event_description <<-MD
  14. Assuming you're using the iOS application, events look like this:
  15. {
  16. "latitude": "37.12345",
  17. "longitude": "-122.12345",
  18. "timestamp": "123456789.0",
  19. "altitude": "22.0",
  20. "horizontal_accuracy": "5.0",
  21. "vertical_accuracy": "3.0",
  22. "speed": "0.52595",
  23. "course": "72.0703",
  24. "device_token": "..."
  25. }
  26. MD
  27. def working?
  28. event_created_within?(2) && !recent_error_logs?
  29. end
  30. def default_options
  31. {
  32. 'secret' => SecureRandom.hex(7),
  33. 'max_accuracy' => '',
  34. 'min_distance' => '',
  35. }
  36. end
  37. def validate_options
  38. errors.add(:base, "secret is required and must be longer than 4 characters") unless options['secret'].present? && options['secret'].length > 4
  39. end
  40. def receive(incoming_events)
  41. incoming_events.each do |event|
  42. interpolate_with(event) do
  43. handle_payload event.payload
  44. end
  45. end
  46. end
  47. def receive_web_request(params, method, format)
  48. params = params.symbolize_keys
  49. if method != 'post'
  50. return ['Not Found', 404]
  51. end
  52. if interpolated['secret'] != params[:secret]
  53. return ['Not Authorized', 401]
  54. end
  55. handle_payload params.except(:secret)
  56. return ['ok', 200]
  57. end
  58. private
  59. def handle_payload(payload)
  60. location = Location.new(payload)
  61. accuracy_field = interpolated[:accuracy_field].presence || "accuracy"
  62. def accurate_enough?(payload, accuracy_field)
  63. !interpolated[:max_accuracy].present? || !payload[accuracy_field] || payload[accuracy_field].to_i < interpolated[:max_accuracy].to_i
  64. end
  65. def far_enough?(payload)
  66. if memory['last_location'].present?
  67. travel = Haversine.distance(memory['last_location']['latitude'].to_i, memory['last_location']['longitude'].to_i, payload['latitude'].to_i, payload['longitude'].to_i).to_meters
  68. !interpolated[:min_distance].present? || travel > interpolated[:min_distance].to_i
  69. else # for the first run, before "last_location" exists
  70. true
  71. end
  72. end
  73. if location.present? && accurate_enough?(payload, accuracy_field) && far_enough?(payload)
  74. if interpolated[:max_accuracy].present? && !payload[accuracy_field].present?
  75. log "Accuracy field missing; all locations will be kept"
  76. end
  77. create_event payload: payload, location: location
  78. memory["last_location"] = payload
  79. end
  80. end
  81. end
  82. end