Class: SpreeCmCommissioner::Integrations::BookMeBusV1::Resources::Trip

Inherits:
Base
  • Object
show all
Defined in:
app/services/spree_cm_commissioner/integrations/book_me_bus_v1/resources/trip.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

from_json_api, from_json_api_item, from_json_api_single

Constructor Details

#initialize(attributes = {}) ⇒ Trip

Returns a new instance of Trip.



31
32
33
34
35
36
37
38
39
# File 'app/services/spree_cm_commissioner/integrations/book_me_bus_v1/resources/trip.rb', line 31

def initialize(attributes = {})
  super(attributes)

  @operator = attributes['operator'] if attributes['operator'].is_a?(Hash)
  @vehicle_type = attributes['vehicle_type'] if attributes['vehicle_type'].is_a?(Hash)
  @stop_times = attributes['stop_times'] if attributes['stop_times'].is_a?(Array)
  @price = attributes['price'] if attributes['price'].is_a?(Hash)
  @service_calendar = attributes['service_calendar'] if attributes['service_calendar'].is_a?(Hash)
end

Instance Attribute Details

#arrival_timeObject

Returns the value of attribute arrival_time.



3
4
5
# File 'app/services/spree_cm_commissioner/integrations/book_me_bus_v1/resources/trip.rb', line 3

def arrival_time
  @arrival_time
end

#currency_codeObject

Returns the value of attribute currency_code.



3
4
5
# File 'app/services/spree_cm_commissioner/integrations/book_me_bus_v1/resources/trip.rb', line 3

def currency_code
  @currency_code
end

#departure_timeObject

Returns the value of attribute departure_time.



3
4
5
# File 'app/services/spree_cm_commissioner/integrations/book_me_bus_v1/resources/trip.rb', line 3

def departure_time
  @departure_time
end

#destination_idObject

Returns the value of attribute destination_id.



3
4
5
# File 'app/services/spree_cm_commissioner/integrations/book_me_bus_v1/resources/trip.rb', line 3

def destination_id
  @destination_id
end

#destination_nameObject

Returns the value of attribute destination_name.



3
4
5
# File 'app/services/spree_cm_commissioner/integrations/book_me_bus_v1/resources/trip.rb', line 3

def destination_name
  @destination_name
end

#from_stop_idObject

Returns the value of attribute from_stop_id.



3
4
5
# File 'app/services/spree_cm_commissioner/integrations/book_me_bus_v1/resources/trip.rb', line 3

def from_stop_id
  @from_stop_id
end

#idObject

Returns the value of attribute id.



3
4
5
# File 'app/services/spree_cm_commissioner/integrations/book_me_bus_v1/resources/trip.rb', line 3

def id
  @id
end

#instant_bookObject

Returns the value of attribute instant_book.



3
4
5
# File 'app/services/spree_cm_commissioner/integrations/book_me_bus_v1/resources/trip.rb', line 3

def instant_book
  @instant_book
end

#local_priceObject

Returns the value of attribute local_price.



3
4
5
# File 'app/services/spree_cm_commissioner/integrations/book_me_bus_v1/resources/trip.rb', line 3

def local_price
  @local_price
end

#non_local_priceObject

Returns the value of attribute non_local_price.



3
4
5
# File 'app/services/spree_cm_commissioner/integrations/book_me_bus_v1/resources/trip.rb', line 3

def non_local_price
  @non_local_price
end

#number_of_seatsObject

Returns the value of attribute number_of_seats.



3
4
5
# File 'app/services/spree_cm_commissioner/integrations/book_me_bus_v1/resources/trip.rb', line 3

def number_of_seats
  @number_of_seats
end

#on_dateObject

Returns the value of attribute on_date.



3
4
5
# File 'app/services/spree_cm_commissioner/integrations/book_me_bus_v1/resources/trip.rb', line 3

def on_date
  @on_date
end

#operatorObject

Returns the value of attribute operator.



3
4
5
# File 'app/services/spree_cm_commissioner/integrations/book_me_bus_v1/resources/trip.rb', line 3

def operator
  @operator
end

#origin_idObject

Returns the value of attribute origin_id.



3
4
5
# File 'app/services/spree_cm_commissioner/integrations/book_me_bus_v1/resources/trip.rb', line 3

def origin_id
  @origin_id
end

#origin_nameObject

Returns the value of attribute origin_name.



3
4
5
# File 'app/services/spree_cm_commissioner/integrations/book_me_bus_v1/resources/trip.rb', line 3

def origin_name
  @origin_name
end

#priceObject

Returns the value of attribute price.



3
4
5
# File 'app/services/spree_cm_commissioner/integrations/book_me_bus_v1/resources/trip.rb', line 3

def price
  @price
end

#route_idObject

Returns the value of attribute route_id.



3
4
5
# File 'app/services/spree_cm_commissioner/integrations/book_me_bus_v1/resources/trip.rb', line 3

def route_id
  @route_id
end

#route_typeObject

Returns the value of attribute route_type.



3
4
5
# File 'app/services/spree_cm_commissioner/integrations/book_me_bus_v1/resources/trip.rb', line 3

def route_type
  @route_type
end

#seats_remainingObject

Returns the value of attribute seats_remaining.



3
4
5
# File 'app/services/spree_cm_commissioner/integrations/book_me_bus_v1/resources/trip.rb', line 3

def seats_remaining
  @seats_remaining
end

#service_availableObject

Returns the value of attribute service_available.



3
4
5
# File 'app/services/spree_cm_commissioner/integrations/book_me_bus_v1/resources/trip.rb', line 3

def service_available
  @service_available
end

#service_calendarObject

Returns the value of attribute service_calendar.



3
4
5
# File 'app/services/spree_cm_commissioner/integrations/book_me_bus_v1/resources/trip.rb', line 3

def service_calendar
  @service_calendar
end

#stop_timesObject

Returns the value of attribute stop_times.



3
4
5
# File 'app/services/spree_cm_commissioner/integrations/book_me_bus_v1/resources/trip.rb', line 3

def stop_times
  @stop_times
end

#to_stop_idObject

Returns the value of attribute to_stop_id.



3
4
5
# File 'app/services/spree_cm_commissioner/integrations/book_me_bus_v1/resources/trip.rb', line 3

def to_stop_id
  @to_stop_id
end

#travel_durationObject

Returns the value of attribute travel_duration.



3
4
5
# File 'app/services/spree_cm_commissioner/integrations/book_me_bus_v1/resources/trip.rb', line 3

def travel_duration
  @travel_duration
end

#trip_idObject

Returns the value of attribute trip_id.



3
4
5
# File 'app/services/spree_cm_commissioner/integrations/book_me_bus_v1/resources/trip.rb', line 3

def trip_id
  @trip_id
end

#typeObject

Returns the value of attribute type.



3
4
5
# File 'app/services/spree_cm_commissioner/integrations/book_me_bus_v1/resources/trip.rb', line 3

def type
  @type
end

#vehicle_typeObject

Returns the value of attribute vehicle_type.



3
4
5
# File 'app/services/spree_cm_commissioner/integrations/book_me_bus_v1/resources/trip.rb', line 3

def vehicle_type
  @vehicle_type
end

Instance Method Details

#amenitiesObject



67
68
69
# File 'app/services/spree_cm_commissioner/integrations/book_me_bus_v1/resources/trip.rb', line 67

def amenities
  @vehicle_type&.dig('amenities') || []
end

#arrival_time_formattedObject



154
155
156
157
158
159
160
161
# File 'app/services/spree_cm_commissioner/integrations/book_me_bus_v1/resources/trip.rb', line 154

def arrival_time_formatted
  return nil unless @arrival_time

  seconds = @arrival_time.to_i
  hours = seconds / 3600
  minutes = (seconds % 3600) / 60
  format('%<hours>02d:%<minutes>02d', hours: hours, minutes: minutes)
end

#available_seatsObject



79
80
81
# File 'app/services/spree_cm_commissioner/integrations/book_me_bus_v1/resources/trip.rb', line 79

def available_seats
  seats_remaining
end

#departure_time_formattedObject

Time helpers - departure_time is in seconds from midnight



145
146
147
148
149
150
151
152
# File 'app/services/spree_cm_commissioner/integrations/book_me_bus_v1/resources/trip.rb', line 145

def departure_time_formatted
  return nil unless @departure_time

  seconds = @departure_time.to_i
  hours = seconds / 3600
  minutes = (seconds % 3600) / 60
  format('%<hours>02d:%<minutes>02d', hours: hours, minutes: minutes)
end

#destination_location_idObject



104
105
106
# File 'app/services/spree_cm_commissioner/integrations/book_me_bus_v1/resources/trip.rb', line 104

def destination_location_id
  destination_stop&.dig('location', 'id')
end

#destination_location_nameObject



108
109
110
# File 'app/services/spree_cm_commissioner/integrations/book_me_bus_v1/resources/trip.rb', line 108

def destination_location_name
  destination_stop&.dig('location', 'name')
end

#destination_stopObject



92
93
94
# File 'app/services/spree_cm_commissioner/integrations/book_me_bus_v1/resources/trip.rb', line 92

def destination_stop
  @stop_times&.last
end

#destination_stop_idObject



128
129
130
# File 'app/services/spree_cm_commissioner/integrations/book_me_bus_v1/resources/trip.rb', line 128

def destination_stop_id
  destination_stop&.dig('stop', 'id')
end

#destination_stop_latObject



136
137
138
# File 'app/services/spree_cm_commissioner/integrations/book_me_bus_v1/resources/trip.rb', line 136

def destination_stop_lat
  destination_stop&.dig('stop', 'lat')
end

#destination_stop_lonObject



140
141
142
# File 'app/services/spree_cm_commissioner/integrations/book_me_bus_v1/resources/trip.rb', line 140

def destination_stop_lon
  destination_stop&.dig('stop', 'lon')
end

#destination_stop_nameObject



132
133
134
# File 'app/services/spree_cm_commissioner/integrations/book_me_bus_v1/resources/trip.rb', line 132

def destination_stop_name
  destination_stop&.dig('stop', 'name')
end

#display_routeObject



163
164
165
# File 'app/services/spree_cm_commissioner/integrations/book_me_bus_v1/resources/trip.rb', line 163

def display_route
  "#{origin_location_name}#{destination_location_name}"
end

#display_scheduleObject



167
168
169
# File 'app/services/spree_cm_commissioner/integrations/book_me_bus_v1/resources/trip.rb', line 167

def display_schedule
  "#{departure_time_formatted} - #{arrival_time_formatted}"
end

#operator_idObject

Operator accessors



42
43
44
# File 'app/services/spree_cm_commissioner/integrations/book_me_bus_v1/resources/trip.rb', line 42

def operator_id
  @operator&.dig('id')
end

#operator_logoObject



50
51
52
# File 'app/services/spree_cm_commissioner/integrations/book_me_bus_v1/resources/trip.rb', line 50

def 
  @operator&.dig('logo', 'thumb')
end

#operator_nameObject



46
47
48
# File 'app/services/spree_cm_commissioner/integrations/book_me_bus_v1/resources/trip.rb', line 46

def operator_name
  @operator&.dig('name')
end

#origin_location_idObject



96
97
98
# File 'app/services/spree_cm_commissioner/integrations/book_me_bus_v1/resources/trip.rb', line 96

def origin_location_id
  origin_stop&.dig('location', 'id')
end

#origin_location_nameObject



100
101
102
# File 'app/services/spree_cm_commissioner/integrations/book_me_bus_v1/resources/trip.rb', line 100

def origin_location_name
  origin_stop&.dig('location', 'name')
end

#origin_stopObject

Stop times accessors



88
89
90
# File 'app/services/spree_cm_commissioner/integrations/book_me_bus_v1/resources/trip.rb', line 88

def origin_stop
  @stop_times&.first
end

#origin_stop_idObject



112
113
114
# File 'app/services/spree_cm_commissioner/integrations/book_me_bus_v1/resources/trip.rb', line 112

def origin_stop_id
  origin_stop&.dig('stop', 'id')
end

#origin_stop_latObject



120
121
122
# File 'app/services/spree_cm_commissioner/integrations/book_me_bus_v1/resources/trip.rb', line 120

def origin_stop_lat
  origin_stop&.dig('stop', 'lat')
end

#origin_stop_lonObject



124
125
126
# File 'app/services/spree_cm_commissioner/integrations/book_me_bus_v1/resources/trip.rb', line 124

def origin_stop_lon
  origin_stop&.dig('stop', 'lon')
end

#origin_stop_nameObject



116
117
118
# File 'app/services/spree_cm_commissioner/integrations/book_me_bus_v1/resources/trip.rb', line 116

def origin_stop_name
  origin_stop&.dig('stop', 'name')
end

#sync_identifierObject



83
84
85
# File 'app/services/spree_cm_commissioner/integrations/book_me_bus_v1/resources/trip.rb', line 83

def sync_identifier
  trip_id || id
end

#to_hObject



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'app/services/spree_cm_commissioner/integrations/book_me_bus_v1/resources/trip.rb', line 171

def to_h
  {
    id: id,
    type: type,
    trip_id: trip_id,
    on_date: on_date,
    departure_time: departure_time,
    arrival_time: arrival_time,
    travel_duration: travel_duration,
    local_price: local_price,
    non_local_price: non_local_price,
    currency_code: currency_code,
    seats_remaining: seats_remaining,
    number_of_seats: number_of_seats,
    route_type: route_type,
    route_id: route_id,
    from_stop_id: from_stop_id,
    to_stop_id: to_stop_id,
    instant_book: instant_book,
    service_available: service_available,
    operator: operator,
    vehicle_type: vehicle_type,
    stop_times: stop_times,
    price: price,
    service_calendar: service_calendar
  }.compact
end

#vehicle_type_codeObject



63
64
65
# File 'app/services/spree_cm_commissioner/integrations/book_me_bus_v1/resources/trip.rb', line 63

def vehicle_type_code
  @vehicle_type&.dig('code')
end

#vehicle_type_idObject

Vehicle accessors



55
56
57
# File 'app/services/spree_cm_commissioner/integrations/book_me_bus_v1/resources/trip.rb', line 55

def vehicle_type_id
  @vehicle_type&.dig('id')
end

#vehicle_type_nameObject



59
60
61
# File 'app/services/spree_cm_commissioner/integrations/book_me_bus_v1/resources/trip.rb', line 59

def vehicle_type_name
  @vehicle_type&.dig('name')
end

#vehicle_type_route_typeObject



71
72
73
# File 'app/services/spree_cm_commissioner/integrations/book_me_bus_v1/resources/trip.rb', line 71

def vehicle_type_route_type
  @vehicle_type&.dig('route_type')
end

#vehicle_type_type_nameObject



75
76
77
# File 'app/services/spree_cm_commissioner/integrations/book_me_bus_v1/resources/trip.rb', line 75

def vehicle_type_type_name
  @vehicle_type&.dig('type_name')
end