Module: Mqttopia::Serializers::LiveLocation

Defined in:
lib/mqttopia/serializers/live_location.rb

Constant Summary collapse

KEYS =
i[tid batt loc].freeze

Class Method Summary collapse

Class Method Details

.current_timestampObject



38
39
40
# File 'lib/mqttopia/serializers/live_location.rb', line 38

def current_timestamp
  DateTime.now.strftime("%Q")
end

.serialize(body) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/mqttopia/serializers/live_location.rb', line 10

def serialize(body)
  return unless valid_live_location?(body[:live_location])

  {
    trip_id: body[:trip_id],
    live_location: serialize_live_location(
      body.dig(:live_location, :loc)&.merge!(batt: body.dig(:live_location, :batt))
    ),
    logged_at: body.dig(:live_location, :loc, :ts)&.to_s,
    received_at: current_timestamp
  }
end

.serialize_live_location(live_location) ⇒ Object



23
24
25
26
27
28
29
30
31
32
# File 'lib/mqttopia/serializers/live_location.rb', line 23

def serialize_live_location(live_location)
  {
    latitude: live_location[:lat],
    longitude: live_location[:lon],
    accuracy: live_location[:acc],
    altitude: live_location[:alt],
    speed: live_location[:spd],
    battery: live_location[:batt]
  }
end

.valid_live_location?(live_location) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/mqttopia/serializers/live_location.rb', line 34

def valid_live_location?(live_location)
  live_location.is_a?(Hash) && KEYS.all? { |key| live_location.key?(key) }
end