Class: Charging::Collection

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
lib/charging/collection.rb

Overview

Represents a object collection result. It is a delegator for an array of object.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response, attributes) ⇒ Collection

:nodoc:



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/charging/collection.rb', line 10

def initialize(response, attributes) # :nodoc:
  Helpers.required_arguments!(attributes.merge('response' => response))

  @last_response = response
  
  attributes.each do |attribute, value|
    instance_variable_set("@#{attribute}", value)
  end
  
  super(load_data_with_response!)
end

Instance Attribute Details

#last_responseObject (readonly)

Responds the last http response from the API.



8
9
10
# File 'lib/charging/collection.rb', line 8

def last_response
  @last_response
end

Instance Method Details

#load_data_with_response!Object

:nodoc:



22
23
24
25
26
27
# File 'lib/charging/collection.rb', line 22

def load_data_with_response! # :nodoc:
  return [] if last_response.code != 200

  raw_lines = MultiJson.decode(last_response.body)
  raw_lines.map { |raw_line| load_object_with(raw_line) }
end