Module: IronBank::Queryable

Included in:
Resource
Defined in:
lib/iron_bank/queryable.rb

Overview

Query-like features, such as ‘find` and `where` methods for a resource.

Instance Method Summary collapse

Instance Method Details

#allObject



38
39
40
# File 'lib/iron_bank/queryable.rb', line 38

def all
  where({})
end

#find(id) ⇒ Object

We use the REST endpoint for the ‘find` method



8
9
10
11
12
13
14
15
16
# File 'lib/iron_bank/queryable.rb', line 8

def find(id)
  raise IronBank::NotFoundError unless id

  response = IronBank.client.connection.get(
    "v1/object/#{object_name}/#{id}"
  )

  new(IronBank::Object.new(response.body).deep_underscore)
end

#find_eachObject

This methods leverages the fact that Zuora only returns 2,000 records at a time, hance providing a default batch size

See knowledgecenter.zuora.com/DC_Developers/BC_ZOQL#Limits



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/iron_bank/queryable.rb', line 23

def find_each
  return enum_for(:find_each) unless block_given?

  client       = IronBank.client
  query_string = IronBank::QueryBuilder.zoql(object_name, query_fields)
  query_result = client.query(query_string) # up to 2k records from Zuora

  loop do
    query_result[:records].each { |data| yield new(data) }
    break if query_result[:done]

    query_result = client.query_more(query_result[:queryLocator])
  end
end

#firstObject



42
43
44
# File 'lib/iron_bank/queryable.rb', line 42

def first
  where({}, limit: 1).first
end

#where(conditions, limit: IronBank::Actions::Query::DEFAULT_ZUORA_LIMIT) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/iron_bank/queryable.rb', line 46

def where(conditions, limit: IronBank::Actions::Query::DEFAULT_ZUORA_LIMIT)
  query_string = IronBank::QueryBuilder.
                 zoql(object_name, query_fields, conditions)

  IronBank.logger.info "query: #{query_string}"

  records = IronBank::Query.call(query_string, limit: limit)[:records]
  return [] unless records

  records.each.with_object([]) do |data, result|
    result << new(data)
  end
end