Class: SpreeCmCommissioner::Integrations::Larryta::Resources::Base

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.from_api_response(data) ⇒ Object, Array

Parse API response data into resource objects Handles both single and collection responses

Parameters:

  • data (Hash)

    The API response body

Returns:

  • (Object, Array)

    Single resource or array of resources



8
9
10
11
12
13
14
15
16
17
18
# File 'app/services/spree_cm_commissioner/integrations/larryta/resources/base.rb', line 8

def self.from_api_response(data)
  return [] unless data.is_a?(Hash)

  if data['data'].is_a?(Array)
    data['data'].map { |item| new((item, data)) }
  elsif data['data'].is_a?(Hash)
    new((data['data'], data))
  else
    new(data)
  end
end

Instance Method Details

#to_hObject

Default to_h method - override in subclasses for custom serialization



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

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