Class: Phaxio::Resource::Collection

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/phaxio/resource.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response_data, resource) ⇒ Collection

Returns a new collection of resource instances for this data. Generally this is not called directly.



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/phaxio/resource.rb', line 141

def initialize response_data, resource
  # For some endpoints we'll get a hash with `paging` and `data` attributes.
  # For others, just an array.
  if response_data.is_a? Hash
    if response_data.key? 'paging'
      self.total = response_data['paging']['total']
      self.per_page = response_data['paging']['per_page']
      self.page = response_data['paging']['page']
    end
    self.raw_data = response_data['data']
  else
    self.raw_data = response_data
  end
  self.collection = raw_data.map { |record_data| resource.response_record record_data }
end

Instance Attribute Details

#collectionObject

The raw response data



135
136
137
# File 'lib/phaxio/resource.rb', line 135

def collection
  @collection
end

#pageObject

The raw response data



135
136
137
# File 'lib/phaxio/resource.rb', line 135

def page
  @page
end

#per_pageObject

The raw response data



135
136
137
# File 'lib/phaxio/resource.rb', line 135

def per_page
  @per_page
end

#raw_dataObject

The raw response data



135
136
137
# File 'lib/phaxio/resource.rb', line 135

def raw_data
  @raw_data
end

#totalObject

The raw response data



135
136
137
# File 'lib/phaxio/resource.rb', line 135

def total
  @total
end

Instance Method Details

#[](idx) ⇒ Object



157
158
159
# File 'lib/phaxio/resource.rb', line 157

def [] idx
  collection[idx]
end

#each(&block) ⇒ Object



161
162
163
# File 'lib/phaxio/resource.rb', line 161

def each(&block)
  collection.each(&block)
end

#lengthObject



165
166
167
# File 'lib/phaxio/resource.rb', line 165

def length
  collection.length
end

#sizeObject



169
170
171
# File 'lib/phaxio/resource.rb', line 169

def size
  length
end