Class: Helio::ListObject

Inherits:
HelioObject show all
Includes:
Enumerable, APIOperations::Create, APIOperations::List, APIOperations::Request
Defined in:
lib/helio/list_object.rb

Constant Summary collapse

OBJECT_NAME =
"list"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from APIOperations::Create

#create

Methods included from APIOperations::Request

included

Methods included from APIOperations::List

#list

Methods inherited from HelioObject

#==, #[]=, #as_json, construct_from, #deleted?, #dirty!, #inspect, #keys, #marshal_dump, #marshal_load, protected_fields, #refresh_from, serialize_params, #serialize_params, #to_hash, #to_json, #to_s, #update_attributes, #values

Constructor Details

#initialize(*args) ⇒ ListObject

Returns a new instance of ListObject.



24
25
26
27
# File 'lib/helio/list_object.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 Helio::HelioObject

Instance Attribute Details

#filtersObject

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.



15
16
17
# File 'lib/helio/list_object.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/helio/list_object.rb', line 20

def self.empty_list(opts = {})
  ListObject.construct_from({ data: [] }, opts)
end

Instance Method Details

#[](k) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/helio/list_object.rb', line 29

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.



53
54
55
56
57
58
59
60
61
62
# File 'lib/helio/list_object.rb', line 53

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.



43
44
45
# File 'lib/helio/list_object.rb', line 43

def each(&blk)
  data.each(&blk)
end

#empty?Boolean

Returns true if the page object contains no elements.

Returns:

  • (Boolean)


65
66
67
# File 'lib/helio/list_object.rb', line 65

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.



79
80
81
82
83
84
85
86
# File 'lib/helio/list_object.rb', line 79

def next_page(params = {}, opts = {})
  return self.class.empty_list(opts) unless has_more
  last_id = data.last.id

  params = filters.merge(starting_after: last_id).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.



92
93
94
95
96
97
98
# File 'lib/helio/list_object.rb', line 92

def previous_page(params = {}, opts = {})
  first_id = data.first.id

  params = filters.merge(ending_before: first_id).merge(params)

  list(params, opts)
end

#resource_urlObject



100
101
102
103
# File 'lib/helio/list_object.rb', line 100

def resource_url
  url ||
    raise(ArgumentError, "List object does not contain a 'url' field.")
end

#retrieve(id, opts = {}) ⇒ Object



69
70
71
72
73
# File 'lib/helio/list_object.rb', line 69

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_helio_object(resp.data, opts)
end