Class: Supercast::DataList
- Inherits:
-
DataObject
- Object
- DataObject
- Supercast::DataList
- Includes:
- Enumerable, Operations::Create, Operations::List, Operations::Request
- Defined in:
- lib/supercast/data_list.rb
Constant Summary collapse
- OBJECT_NAME =
'list'
Instance Attribute Summary collapse
-
#filters ⇒ Object
This accessor allows a ‘DataList` to inherit various filters that were given to a predecessor.
Class Method Summary collapse
-
.empty_list(opts = {}) ⇒ Object
An empty list object.
Instance Method Summary collapse
- #[](key) ⇒ Object
-
#auto_paging_each(&blk) ⇒ Object
Iterates through each resource in all pages, making additional fetches to the API as necessary.
-
#each(&blk) ⇒ Object
Iterates through each resource in the page represented by the current ‘DataList`.
-
#empty? ⇒ Boolean
Returns true if the page object contains no elements.
-
#initialize(*args) ⇒ DataList
constructor
A new instance of DataList.
-
#next_page(params = {}, opts = {}) ⇒ Object
Fetches the next page in the resource list (if there is one).
-
#previous_page(params = {}, opts = {}) ⇒ Object
Fetches the previous page in the resource list (if there is one).
- #resource_url ⇒ Object
- #retrieve(id, opts = {}) ⇒ Object
Methods included from Operations::Create
Methods included from Operations::Request
Methods included from Operations::List
Methods inherited from DataObject
#==, #[]=, #as_json, construct_from, #dirty!, #eql?, #hash, #inspect, #keys, #marshal_dump, #marshal_load, protected_fields, #serialize_params, #to_hash, #to_json, #to_s, #update_attributes, #values
Constructor Details
#initialize(*args) ⇒ DataList
Returns a new instance of DataList.
24 25 26 27 |
# File 'lib/supercast/data_list.rb', line 24 def initialize(*args) super self.filters = {} end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Supercast::DataObject
Instance Attribute Details
#filters ⇒ Object
This accessor allows a ‘DataList` to inherit various filters that were given to a predecessor. This allows for things like consistent limits, expansions, and predicates as a user pages through resources.
15 16 17 |
# File 'lib/supercast/data_list.rb', line 15 def filters @filters end |
Class Method Details
.empty_list(opts = {}) ⇒ 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.
20 21 22 |
# File 'lib/supercast/data_list.rb', line 20 def self.empty_list(opts = {}) DataList.construct_from({ data: [] }, opts) end |
Instance Method Details
#[](key) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/supercast/data_list.rb', line 29 def [](key) case key when String, Symbol super else raise ArgumentError, "You tried to access the #{key.inspect} index, but DataList " \ 'types only support String keys. (HINT: List calls return an ' \ "object with a 'data' (which is the data array). You likely " \ "want to call #data[#{key.inspect}])" 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
.
57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/supercast/data_list.rb', line 57 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 |
#each(&blk) ⇒ Object
Iterates through each resource in the page represented by the current ‘DataList`.
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
.
47 48 49 |
# File 'lib/supercast/data_list.rb', line 47 def each(&blk) data.each(&blk) end |
#empty? ⇒ Boolean
Returns true if the page object contains no elements.
70 71 72 |
# File 'lib/supercast/data_list.rb', line 70 def empty? data.empty? end |
#next_page(params = {}, opts = {}) ⇒ Object
Fetches the next page in the resource list (if there is one).
This method will try to respect the limit of the current page. If none was given, the default limit will be fetched again.
85 86 87 88 89 90 91 |
# File 'lib/supercast/data_list.rb', line 85 def next_page(params = {}, opts = {}) return self.class.empty_list(opts) unless defined?(page) && defined?(per_page) && defined?(total) && page * per_page < total params = filters.merge(page: page + 1).merge(params) list(params, opts) end |
#previous_page(params = {}, opts = {}) ⇒ Object
Fetches the previous page in the resource list (if there is one).
This method will try to respect the limit of the current page. If none was given, the default limit will be fetched again.
97 98 99 100 101 102 103 |
# File 'lib/supercast/data_list.rb', line 97 def previous_page(params = {}, opts = {}) return self.class.empty_list(opts) unless page && page > 1 params = filters.merge(page: page - 1).merge(params) list(params, opts) end |
#resource_url ⇒ Object
105 106 107 108 |
# File 'lib/supercast/data_list.rb', line 105 def resource_url url || raise(ArgumentError, "List object does not contain a 'url' field.") end |
#retrieve(id, opts = {}) ⇒ Object
74 75 76 77 78 79 |
# File 'lib/supercast/data_list.rb', line 74 def retrieve(id, opts = {}) id, retrieve_params = Util.normalize_id(id) resp, opts = request(:get, "#{resource_url}/#{CGI.escape(id)}", retrieve_params, opts) Util.convert_to_supercast_object(resp.data, opts) end |