Class: Mqttopia::Subscriptions::Services::TripPoints

Inherits:
Base
  • Object
show all
Defined in:
lib/mqttopia/subscriptions/services/trip_points.rb

Defined Under Namespace

Classes: TripPointsInvalidError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helpers::DataExtractor

#extract_trip_id, #extract_user_id

Constructor Details

#initialize(trip_id:, point:, options: {}) ⇒ TripPoints

validate :point_json_format



19
20
21
22
23
24
# File 'lib/mqttopia/subscriptions/services/trip_points.rb', line 19

def initialize(trip_id:, point:, options: {})
  @trip_id = trip_id
  @point = point
  @options = options
  @user_id = options[:user_id]
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



13
14
15
# File 'lib/mqttopia/subscriptions/services/trip_points.rb', line 13

def options
  @options
end

#pointObject (readonly)

Returns the value of attribute point.



13
14
15
# File 'lib/mqttopia/subscriptions/services/trip_points.rb', line 13

def point
  @point
end

#trip_idObject (readonly)

Returns the value of attribute trip_id.



13
14
15
# File 'lib/mqttopia/subscriptions/services/trip_points.rb', line 13

def trip_id
  @trip_id
end

#user_idObject (readonly)

Returns the value of attribute user_id.



13
14
15
# File 'lib/mqttopia/subscriptions/services/trip_points.rb', line 13

def user_id
  @user_id
end

Class Method Details

.call(topic, payload, options = {}) ⇒ Object



26
27
28
29
30
31
# File 'lib/mqttopia/subscriptions/services/trip_points.rb', line 26

def self.call(topic, payload, options = {})
  trip_id = topic.include?("trips/") && topic.match(%r{trips/[^/]+}) && extract_trip_id(topic)
  options[:user_id] = topic.include?("user/") && topic.match(%r{user/[^/]+}) && extract_user_id(topic)

  new(trip_id: trip_id, point: payload, options: options).process
end

Instance Method Details

#processObject



33
34
35
36
37
38
39
40
# File 'lib/mqttopia/subscriptions/services/trip_points.rb', line 33

def process
  raise TripPointsInvalidError, errors.full_messages.to_sentence unless valid?

  Mqttopia::Serializers::TripPoint.serialize({
                                               trip_id: trip_id,
                                               point: point
                                             })
end