Class: Atomsphere::Query
- Inherits:
-
Object
- Object
- Atomsphere::Query
- Defined in:
- lib/atomsphere/query.rb,
lib/atomsphere/query/builder.rb,
lib/atomsphere/query/expression.rb,
lib/atomsphere/query/builder/group.rb,
lib/atomsphere/query/builder/property.rb,
lib/atomsphere/query/expression/simple_expression.rb,
lib/atomsphere/query/expression/grouping_expression.rb
Defined Under Namespace
Classes: Builder, Expression, GroupingExpression, SimpleExpression
Instance Attribute Summary collapse
-
#filter ⇒ GroupingExpression
top level GroupingExpression for query.
-
#object_type ⇒ String
name of the object to query.
-
#page ⇒ Integer
readonly
the number of pages retrieved.
-
#result_pages ⇒ Array<Api::Response>
readonly
array of api responses for each page retrieved.
Instance Method Summary collapse
-
#all_results ⇒ Array<Hash>
runs #next_page to retrieve all #result_pages until #last_page? is ‘false`, and then returns all rows.
-
#initialize(params = {}) ⇒ Query
constructor
accepts either a string of the name of the object to query, or a hash of options.
-
#last_page? ⇒ true, false
returns ‘true` when any pages have been retrieved the value of #query_token is `nil`.
-
#next_page ⇒ Api::Response, false
retrieve the next page for the query.
-
#results ⇒ Array<Hash>
returns rows from #result_pages that have been retrieved.
- #run ⇒ Object
-
#to_hash ⇒ Hash
validates all parameters with #validate! and returns a hash of the query that will be sent to the boomi api.
-
#to_json ⇒ String
query json that will be sent to the boomi api.
-
#validate! ⇒ true, false
run all ‘validate_*!` private methods to ensure validity of query parameters.
Constructor Details
#initialize(params = {}) ⇒ Query
accepts either a string of the name of the object to query, or a hash of options
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# 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 |
Instance Attribute Details
#filter ⇒ GroupingExpression
top level GroupingExpression for query
10 11 12 |
# File 'lib/atomsphere/query.rb', line 10 def filter @filter end |
#object_type ⇒ String
name of the object to query
10 11 12 |
# File 'lib/atomsphere/query.rb', line 10 def object_type @object_type end |
#page ⇒ Integer (readonly)
the number of pages retrieved
10 11 12 |
# File 'lib/atomsphere/query.rb', line 10 def page @page end |
#result_pages ⇒ Array<Api::Response> (readonly)
array of api responses for each page retrieved
10 11 12 |
# File 'lib/atomsphere/query.rb', line 10 def result_pages @result_pages end |
Instance Method Details
#all_results ⇒ Array<Hash>
runs #next_page to retrieve all #result_pages until #last_page? is ‘false`, and then returns all rows
64 65 66 67 |
# File 'lib/atomsphere/query.rb', line 64 def all_results next_page until last_page? results end |
#last_page? ⇒ true, false
returns ‘true` when any pages have been retrieved the value of #query_token is `nil`
71 72 73 |
# File 'lib/atomsphere/query.rb', line 71 def last_page? !page.eql?(0) && query_token.nil? end |
#next_page ⇒ Api::Response, false
retrieve the next page for the query
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# 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 |
#results ⇒ Array<Hash>
returns rows from #result_pages that have been retrieved
57 58 59 |
# File 'lib/atomsphere/query.rb', line 57 def results result_pages.map(&:to_hash).map{ |h| h['result'] }.map(&:compact).flatten(1) end |
#run ⇒ Object
51 52 53 |
# File 'lib/atomsphere/query.rb', line 51 def run next_page end |
#to_hash ⇒ Hash
validates all parameters with #validate! and returns a hash of the query that will be sent to the boomi api
99 100 101 102 103 104 105 106 107 |
# File 'lib/atomsphere/query.rb', line 99 def to_hash validate! if filter.nil? nil else { QueryFilter: filter.to_hash } end end |
#to_json ⇒ String
query json that will be sent to the boomi api
112 113 114 |
# File 'lib/atomsphere/query.rb', line 112 def to_json JSON.pretty_generate to_hash end |
#validate! ⇒ true, false
run all ‘validate_*!` private methods to ensure validity of query parameters
43 44 45 46 47 48 |
# 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 |