Class: Locomotive::Mounter::PaginatedCollection
- Inherits:
-
Struct
- Object
- Struct
- Locomotive::Mounter::PaginatedCollection
- Defined in:
- lib/locomotive/mounter/engine_api.rb
Instance Attribute Summary collapse
-
#attribute_names ⇒ Object
Returns the value of attribute attribute_names.
-
#list ⇒ Object
Returns the value of attribute list.
-
#params ⇒ Object
Returns the value of attribute params.
-
#total_entries ⇒ Object
(also: #size)
Returns the value of attribute total_entries.
-
#total_pages ⇒ Object
Returns the value of attribute total_pages.
-
#url ⇒ Object
Returns the value of attribute url.
Instance Method Summary collapse
Instance Attribute Details
#attribute_names ⇒ Object
Returns the value of attribute attribute_names
164 165 166 |
# File 'lib/locomotive/mounter/engine_api.rb', line 164 def attribute_names @attribute_names end |
#list ⇒ Object
Returns the value of attribute list.
166 167 168 |
# File 'lib/locomotive/mounter/engine_api.rb', line 166 def list @list end |
#params ⇒ Object
Returns the value of attribute params
164 165 166 |
# File 'lib/locomotive/mounter/engine_api.rb', line 164 def params @params end |
#total_entries ⇒ Object 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_pages ⇒ Object
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 |
#url ⇒ Object
Returns the value of attribute 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 |
#page ⇒ Object
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 |