module IronBank::Queryable

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

Public Instance Methods

all() click to toggle source
# File lib/iron_bank/queryable.rb, line 38
def all
  where({})
end
find(id) click to toggle source

We use the REST endpoint for the `find` method

# 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_each() { |new(data)| ... } click to toggle source

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

# 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
first() click to toggle source
# File lib/iron_bank/queryable.rb, line 42
def first
  where({}, limit: 1).first
end
where(conditions, limit: IronBank::Actions::Query::DEFAULT_ZUORA_LIMIT) click to toggle source
# 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