Class: Telnyx::ListObject
- Inherits:
-
TelnyxObject
- Object
- TelnyxObject
- Telnyx::ListObject
- Includes:
- Enumerable, APIOperations::Create, APIOperations::List, APIOperations::Request
- Defined in:
- lib/telnyx/list_object.rb
Instance Attribute Summary collapse
-
#filters ⇒ Object
This accessor allows a ‘ListObject` 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
- #[](k) ⇒ 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 ‘ListObject`.
-
#empty? ⇒ Boolean
Returns true if the page object contains no elements.
-
#initialize(*args) ⇒ ListObject
constructor
A new instance of ListObject.
- #more? ⇒ Boolean
-
#next_page(params = {}, opts = {}) ⇒ Object
Fetches the next page in the resource list (if there is one).
-
#page_number ⇒ Object
Fetch the current page number from metadata.
-
#page_size ⇒ Object
Fetch the current page size from metadata.
-
#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 APIOperations::Create
Methods included from APIOperations::Request
Methods included from APIOperations::List
Methods inherited from TelnyxObject
#==, #[]=, additive_object_param, additive_object_param?, #as_json, construct_from, #deleted?, #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) ⇒ ListObject
Returns a new instance of ListObject.
22 23 24 25 |
# File 'lib/telnyx/list_object.rb', line 22 def initialize(*args) super self.filters = {} end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Telnyx::TelnyxObject
Instance Attribute Details
#filters ⇒ Object
This accessor allows a ‘ListObject` 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.
13 14 15 |
# File 'lib/telnyx/list_object.rb', line 13 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.
18 19 20 |
# File 'lib/telnyx/list_object.rb', line 18 def self.empty_list(opts = {}) ListObject.construct_from({ data: [] }, opts) end |
Instance Method Details
#[](k) ⇒ Object
27 28 29 30 31 32 33 34 |
# File 'lib/telnyx/list_object.rb', line 27 def [](k) case k when String, Symbol super else raise ArgumentError, "You tried to access the #{k.inspect} index, but ListObject 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[#{k.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
.
51 52 53 54 55 56 57 58 59 60 |
# File 'lib/telnyx/list_object.rb', line 51 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 ‘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
.
41 42 43 |
# File 'lib/telnyx/list_object.rb', line 41 def each(&blk) data.each(&blk) end |
#empty? ⇒ Boolean
Returns true if the page object contains no elements.
63 64 65 |
# File 'lib/telnyx/list_object.rb', line 63 def empty? data.empty? end |
#more? ⇒ Boolean
73 74 75 |
# File 'lib/telnyx/list_object.rb', line 73 def more? !data.empty? && [:page_number] && [:total_pages] && [:total_pages] > [:page_number] 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.
81 82 83 84 85 86 87 88 |
# File 'lib/telnyx/list_object.rb', line 81 def next_page(params = {}, opts = {}) return self.class.empty_list(opts) unless more? next_page_number = page_number.to_i + 1 pagination = { number: next_page_number, size: filter_page_size } params = filters.merge(params).merge(page: pagination) list(params, opts) end |
#page_number ⇒ Object
Fetch the current page number from metadata.
109 110 111 |
# File 'lib/telnyx/list_object.rb', line 109 def page_number (:page_number, 1) end |
#page_size ⇒ Object
Fetch the current page size from metadata.
104 105 106 |
# File 'lib/telnyx/list_object.rb', line 104 def page_size (:page_size, 20) 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.
94 95 96 97 98 99 100 101 |
# File 'lib/telnyx/list_object.rb', line 94 def previous_page(params = {}, opts = {}) prev_page_number = page_number.to_i - 1 prev_page_number = [prev_page_number, 1].max pagination = { number: prev_page_number, size: filter_page_size } params = filters.merge(params).merge(page: pagination) list(params, opts) end |
#resource_url ⇒ Object
113 114 115 116 |
# File 'lib/telnyx/list_object.rb', line 113 def resource_url url || raise(ArgumentError, "List object does not contain a 'url' field.") end |
#retrieve(id, opts = {}) ⇒ Object
67 68 69 70 71 |
# File 'lib/telnyx/list_object.rb', line 67 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_telnyx_object(resp.data, opts) end |