Class: Latitude::API::ListObject
- Inherits:
-
Object
- Object
- Latitude::API::ListObject
- Includes:
- Enumerable
- Defined in:
- lib/latitude/api/list_object.rb
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#links ⇒ Object
readonly
Returns the value of attribute links.
-
#meta ⇒ Object
readonly
Returns the value of attribute meta.
-
#path_params ⇒ Object
readonly
Returns the value of attribute path_params.
-
#request_opts ⇒ Object
readonly
Returns the value of attribute request_opts.
-
#request_params ⇒ Object
readonly
Returns the value of attribute request_params.
-
#resource_class ⇒ Object
readonly
Returns the value of attribute resource_class.
Instance Method Summary collapse
- #[](index) ⇒ Object
- #auto_paging_each(&blk) ⇒ Object
- #each(&blk) ⇒ Object
- #empty? ⇒ Boolean
- #first ⇒ Object
- #has_more? ⇒ Boolean
-
#initialize(resource_class:, parsed:, request_params: {}, request_opts: {}, path_params: {}) ⇒ ListObject
constructor
A new instance of ListObject.
- #last ⇒ Object
- #next_page(opts = {}) ⇒ Object
- #page_number ⇒ Object
- #page_size ⇒ Object
- #previous_page(opts = {}) ⇒ Object
- #size ⇒ Object (also: #length)
- #to_a ⇒ Object
Constructor Details
#initialize(resource_class:, parsed:, request_params: {}, request_opts: {}, path_params: {}) ⇒ ListObject
Returns a new instance of ListObject.
10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/latitude/api/list_object.rb', line 10 def initialize(resource_class:, parsed:, request_params: {}, request_opts: {}, path_params: {}) @resource_class = resource_class = parsed.is_a?(Hash) ? (parsed["meta"] || {}) : {} @links = parsed.is_a?(Hash) ? (parsed["links"] || {}) : {} raw_data = parsed.is_a?(Hash) ? Array(parsed["data"]) : [] @data = raw_data.map do |row| resource_class.new(row, path_params: path_params, opts: request_opts) end @request_params = request_params || {} @request_opts = request_opts || {} @path_params = path_params || {} end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
8 9 10 |
# File 'lib/latitude/api/list_object.rb', line 8 def data @data end |
#links ⇒ Object (readonly)
Returns the value of attribute links.
8 9 10 |
# File 'lib/latitude/api/list_object.rb', line 8 def links @links end |
#meta ⇒ Object (readonly)
Returns the value of attribute meta.
8 9 10 |
# File 'lib/latitude/api/list_object.rb', line 8 def end |
#path_params ⇒ Object (readonly)
Returns the value of attribute path_params.
8 9 10 |
# File 'lib/latitude/api/list_object.rb', line 8 def path_params @path_params end |
#request_opts ⇒ Object (readonly)
Returns the value of attribute request_opts.
8 9 10 |
# File 'lib/latitude/api/list_object.rb', line 8 def request_opts @request_opts end |
#request_params ⇒ Object (readonly)
Returns the value of attribute request_params.
8 9 10 |
# File 'lib/latitude/api/list_object.rb', line 8 def request_params @request_params end |
#resource_class ⇒ Object (readonly)
Returns the value of attribute resource_class.
8 9 10 |
# File 'lib/latitude/api/list_object.rb', line 8 def resource_class @resource_class end |
Instance Method Details
#[](index) ⇒ Object
37 38 39 |
# File 'lib/latitude/api/list_object.rb', line 37 def [](index) @data[index] end |
#auto_paging_each(&blk) ⇒ Object
90 91 92 93 94 95 96 97 98 99 |
# File 'lib/latitude/api/list_object.rb', line 90 def auto_paging_each(&blk) page = self loop do page.each(&blk) break unless page.has_more? page = page.next_page end self end |
#each(&blk) ⇒ Object
23 24 25 |
# File 'lib/latitude/api/list_object.rb', line 23 def each(&blk) @data.each(&blk) end |
#empty? ⇒ Boolean
33 34 35 |
# File 'lib/latitude/api/list_object.rb', line 33 def empty? @data.empty? end |
#first ⇒ Object
41 42 43 |
# File 'lib/latitude/api/list_object.rb', line 41 def first @data.first end |
#has_more? ⇒ Boolean
63 64 65 66 67 68 |
# File 'lib/latitude/api/list_object.rb', line 63 def has_more? return !!@links["next"] if @links && @links["next"] return page_number < ["total_pages"].to_i if && ["total_pages"] @data.size >= page_size end |
#last ⇒ Object
45 46 47 |
# File 'lib/latitude/api/list_object.rb', line 45 def last @data.last end |
#next_page(opts = {}) ⇒ Object
70 71 72 73 74 75 76 77 78 |
# File 'lib/latitude/api/list_object.rb', line 70 def next_page(opts = {}) return empty_page unless has_more? params = deep_dup(@request_params) params[:page] ||= {} params[:page][:number] = page_number + 1 params[:page][:size] = page_size fetch(params, opts) end |
#page_number ⇒ Object
58 59 60 61 |
# File 'lib/latitude/api/list_object.rb', line 58 def page_number value = page_params["number"] || page_params[:number] (value || 1).to_i end |
#page_size ⇒ Object
53 54 55 56 |
# File 'lib/latitude/api/list_object.rb', line 53 def page_size value = page_params["size"] || page_params[:size] (value || 20).to_i end |
#previous_page(opts = {}) ⇒ Object
80 81 82 83 84 85 86 87 88 |
# File 'lib/latitude/api/list_object.rb', line 80 def previous_page(opts = {}) return empty_page if page_number <= 1 params = deep_dup(@request_params) params[:page] ||= {} params[:page][:number] = page_number - 1 params[:page][:size] = page_size fetch(params, opts) end |
#size ⇒ Object Also known as: length
27 28 29 |
# File 'lib/latitude/api/list_object.rb', line 27 def size @data.size end |
#to_a ⇒ Object
49 50 51 |
# File 'lib/latitude/api/list_object.rb', line 49 def to_a @data.dup end |