Sha256: fa8b1d3c8d571278d704c2ae749cc6570299251459243dcc561a3887beee0fd8

Contents?: true

Size: 1.63 KB

Versions: 24

Compression:

Stored size: 1.63 KB

Contents

# frozen_string_literal: true

module IronBank
  # Query-like features, such as `find` and `where` methods for a resource.
  #
  module Queryable
    # We use the REST endpoint for the `find` method
    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

    # This methods leverages the fact that Zuora only returns 2,000 records at a
    # time, hance providing a default batch size
    #
    # See https://knowledgecenter.zuora.com/DC_Developers/BC_ZOQL#Limits
    #
    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

    def all
      where({})
    end

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

    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
  end
end

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
iron_bank-5.4.1 lib/iron_bank/queryable.rb
iron_bank-5.4.0 lib/iron_bank/queryable.rb
iron_bank-5.3.2 lib/iron_bank/queryable.rb
iron_bank-5.3.0 lib/iron_bank/queryable.rb
iron_bank-5.2.6 lib/iron_bank/queryable.rb
iron_bank-5.2.4 lib/iron_bank/queryable.rb
iron_bank-5.2.3 lib/iron_bank/queryable.rb
iron_bank-5.2.0 lib/iron_bank/queryable.rb
iron_bank-5.1.1 lib/iron_bank/queryable.rb
iron_bank-5.1.0 lib/iron_bank/queryable.rb
iron_bank-4.4.3 lib/iron_bank/queryable.rb
iron_bank-5.0.1 lib/iron_bank/queryable.rb
iron_bank-5.0.0 lib/iron_bank/queryable.rb
iron_bank-4.4.1 lib/iron_bank/queryable.rb
iron_bank-4.4.0 lib/iron_bank/queryable.rb
iron_bank-4.3.1 lib/iron_bank/queryable.rb
iron_bank-4.3.0 lib/iron_bank/queryable.rb
iron_bank-4.2.0 lib/iron_bank/queryable.rb
iron_bank-4.1.1 lib/iron_bank/queryable.rb
iron_bank-4.1.0 lib/iron_bank/queryable.rb