Class: ApiBee::Node::List
Overview
Paginated node list
Resolved by Node.resolve when initial data hash contains pagination attributes :total_entries, :href and :entries Note that these are the default values for those fields and they can be configured per-API via the passed config object.
A Node::List exposes methods useful for paginating a list of nodes.
Example:
data = {
:total_entries => 10,
:href => '/products',
:page => 1,
:per_page => 20,
:entries => [
{
:title => 'Ipod'
},
{
:title => 'Ipad'
},
...
]
}
list = Node.resolve(adapter, config, data)
list.current_page # => 1
list.has_next_page? # => true
list.next_page # => 2
list.size # => 20
list.each ... # iterate current page
list.first #=> an instance of Node::Single
Constant Summary
collapse
- DEFAULT_PER_PAGE =
100
Instance Attribute Summary
Attributes inherited from ApiBee::Node
#adapter
Instance Method Summary
collapse
#[], #[]=, #has_key?, #initialize, resolve, simbolized, #to_data
Constructor Details
This class inherits a constructor from ApiBee::Node
Instance Method Details
#current_page ⇒ Object
186
187
188
|
# File 'lib/api_bee/node.rb', line 186
def current_page
(@attributes[:page] || 1).to_i
end
|
#each(&block) ⇒ Object
227
228
229
|
# File 'lib/api_bee/node.rb', line 227
def each(&block)
__entries.each(&block)
end
|
#first ⇒ Object
219
220
221
|
# File 'lib/api_bee/node.rb', line 219
def first
__entries.first
end
|
#get_one(id) ⇒ Object
Get one resource from this list Delegates to adapter.get_one(href, id) and resolves result.
173
174
175
176
|
# File 'lib/api_bee/node.rb', line 173
def get_one(id)
data = @adapter.get_one(@href, id)
data.nil? ? nil : Node.resolve(@adapter, @config, data, @href)
end
|
#has_next_page? ⇒ Boolean
211
212
213
|
# File 'lib/api_bee/node.rb', line 211
def has_next_page?
next_page <= total_pages
end
|
#has_prev_page? ⇒ Boolean
215
216
217
|
# File 'lib/api_bee/node.rb', line 215
def has_prev_page?
current_page > 1
end
|
#last ⇒ Object
223
224
225
|
# File 'lib/api_bee/node.rb', line 223
def last
__entries.last
end
|
#next_page ⇒ Object
199
200
201
|
# File 'lib/api_bee/node.rb', line 199
def next_page
current_page + 1
end
|
#pages ⇒ Object
207
208
209
|
# File 'lib/api_bee/node.rb', line 207
def pages
(1..total_pages).to_a
end
|
#paginate(options = {}) ⇒ Object
231
232
233
234
|
# File 'lib/api_bee/node.rb', line 231
def paginate(options = {})
data = @adapter.get(@attributes[@config.uri_property_name], options)
Node.resolve @adapter, @config, data, @href
end
|
#per_page ⇒ Object
190
191
192
|
# File 'lib/api_bee/node.rb', line 190
def per_page
(@attributes[:per_page] || DEFAULT_PER_PAGE).to_i
end
|
#prev_page ⇒ Object
203
204
205
|
# File 'lib/api_bee/node.rb', line 203
def prev_page
current_page - 1
end
|
#size ⇒ Object
182
183
184
|
# File 'lib/api_bee/node.rb', line 182
def size
__entries.size
end
|
#total_entries ⇒ Object
178
179
180
|
# File 'lib/api_bee/node.rb', line 178
def total_entries
@attributes[:total_entries].to_i
end
|
#total_pages ⇒ Object
194
195
196
197
|
# File 'lib/api_bee/node.rb', line 194
def total_pages
div = (total_entries.to_f / per_page.to_f).ceil
div < 1 ? 1 : div
end
|