class Telnyx::ListObject
Attributes
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.
Public Class Methods
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.
# File lib/telnyx/list_object.rb, line 18 def self.empty_list(opts = {}) ListObject.construct_from({ data: [] }, opts) end
# File lib/telnyx/list_object.rb, line 22 def initialize(*args) super self.filters = {} end
Public Instance Methods
# 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
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
.
# 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
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
.
# File lib/telnyx/list_object.rb, line 41 def each(&blk) data.each(&blk) end
Returns true if the page object contains no elements.
# File lib/telnyx/list_object.rb, line 63 def empty? data.empty? end
# File lib/telnyx/list_object.rb, line 73 def more? !data.empty? && meta[:page_number] && meta[:total_pages] && meta[:total_pages] > meta[:page_number] end
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.
# 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
Fetch the current page number from metadata.
# File lib/telnyx/list_object.rb, line 109 def page_number from_meta(:page_number, 1) end
Fetch the current page size from metadata.
# File lib/telnyx/list_object.rb, line 104 def page_size from_meta(:page_size, 20) end
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.
# 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
# File lib/telnyx/list_object.rb, line 113 def resource_url url || raise(ArgumentError, "List object does not contain a 'url' field.") end
# 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
Private Instance Methods
Determine page size from filters
# File lib/telnyx/list_object.rb, line 121 def filter_page_size if filters && filters[:page] && filters[:page][:size] filters[:page][:size] else 20 end end
Fetches `attribute` from current page metada. If it's not found returns `default`, which defaults to `nil`.
# File lib/telnyx/list_object.rb, line 131 def from_meta(attribute, default = nil) attribute = attribute.to_sym if meta && meta[attribute] meta.public_send(attribute) else default end end