Class: Moonclerk::ListObject
- Inherits:
-
APIResource
- Object
- MoonclerkObject
- APIResource
- Moonclerk::ListObject
- Includes:
- Enumerable, APIOperations::List
- Defined in:
- lib/moonclerk/list_object.rb
Instance Attribute Summary collapse
-
#count ⇒ Object
This accessor allows a ‘ListObject` to inherit a count that was given to a predecessor.
-
#offset ⇒ Object
This accessor allows a ‘ListObject` to inherit a count that was given to a predecessor.
Class Method Summary collapse
-
.empty_list ⇒ Object
An empty list object.
Instance Method Summary collapse
- #[](k) ⇒ Object
-
#auto_paging_each(&blk) ⇒ Object
Iterates through each resource in all pages, making additional fetches to the API as necessary.
- #create(params = {}) ⇒ Object
-
#each(&blk) ⇒ Object
Iterates through each resource in the page represented by the current ‘ListObject`.
-
#empty? ⇒ Boolean
Returns true if the page object contains no elements.
-
#next_page(params = {}) ⇒ Object
Fetches the next page in the resource list (if there is one).
-
#previous_page(params = {}) ⇒ Object
Fetches the previous page in the resource list (if there is one).
- #retrieve(id) ⇒ Object
Methods included from APIOperations::List
Methods inherited from APIResource
class_name, find, #refresh, retrieve, url, #url
Methods included from APIOperations::Request
Methods inherited from MoonclerkObject
#==, #[]=, #_dump, _load, #as_json, construct_from, #initialize, #inspect, #keys, #refresh_from, #respond_to?, #serialize_nested_object, serialize_params, #to_hash, #to_json, #to_s, #update_attributes, #values
Constructor Details
This class inherits a constructor from Moonclerk::MoonclerkObject
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Moonclerk::MoonclerkObject
Instance Attribute Details
#count ⇒ Object
This accessor allows a ‘ListObject` to inherit a count that was given to a predecessor. This allows consistent counts as a user pages through resources. Offset is used to shift the starting point of the list.
9 10 11 |
# File 'lib/moonclerk/list_object.rb', line 9 def count @count end |
#offset ⇒ Object
This accessor allows a ‘ListObject` to inherit a count that was given to a predecessor. This allows consistent counts as a user pages through resources. Offset is used to shift the starting point of the list.
9 10 11 |
# File 'lib/moonclerk/list_object.rb', line 9 def offset @offset end |
Class Method Details
.empty_list ⇒ Object
An empty list object. This is returned from next
when we know that there isn’t a next page in order to replicate the behavior of the API when it attempts to return a page beyond the last.
14 15 16 |
# File 'lib/moonclerk/list_object.rb', line 14 def self.empty_list ListObject.construct_from({ data: [], object: "" }) end |
Instance Method Details
#[](k) ⇒ Object
18 19 20 21 22 23 24 25 |
# File 'lib/moonclerk/list_object.rb', line 18 def [](k) case k when String, Symbol super else raise ArgumentError.new("You tried to access the #{k.inspect} index, but ListObject types only support String keys.") end end |
#auto_paging_each(&blk) ⇒ Object
Iterates through each resource in all pages, making additional fetches to the API as necessary.
Note that this method will make as many API calls as necessary to fetch all resources. For more granular control, please see each
and next_page
.
42 43 44 45 46 47 48 49 50 51 |
# File 'lib/moonclerk/list_object.rb', line 42 def auto_paging_each(&blk) return enum_for(:auto_paging_each) unless block_given? page = self loop do page.each(&blk) page = page.next_page break if page.empty? end end |
#create(params = {}) ⇒ Object
64 65 66 67 |
# File 'lib/moonclerk/list_object.rb', line 64 def create(params = {}) response = request(:post, url, params) Util.convert_to_moonclerk_object(response) end |
#each(&blk) ⇒ Object
Iterates through each resource in the page represented by the current ‘ListObject`.
Note that this method makes no effort to fetch a new page when it gets to the end of the current page’s resources. See also auto_paging_each
.
32 33 34 |
# File 'lib/moonclerk/list_object.rb', line 32 def each(&blk) self.data.each(&blk) end |
#empty? ⇒ Boolean
Returns true if the page object contains no elements.
54 55 56 |
# File 'lib/moonclerk/list_object.rb', line 54 def empty? self.data.blank? end |
#next_page(params = {}) ⇒ Object
Fetches the next page in the resource list (if there is one).
This method will try to respect the count of the current page. If none was given, the default count will be fetched again.
73 74 75 76 77 78 79 80 |
# File 'lib/moonclerk/list_object.rb', line 73 def next_page(params = {}) params = { :count => count, # may be nil :offset => (offset || 0) + (count || 20), }.merge(params) list(params) end |
#previous_page(params = {}) ⇒ Object
Fetches the previous page in the resource list (if there is one).
This method will try to respect the count of the current page. If none was given, the default count will be fetched again.
86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/moonclerk/list_object.rb', line 86 def previous_page(params = {}) new_offset = (offset || 0) - (count || 20) new_offset = 0 if new_offset < 0 params = { :count => count, # may be nil :offset => new_offset, }.merge(params) list(params) end |
#retrieve(id) ⇒ Object
58 59 60 61 62 |
# File 'lib/moonclerk/list_object.rb', line 58 def retrieve(id) id, retrieve_params = Util.normalize_id(id) response = request(:get,"#{url}/#{CGI.escape(id)}", retrieve_params) Util.convert_to_moonclerk_object(response) end |