Class: RockRMS::Response::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/rock_rms/response/base.rb

Constant Summary collapse

BASE_MAPPING =
{
  id: 'Id',
  created_date_time: 'CreatedDateTime',
  modified_date_time: 'ModifiedDateTime',
  attributes: 'Attributes',
  attribute_values: 'AttributeValues'
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Base

Returns a new instance of Base.



18
19
20
# File 'lib/rock_rms/response/base.rb', line 18

def initialize(data)
  @data = data
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



4
5
6
# File 'lib/rock_rms/response/base.rb', line 4

def data
  @data
end

Class Method Details

.format(data) ⇒ Object



14
15
16
# File 'lib/rock_rms/response/base.rb', line 14

def self.format(data)
  new(data).format
end

Instance Method Details

#formatObject



22
23
24
25
26
27
28
# File 'lib/rock_rms/response/base.rb', line 22

def format
  if data.is_a?(Array)
    data.map { |item| format_single(item) }
  else
    format_single(data)
  end
end

#format_attributes(res, klass) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/rock_rms/response/base.rb', line 45

def format_attributes(res, klass)
  return res if res.nil?

  res.each_with_object({}) do |(attr, val), object|
    object[attr] = klass.format(val)
  end
end

#to_h(dict, data) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/rock_rms/response/base.rb', line 30

def to_h(dict, data)
  return {} if data.nil?

  dict
    .merge(BASE_MAPPING)
    .each_with_object({}) do |(l, r), object|
      if l == :attributes || l == :attribute_values
        format_klass = l == :attributes ? Attribute : AttributeValue
        object[l] = format_attributes(data[r], format_klass)
      else
        object[l] = data[r]
      end
    end
end