Class: QuickTravel::Adapter::Deserializer

Inherits:
Object
  • Object
show all
Defined in:
lib/quick_travel/adapter.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Deserializer

Returns a new instance of Deserializer.



120
121
122
# File 'lib/quick_travel/adapter.rb', line 120

def initialize(data)
  @data = data
end

Class Method Details

.extract(raw_objects, klass, opts = {}) ⇒ Object



138
139
140
141
142
143
144
# File 'lib/quick_travel/adapter.rb', line 138

def self.extract(raw_objects, klass, opts = {})
  objects = Array.wrap(raw_objects)
  if opts[:object_key_name]
    objects = objects.map { |item| item[opts[:object_key_name]] }
  end
  objects.map { |item| klass.new(item) }
end

Instance Method Details

#extract(key, klass, opts = {}) ⇒ Object

Extract under a specified key in the data



132
133
134
135
136
# File 'lib/quick_travel/adapter.rb', line 132

def extract(key, klass, opts = {})
  collection_data = @data[key]
  fail "No collection key [#{key}] found in data from API" if collection_data.blank?
  self.class.extract(collection_data, klass, opts)
end

#extract_under_root(klass, opts = {}) ⇒ Object

Extract the ‘root’ – the data must be a direct collection of objects

opts[:object_key_name] => inside each hash extract attributes under a subkey (per object)


127
128
129
# File 'lib/quick_travel/adapter.rb', line 127

def extract_under_root(klass, opts = {})
  self.class.extract(@data, klass, opts)
end