Class: Zaius::ListObject

Inherits:
ZaiusObject show all
Includes:
Enumerable, APIOperations::List, APIOperations::Request
Defined in:
lib/zaius/list_object.rb

Constant Summary collapse

OBJECT_NAME =

include Zaius::APIOperations::Create

"list".freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from APIOperations::Request

included

Methods included from APIOperations::List

#list

Methods inherited from ZaiusObject

#==, #[]=, #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.



22
23
24
25
# File 'lib/zaius/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 Zaius::ZaiusObject

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.



13
14
15
# File 'lib/zaius/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/zaius/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/zaius/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

#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/zaius/list_object.rb', line 41

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

#resource_urlObject



51
52
53
54
# File 'lib/zaius/list_object.rb', line 51

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

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



45
46
47
48
49
# File 'lib/zaius/list_object.rb', line 45

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