Class: SpreeCmCommissioner::Integrations::VireakBuntham::ExternalClient::Client

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

Overview

HTTP client for the Vireak Buntham (VET) API.

All endpoints that operate against a vehicle category accept a ‘type` param (1 = bus, 2 = ferry, 3 = air bus). The value is taken from `integration.vet_type_param` so each integration instance is scoped to one category.

Constant Summary

Constants included from Connection

SpreeCmCommissioner::Integrations::VireakBuntham::ExternalClient::Connection::OPEN_TIMEOUT, SpreeCmCommissioner::Integrations::VireakBuntham::ExternalClient::Connection::REQUEST_TIMEOUT

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from AuditWrapper

#with_audit

Constructor Details

#initialize(integration:) ⇒ Client

Returns a new instance of Client.



14
15
16
# File 'app/services/spree_cm_commissioner/integrations/vireak_buntham/external_client/client.rb', line 14

def initialize(integration:)
  @integration = integration
end

Instance Attribute Details

#integrationObject (readonly)

Returns the value of attribute integration.



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

def integration
  @integration
end

Instance Method Details

#check_transaction!(transaction_id:, internal_order: nil) ⇒ Object

POST /booking/checkTransaction — verify a transaction by id.



153
154
155
156
157
158
159
160
161
162
163
164
# File 'app/services/spree_cm_commissioner/integrations/vireak_buntham/external_client/client.rb', line 153

def check_transaction!(transaction_id:, internal_order: nil)
  endpoint = 'booking/checkTransaction'

  with_audit(
    event_base: 'vireakbuntham.booking.check_transaction',
    request: { method: 'POST', endpoint: endpoint, body: { transactionId: transaction_id } },
    auditable: internal_order || integration
  ) do
    response = post(endpoint, { transactionId: transaction_id })
    handle_response(response, SpreeCmCommissioner::Integrations::VireakBuntham::Resources::BookingTransaction)
  end
end

#complete_booking!(reference:, transaction_id:, bank_reference: '', internal_order: nil) ⇒ Object

POST /booking/complete — mark a booking as paid.



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'app/services/spree_cm_commissioner/integrations/vireak_buntham/external_client/client.rb', line 134

def complete_booking!(reference:, transaction_id:, bank_reference: '', internal_order: nil)
  endpoint = 'booking/complete'
  body = {
    reference: reference,
    transactionId: transaction_id,
    bankReference: bank_reference
  }

  with_audit(
    event_base: 'vireakbuntham.booking.complete',
    request: { method: 'POST', endpoint: endpoint, body: body },
    auditable: internal_order || integration
  ) do
    response = post(endpoint, body)
    handle_response(response)
  end
end

#confirm_booking!(request:, internal_order: nil) ⇒ Object

POST /booking/confirm — create a booking. Caller passes a ConfirmBookingRequest; the resource owns VET’s indexed form encoding (seatName=…, seatName=…).



119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'app/services/spree_cm_commissioner/integrations/vireak_buntham/external_client/client.rb', line 119

def confirm_booking!(request:, internal_order: nil)
  endpoint = 'booking/confirm'
  body     = request.to_form

  with_audit(
    event_base: 'vireakbuntham.booking.confirm',
    request: { method: 'POST', endpoint: endpoint, body: body },
    auditable: internal_order || integration
  ) do
    response = post(endpoint, body)
    handle_response(response, SpreeCmCommissioner::Integrations::VireakBuntham::Resources::Booking)
  end
end

#get_available_seats!(journey:, date:, internal_order: nil) ⇒ Object

POST /seat/list — available seats for a ferry journey on a date.



88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'app/services/spree_cm_commissioner/integrations/vireak_buntham/external_client/client.rb', line 88

def get_available_seats!(journey:, date:, internal_order: nil)
  endpoint = 'seat/list'
  body = { journey: journey, date: date }

  with_audit(
    event_base: 'vireakbuntham.seats.list',
    request: { method: 'POST', endpoint: endpoint, body: body },
    auditable: internal_order || integration
  ) do
    response = post(endpoint, body)
    handle_response(response, SpreeCmCommissioner::Integrations::VireakBuntham::Resources::Seat, collection: true)
  end
end

#get_locations!Object

POST /destination/from — list VET locations for the integration’s vehicle category.



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

def get_locations! # rubocop:disable Naming/AccessorMethodName
  endpoint = 'destination/from'
  body = { type: integration.vet_type_param }

  with_audit(
    event_base: 'vireakbuntham.locations.get',
    request: { method: 'POST', endpoint: endpoint, body: body },
    auditable: integration
  ) do
    response = post(endpoint, body)
    handle_response(response, SpreeCmCommissioner::Integrations::VireakBuntham::Resources::Location)
  end
end

#get_schedules!(destination_from:, destination_to:, internal_order: nil) ⇒ Object

POST /schedule/list — all recurring schedules for a route. No date param.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'app/services/spree_cm_commissioner/integrations/vireak_buntham/external_client/client.rb', line 34

def get_schedules!(destination_from:, destination_to:, internal_order: nil)
  endpoint = 'schedule/list'
  body = {
    destinationFrom: destination_from,
    destinationTo: destination_to,
    type: integration.vet_type_param
  }

  with_audit(
    event_base: 'vireakbuntham.schedules.list',
    request: { method: 'POST', endpoint: endpoint, body: body },
    auditable: internal_order || integration
  ) do
    response = post(endpoint, body)
    handle_response(response, SpreeCmCommissioner::Integrations::VireakBuntham::Resources::Schedule)
  end
end

#get_schedules_by_date!(destination_from:, destination_to:, date:, internal_order: nil) ⇒ Object

POST /schedule/listByDate — schedules for a route on a specific date.



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

def get_schedules_by_date!(destination_from:, destination_to:, date:, internal_order: nil)
  endpoint = 'schedule/listByDate'
  body = {
    destinationFrom: destination_from,
    destinationTo: destination_to,
    type: integration.vet_type_param,
    date: date
  }

  with_audit(
    event_base: 'vireakbuntham.schedules.list_by_date',
    request: { method: 'POST', endpoint: endpoint, body: body },
    auditable: internal_order || integration
  ) do
    response = post(endpoint, body)
    handle_response(response, SpreeCmCommissioner::Integrations::VireakBuntham::Resources::Schedule)
  end
end

#get_seat_layout!(journey:, date:, internal_order: nil) ⇒ Object

POST /seat/layout — full seat layout for a journey on a date.



103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'app/services/spree_cm_commissioner/integrations/vireak_buntham/external_client/client.rb', line 103

def get_seat_layout!(journey:, date:, internal_order: nil)
  endpoint = 'seat/layout'
  body = { journey: journey, date: date }

  with_audit(
    event_base: 'vireakbuntham.seats.layout',
    request: { method: 'POST', endpoint: endpoint, body: body },
    auditable: internal_order || integration
  ) do
    response = post(endpoint, body)
    handle_response(response, SpreeCmCommissioner::Integrations::VireakBuntham::Resources::SeatLayout)
  end
end

#get_unavailable_seats!(journey:, date:, internal_order: nil) ⇒ Object

POST /seat/unavailable — occupied seats for a journey on a date.



73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'app/services/spree_cm_commissioner/integrations/vireak_buntham/external_client/client.rb', line 73

def get_unavailable_seats!(journey:, date:, internal_order: nil)
  endpoint = 'seat/unavailable'
  body = { journey: journey, date: date }

  with_audit(
    event_base: 'vireakbuntham.seats.unavailable',
    request: { method: 'POST', endpoint: endpoint, body: body },
    auditable: internal_order || integration
  ) do
    response = post(endpoint, body)
    handle_response(response, SpreeCmCommissioner::Integrations::VireakBuntham::Resources::Seat, collection: true)
  end
end