Class: Stripe::SearchResultObject

Inherits:
StripeObject show all
Includes:
Enumerable, APIOperations::Request, APIOperations::Search
Defined in:
lib/stripe/search_result_object.rb

Constant Summary collapse

OBJECT_NAME =
"search_result"

Constants inherited from StripeObject

Stripe::StripeObject::RESERVED_FIELD_NAMES

Instance Attribute Summary collapse

Attributes inherited from StripeObject

#last_response

Class Method Summary collapse

Instance Method Summary collapse

Methods included from APIOperations::Request

included

Methods included from APIOperations::Search

#_search

Methods inherited from StripeObject

#==, #[]=, 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) ⇒ SearchResultObject

Returns a new instance of SearchResultObject.



26
27
28
29
# File 'lib/stripe/search_result_object.rb', line 26

def initialize(*args)
  super
  self.filters = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Stripe::StripeObject

Instance Attribute Details

#filtersObject

This accessor allows a ‘SearchResultObject` 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.



17
18
19
# File 'lib/stripe/search_result_object.rb', line 17

def filters
  @filters
end

Class Method Details

.empty_search_result(opts = {}) ⇒ Object

An empty search result 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.



22
23
24
# File 'lib/stripe/search_result_object.rb', line 22

def self.empty_search_result(opts = {})
  SearchResultObject.construct_from({ data: [] }, opts, :v1)
end

.object_nameObject



10
11
12
# File 'lib/stripe/search_result_object.rb', line 10

def self.object_name
  "search_result"
end

Instance Method Details

#[](key) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/stripe/search_result_object.rb', line 31

def [](key)
  case key
  when String, Symbol
    super
  else
    raise ArgumentError,
          "You tried to access the #{key.inspect} index, but " \
          "SearchResultObject types only support String keys. " \
          "(HINT: Search calls return an object with a 'data' (which is " \
          "the data array). You likely want to call #data[#{key.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_search_result_page.



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/stripe/search_result_object.rb', line 64

def auto_paging_each(&blk)
  return enum_for(:auto_paging_each) unless block_given?

  page = self

  loop do
    page.each(&blk)
    page = page.next_search_result_page

    break if page.empty?
  end
end

#each(&blk) ⇒ Object

Iterates through each resource in the page represented by the current ‘SearchListObject`.

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.



49
50
51
# File 'lib/stripe/search_result_object.rb', line 49

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

#empty?Boolean

Returns true if the page object contains no elements.

Returns:

  • (Boolean)


54
55
56
# File 'lib/stripe/search_result_object.rb', line 54

def empty?
  data.empty?
end

#next_search_result_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
# File 'lib/stripe/search_result_object.rb', line 81

def next_search_result_page(params = {}, opts = {})
  return self.class.empty_search_result(opts) unless has_more

  params = filters.merge(page: next_page).merge(params)

  request_stripe_object(method: :get, path: url, params: params, opts: opts)
end