Module: Mqttopia::Serializers::TripPoint

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

Constant Summary collapse

KEYS =
i[lat lon ts ctp_id trip_id].freeze

Class Method Summary collapse

Class Method Details

.action_type_mapper(action_type) ⇒ Object



41
42
43
44
45
46
# File 'lib/mqttopia/serializers/trip_point.rb', line 41

def action_type_mapper(action_type)
  {
    "0" => "arrival",
    "1" => "departure"
  }.fetch(action_type&.to_s) { nil }
end

.current_timestampObject



37
38
39
# File 'lib/mqttopia/serializers/trip_point.rb', line 37

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

.serialize(body) ⇒ Object



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

def serialize(body)
  return unless valid_point?(body[:point])

  {
    trip_id: body[:trip_id],
    point: serialize_point(body[:point]),
    logged_at: body.dig(:point, :ts)&.to_s,
    received_at: current_timestamp
  }
end

.serialize_point(point) ⇒ Object



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

def serialize_point(point)
  {
    latitude: point[:lat],
    longitude: point[:lon],
    accuracy: point[:acc],
    altitude: point[:alt],
    action_type: action_type_mapper(point[:act]),
    current_trip_point_id: point[:ctp_id],
    logged_at: point[:ts]
  }
end

.valid_point?(point) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/mqttopia/serializers/trip_point.rb', line 33

def valid_point?(point)
  point.is_a?(Hash) && KEYS.all? { |key| point.key?(key) }
end