Class: Chain::Query

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/chain/query.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, first_query = {}) ⇒ Query

Returns a new instance of Query.



8
9
10
11
# File 'lib/chain/query.rb', line 8

def initialize(client, first_query = {})
  @client = client
  @first_query = first_query
end

Instance Attribute Details

#clientClient (readonly)

Returns:



6
7
8
# File 'lib/chain/query.rb', line 6

def client
  @client
end

Instance Method Details

#eachvoid

This method returns an undefined value.

Iterate through objects in response, fetching the next page of results from the API as needed.

Implements required method Enumerable#each.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/chain/query.rb', line 19

def each
  page = fetch(@first_query)

  loop do
    if page['items'].empty? # we consume this array as we iterate
      break if page['last_page']
      page = fetch(page['next'])

      # The second predicate (empty?) *should* be redundant, but we check it
      # anyway as a defensive measure.
      break if page['items'].empty?
    end

    item = page['items'].shift
    yield translate(item)
  end
end

#fetch(query) ⇒ Object

This method is abstract.

Raises:

  • (NotImplementedError)


38
39
40
# File 'lib/chain/query.rb', line 38

def fetch(query)
  raise NotImplementedError
end

#translate(response_object) ⇒ Object

This method is abstract.

Overwrite to translate API response data to a different Ruby object.

Raises:

  • (NotImplementedError)


44
45
46
# File 'lib/chain/query.rb', line 44

def translate(response_object)
  raise NotImplementedError
end