Class: Locomotive::Mounter::PaginatedCollection

Inherits:
Struct
  • Object
show all
Defined in:
lib/locomotive/mounter/engine_api.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#attribute_namesObject

Returns the value of attribute attribute_names

Returns:

  • (Object)

    the current value of attribute_names



164
165
166
# File 'lib/locomotive/mounter/engine_api.rb', line 164

def attribute_names
  @attribute_names
end

#listObject

Returns the value of attribute list.



166
167
168
# File 'lib/locomotive/mounter/engine_api.rb', line 166

def list
  @list
end

#paramsObject

Returns the value of attribute params

Returns:

  • (Object)

    the current value of params



164
165
166
# File 'lib/locomotive/mounter/engine_api.rb', line 164

def params
  @params
end

#total_entriesObject Also known as: size

Returns the value of attribute total_entries.



166
167
168
# File 'lib/locomotive/mounter/engine_api.rb', line 166

def total_entries
  @total_entries
end

#total_pagesObject

Returns the value of attribute total_pages.



166
167
168
# File 'lib/locomotive/mounter/engine_api.rb', line 166

def total_pages
  @total_pages
end

#urlObject

Returns the value of attribute url

Returns:

  • (Object)

    the current value of url



164
165
166
# File 'lib/locomotive/mounter/engine_api.rb', line 164

def url
  @url
end

Instance Method Details

#each(&block) ⇒ Object



170
171
172
173
174
175
176
# File 'lib/locomotive/mounter/engine_api.rb', line 170

def each(&block)
  if self.total_pages == 1 && self.list
    self.list.each(&block)
  else
    self.paginated_each(&block)
  end
end

#pageObject



198
199
200
# File 'lib/locomotive/mounter/engine_api.rb', line 198

def page
  @page ||= 1
end

#paginated_each(&block) ⇒ Object



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/locomotive/mounter/engine_api.rb', line 178

def paginated_each(&block)
  loop do
    self.params[:query][:page] = self.page

    response  = Locomotive::Mounter::EngineApi.get(self.url, self.params)
    data      = response.parsed_response

    if response.success?
      self.list = Locomotive::Mounter::EngineApi.send(:keep_attributes, data, self.attribute_names)
      self.list.each(&block)
    else
      raise ApiReadException.new(data['error'])
    end

    break if self.page >= self.total_pages

    @page += 1
  end
end