Class: SpreeCmCommissioner::Integrations::VireakBuntham::Resources::Base
- Inherits:
-
Object
- Object
- SpreeCmCommissioner::Integrations::VireakBuntham::Resources::Base
- Defined in:
- app/services/spree_cm_commissioner/integrations/vireak_buntham/resources/base.rb
Overview
Base resource for parsing VET (Vireak Buntham) API responses.
VET endpoints return either:
-
A plain array (e.g. /schedule/list, /seat/unavailable, /destination/from)
-
A wrapped envelope { “header”: { … }, “body”: { … } } (e.g. /booking/confirm)
Direct Known Subclasses
Booking, BookingTransaction, Location, Schedule, Seat, SeatLayout
Class Method Summary collapse
Instance Method Summary collapse
Class Method Details
.from_api_response(data) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'app/services/spree_cm_commissioner/integrations/vireak_buntham/resources/base.rb', line 8 def self.from_api_response(data) case data when Array data.map { |item| new(item) } when Hash if data['body'].is_a?(Array) data['body'].map { |item| new(item.merge('header' => data['header'])) } elsif data['body'].is_a?(Hash) new(data['body'].merge('header' => data['header'])) else new(data) end else [] end end |
Instance Method Details
#to_h ⇒ Object
25 26 27 28 29 30 |
# File 'app/services/spree_cm_commissioner/integrations/vireak_buntham/resources/base.rb', line 25 def to_h instance_variables.each_with_object({}) do |var, hash| key = var.to_s.delete('@').to_sym hash[key] = instance_variable_get(var) end end |