user_location_agent.rb 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. require 'securerandom'
  2. module Agents
  3. class UserLocationAgent < Agent
  4. cannot_be_scheduled!
  5. gem_dependency_check { defined?(Haversine) }
  6. description do
  7. <<-MD
  8. #{'## Include `haversine` in your Gemfile to use this Agent!' if dependencies_missing?}
  9. The UserLocationAgent 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.
  10. Your POST path will be `https://#{ENV['DOMAIN']}/users/#{user.id}/update_location/:secret` where `:secret` is specified in your options.
  11. 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`.
  12. 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.
  13. MD
  14. end
  15. event_description <<-MD
  16. Assuming you're using the iOS application, events look like this:
  17. {
  18. "latitude": "37.12345",
  19. "longitude": "-122.12345",
  20. "timestamp": "123456789.0",
  21. "altitude": "22.0",
  22. "horizontal_accuracy": "5.0",
  23. "vertical_accuracy": "3.0",
  24. "speed": "0.52595",
  25. "course": "72.0703",
  26. "device_token": "..."
  27. }
  28. MD
  29. def working?
  30. event_created_within?(2) && !recent_error_logs?
  31. end
  32. def default_options
  33. {
  34. 'secret' => SecureRandom.hex(7),
  35. 'max_accuracy' => '',
  36. 'min_distance' => '',
  37. }
  38. end
  39. def validate_options
  40. errors.add(:base, "secret is required and must be longer than 4 characters") unless options['secret'].present? && options['secret'].length > 4
  41. end
  42. def receive(incoming_events)
  43. incoming_events.each do |event|
  44. interpolate_with(event) do
  45. handle_payload event.payload
  46. end
  47. end
  48. end
  49. def receive_web_request(params, method, format)
  50. params = params.symbolize_keys
  51. if method != 'post'
  52. return ['Not Found', 404]
  53. end
  54. if interpolated['secret'] != params[:secret]
  55. return ['Not Authorized', 401]
  56. end
  57. handle_payload params.except(:secret)
  58. return ['ok', 200]
  59. end
  60. private
  61. def handle_payload(payload)
  62. location = Location.new(payload)
  63. accuracy_field = interpolated[:accuracy_field].presence || "accuracy"
  64. def accurate_enough?(payload, accuracy_field)
  65. !interpolated[:max_accuracy].present? || !payload[accuracy_field] || payload[accuracy_field].to_i < interpolated[:max_accuracy].to_i
  66. end
  67. def far_enough?(payload)
  68. if memory['last_location'].present?
  69. 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
  70. !interpolated[:min_distance].present? || travel > interpolated[:min_distance].to_i
  71. else # for the first run, before "last_location" exists
  72. true
  73. end
  74. end
  75. if location.present? && accurate_enough?(payload, accuracy_field) && far_enough?(payload)
  76. if interpolated[:max_accuracy].present? && !payload[accuracy_field].present?
  77. log "Accuracy field missing; all locations will be kept"
  78. end
  79. create_event payload: payload, location: location
  80. memory["last_location"] = payload
  81. end
  82. end
  83. end
  84. end