Class: SpreeCmCommissioner::Integrations::BookMeBusV1::ExternalClient::Client

Inherits:
Object
  • Object
show all
Includes:
AuditWrapper, Connection
Defined in:
app/services/spree_cm_commissioner/integrations/book_me_bus_v1/external_client/client.rb

Constant Summary collapse

BASE_PATH =
'/api/v2'.freeze
RATE_LIMIT =
100

Constants included from Connection

SpreeCmCommissioner::Integrations::BookMeBusV1::ExternalClient::Connection::OAUTH_TOKEN_PATH, SpreeCmCommissioner::Integrations::BookMeBusV1::ExternalClient::Connection::OPEN_TIMEOUT, SpreeCmCommissioner::Integrations::BookMeBusV1::ExternalClient::Connection::REQUEST_TIMEOUT

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from AuditWrapper

#with_audit

Methods included from Connection

#access_token, #connection, #delete, #get, #patch, #post, #put

Constructor Details

#initialize(integration:) ⇒ Client

Returns a new instance of Client.



11
12
13
# File 'app/services/spree_cm_commissioner/integrations/book_me_bus_v1/external_client/client.rb', line 11

def initialize(integration:)
  @integration = integration
end

Instance Attribute Details

#integrationObject (readonly)

Returns the value of attribute integration.



9
10
11
# File 'app/services/spree_cm_commissioner/integrations/book_me_bus_v1/external_client/client.rb', line 9

def integration
  @integration
end

Instance Method Details

#confirm_wallet_payment!(payment_id:, internal_order:) ⇒ SpreeCmCommissioner::Integrations::BookMeBusV1::Resources::WalletPayment

Confirm a wallet payment (deduct from user’s BookMeBus wallet)

Parameters:

  • payment_id (String)

    The wallet payment ID

Returns:



173
174
175
176
177
178
179
180
181
182
183
184
# File 'app/services/spree_cm_commissioner/integrations/book_me_bus_v1/external_client/client.rb', line 173

def confirm_wallet_payment!(payment_id:, internal_order:)
  endpoint = "#{BASE_PATH}/bmb_wallets?payment_id=#{payment_id}"

  with_audit(
    event_base: 'bookmebus.wallet_payments.post',
    request: { method: 'POST', endpoint: endpoint, body: {} },
    auditable: internal_order
  ) do
    response = post(endpoint, {})
    handle_response(response, SpreeCmCommissioner::Integrations::BookMeBusV1::Resources::WalletPayment)
  end
end

#create_reservation!(params) ⇒ SpreeCmCommissioner::Integrations::BookMeBusV1::Resources::ReservationCart

Create a reservation cart in BookMeBus

Parameters:

  • params (Hash)

    Reservation parameters

Options Hash (params):

  • :trip_id (Integer)

    The BookMeBus trip ID

  • :from_stop_id (Integer)

    The boarding stop ID

  • :to_stop_id (Integer)

    The drop-off stop ID

  • :from_stop_time_id (Integer)

    The boarding stop time ID

  • :to_stop_time_id (Integer)

    The drop-off stop time ID

  • :departure_date (String)

    The travel date (YYYY-MM-DD format)

  • :reserved_seats (Hash)

    Reserved seats hash keyed by stop_id with seat detail objects

  • :contact_name (String)

    Passenger contact name

  • :contact_email (String)

    Passenger contact email

  • :contact_phone (String)

    Passenger contact phone

  • :internal_order (Object)

    Internal order for audit

Returns:



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'app/services/spree_cm_commissioner/integrations/book_me_bus_v1/external_client/client.rb', line 119

def create_reservation!(params)
  number_of_seats = params[:reserved_seats].values.flatten.count

  body = {
    reservations: [
      {
        trip_id: params[:trip_id],
        from_stop_id: params[:from_stop_id],
        to_stop_id: params[:to_stop_id],
        from_stop_time_id: params[:from_stop_time_id],
        to_stop_time_id: params[:to_stop_time_id],
        departure_date: params[:departure_date],
        number_of_seats: number_of_seats,
        reserved_seats: params[:reserved_seats],
        contact_name: params[:contact_name],
        contact_email: params[:contact_email],
        contact_phone: params[:contact_phone],
        is_request_pickup: false
      }.compact
    ]
  }

  endpoint = "#{BASE_PATH}/trip_reservations"

  with_audit(
    event_base: 'bookmebus.reservations.post',
    request: { method: 'POST', endpoint: endpoint, body: body },
    auditable: params[:internal_order]
  ) do
    response = post(endpoint, body)
    handle_response(response, SpreeCmCommissioner::Integrations::BookMeBusV1::Resources::ReservationCart)
  end
end

#create_wallet_payment!(cart_id:, internal_order:) ⇒ SpreeCmCommissioner::Integrations::BookMeBusV1::Resources::WalletPayment

Wallet Payment Operations Create a wallet payment for a reservation cart

Parameters:

  • cart_id (String)

    The reservation cart ID

Returns:



157
158
159
160
161
162
163
164
165
166
167
168
# File 'app/services/spree_cm_commissioner/integrations/book_me_bus_v1/external_client/client.rb', line 157

def create_wallet_payment!(cart_id:, internal_order:)
  endpoint = "#{BASE_PATH}/wallet_payments?reservation_cart_id=#{cart_id}"

  with_audit(
    event_base: 'bookmebus.wallet_payments.post',
    request: { method: 'POST', endpoint: endpoint, body: {} },
    auditable: internal_order
  ) do
    response = post(endpoint, {})
    handle_response(response, SpreeCmCommissioner::Integrations::BookMeBusV1::Resources::WalletPayment)
  end
end

#get_location!(id:) ⇒ SpreeCmCommissioner::Integrations::BookMeBusV1::Resources::Location

Get a specific location by ID

Parameters:

  • id (String)

    The location ID

Returns:



35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/services/spree_cm_commissioner/integrations/book_me_bus_v1/external_client/client.rb', line 35

def get_location!(id:)
  endpoint = "#{BASE_PATH}/locations/#{id}"

  with_audit(
    event_base: 'bookmebus.locations.get',
    request: { method: 'GET', endpoint: endpoint, body: {} },
    auditable: integration
  ) do
    response = get(endpoint)
    handle_response(response, SpreeCmCommissioner::Integrations::BookMeBusV1::Resources::Location)
  end
end

#get_locations!Array<SpreeCmCommissioner::Integrations::BookMeBusV1::Resources::Location>

Get all available locations

Returns:



19
20
21
22
23
24
25
26
27
28
29
30
# File 'app/services/spree_cm_commissioner/integrations/book_me_bus_v1/external_client/client.rb', line 19

def get_locations! # rubocop:disable Naming/AccessorMethodName
  endpoint = "#{BASE_PATH}/locations"

  with_audit(
    event_base: 'bookmebus.locations.get_all',
    request: { method: 'GET', endpoint: endpoint, body: {} },
    auditable: integration
  ) do
    response = get(endpoint)
    handle_response(response, SpreeCmCommissioner::Integrations::BookMeBusV1::Resources::Location, collection: true)
  end
end

#get_seat_layout!(trip_id:, from_stop_id:, to_stop_id:, on_date:, show_all_stops: false) ⇒ SpreeCmCommissioner::Integrations::BookMeBusV1::Resources::SeatLayout

Get seat layout for a specific trip segment

Parameters:

  • trip_id (String)

    The trip ID

  • from_stop_id (String)

    The departure stop ID

  • to_stop_id (String)

    The arrival stop ID

  • on_date (String)

    The travel date (YYYY-MM-DD format)

  • show_all_stops (Boolean) (defaults to: false)

    Whether to show all stops (default: false)

Returns:



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'app/services/spree_cm_commissioner/integrations/book_me_bus_v1/external_client/client.rb', line 82

def get_seat_layout!(trip_id:, from_stop_id:, to_stop_id:, on_date:, show_all_stops: false)
  params = {
    trip_id: trip_id,
    from_stop_id: from_stop_id,
    to_stop_id: to_stop_id,
    on_date: on_date,
    show_all_stops: show_all_stops
  }
  endpoint = "#{BASE_PATH}/seat_layouts"

  with_audit(
    event_base: 'bookmebus.seat_layout.get',
    request: { method: 'GET', endpoint: endpoint, params: params },
    auditable: integration
  ) do
    response = get(endpoint, params)

    handle_response(response, SpreeCmCommissioner::Integrations::BookMeBusV1::Resources::SeatLayout)
  end
end

#inventory_item_status!(stop_id:, trip_id:, label:, layer:, departure_date:) ⇒ SpreeCmCommissioner::Integrations::BookMeBusV1::Resources::InventoryItemStatus

Get passenger seat info for a specific seat

Parameters:

  • stop_id (String)

    The stop ID

  • trip_id (String)

    The trip ID

  • seat (String)

    The seat number/label

  • departure_date (String)

    The departure date (YYYY-MM-DD format)

Returns:



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'app/services/spree_cm_commissioner/integrations/book_me_bus_v1/external_client/client.rb', line 192

def inventory_item_status!(stop_id:, trip_id:, label:, layer:, departure_date:)
  params = {
    stop_id: stop_id,
    trip_id: trip_id,
    seat: {
      label: label,
      layer: layer
    },
    departure_date: departure_date
  }
  endpoint = "#{BASE_PATH}/passenger_seat_infos"

  with_audit(
    event_base: 'bookmebus.passenger_seat_infos.get',
    request: { method: 'GET', endpoint: endpoint, params: params },
    auditable: integration
  ) do
    response = get(endpoint, params)
    handle_response(response, SpreeCmCommissioner::Integrations::BookMeBusV1::Resources::InventoryItemStatus)
  end
end

#search_trips!(origin_id:, destination_id:, on_date:) ⇒ Array<SpreeCmCommissioner::Integrations::BookMeBusV1::Resources::Trip>

Search available trips for a given route and date

Parameters:

  • origin_id (String)

    The origin location ID

  • destination_id (String)

    The destination location ID

  • on_date (String)

    The travel date (YYYY-MM-DD format)

Returns:



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'app/services/spree_cm_commissioner/integrations/book_me_bus_v1/external_client/client.rb', line 55

def search_trips!(origin_id:, destination_id:, on_date:)
  params = {
    origin_id: origin_id,
    destination_id: destination_id,
    on_date: on_date
  }
  endpoint = "#{BASE_PATH}/trip_availabilities"

  with_audit(
    event_base: 'bookmebus.trips.get',
    request: { method: 'GET', endpoint: endpoint, params: params },
    auditable: integration
  ) do
    response = get(endpoint, params)
    handle_response(response, SpreeCmCommissioner::Integrations::BookMeBusV1::Resources::Trip, collection: true)
  end
end