class Atomsphere::Query
@attr [String] object_type
name of the object to query @attr [GroupingExpression] filter top level {GroupingExpression} for query @attr_reader [Integer] page the number of pages retrieved @attr_reader [Array<Api::Response>] result_pages
array of api responses for each page retrieved
Attributes
Public Class Methods
accepts either a string of the name of the object to query, or a hash of options @param [String] params name of the object to query @param [Hash] params parameters to initialize query with @option params [String] :object_type name of the object to query @option params [GroupingExpression] :filter top level {GroupingExpression}
# File lib/atomsphere/query.rb, line 19 def initialize(params={}) case params when String params = {object_type: params} when Symbol params = {object_type: params.to_s.upper_camelcase} end params = { object_type: nil, page: 0, result_pages: [], filter: nil }.merge(Hash[params.select{|k,v| [:object_type, :filter].include? k}]) %w(object_type page result_pages filter).each do |v| instance_variable_set :"@#{v}", params[v.to_sym] end self end
Public Instance Methods
runs {#next_page} to retrieve all {#result_pages} until {#last_page?} is `false`, and then returns all rows @return [Array<Hash>] Array of returned rows as hashes
# File lib/atomsphere/query.rb, line 64 def all_results next_page until last_page? results end
returns `true` when any pages have been retrieved the value of {#query_token} is `nil` @return [true, false]
# File lib/atomsphere/query.rb, line 71 def last_page? !page.eql?(0) && query_token.nil? end
retrieve the next page for the query @return [Api::Response, false] returns the response, or `false` if {#last_page?} is `true`
# File lib/atomsphere/query.rb, line 77 def next_page return false if last_page? begin response = if query_token.nil? @page = 1 api_client.post([object_type, :query], to_hash) else api_client.post([object_type, :queryMore], query_token) end rescue => e @page -= 1 raise e end result_pages[page-1] = response end
returns rows from {#result_pages} that have been retrieved @return [Array<Hash>] Array of returned rows as hashes
# File lib/atomsphere/query.rb, line 57 def results result_pages.map(&:to_hash).map{ |h| h['result'] }.map(&:compact).flatten(1) end
@see next_page
# File lib/atomsphere/query.rb, line 51 def run next_page end
validates all parameters with {#validate!} and returns a hash of the query that will be sent to the boomi api @see to_json
@return [Hash] hash representation of query that will be sent to the boomi api
# File lib/atomsphere/query.rb, line 99 def to_hash validate! if filter.nil? nil else { QueryFilter: filter.to_hash } end end
query json that will be sent to the boomi api @see to_hash
@return [String] JSON query string
# File lib/atomsphere/query.rb, line 112 def to_json JSON.pretty_generate to_hash end
run all `validate_*!` private methods to ensure validity of query parameters @return [true, false]
# File lib/atomsphere/query.rb, line 43 def validate! private_methods.select{ |m| m =~ /^validate_[a-z0-9_]+\!$/ }.each{ |v| send(v) } filter.validate! unless filter.nil? true end
Private Instance Methods
an instance of the API client @return [Api::Client]
# File lib/atomsphere/query.rb, line 129 def api_client Api::Client.new end
# File lib/atomsphere/query.rb, line 121 def validate_filter! unless filter.nil? || filter.is_a?(GroupingExpression) raise ArgumentError, 'filter must be a GroupingExpression' end end
# File lib/atomsphere/query.rb, line 117 def validate_object_type! raise ArgumentError, "object_type is required" if @object_type.nil? end