Class: SpreeCmCommissioner::Integrations::BookMeBusV1::Resources::Base

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Base

Returns a new instance of Base.



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

def initialize(attributes = {})
  attributes.each do |key, value|
    setter = "#{key}="
    send(setter, value) if respond_to?(setter)
  end
end

Class Method Details

.from_json_api(data) ⇒ Array

Parse JSON:API format collection response

Parameters:

  • data (Hash)

    The JSON:API response body

Returns:

  • (Array)

    Array of resource objects



7
8
9
10
11
# File 'app/services/spree_cm_commissioner/integrations/book_me_bus_v1/resources/base.rb', line 7

def from_json_api(data)
  return [] unless data['data'].is_a?(Array)

  data['data'].map { |item| from_json_api_item(item) }
end

.from_json_api_item(item) ⇒ Object

Parse a single JSON:API item

Parameters:

  • item (Hash)

    A single JSON:API resource object

Returns:

  • (Object)

    Resource object



25
26
27
28
29
30
# File 'app/services/spree_cm_commissioner/integrations/book_me_bus_v1/resources/base.rb', line 25

def from_json_api_item(item)
  attributes = item['attributes'] || {}
  attributes['id'] = item['id']
  attributes['type'] = item['type']
  new(attributes)
end

.from_json_api_single(data) ⇒ Object

Parse JSON:API format single resource response

Parameters:

  • data (Hash)

    The JSON:API response body

Returns:

  • (Object)

    Single resource object



16
17
18
19
20
# File 'app/services/spree_cm_commissioner/integrations/book_me_bus_v1/resources/base.rb', line 16

def from_json_api_single(data)
  return nil unless data['data'].is_a?(Hash)

  from_json_api_item(data['data'])
end