Class: Atomsphere::Query

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Query

accepts either a string of the name of the object to query, or a hash of options

Parameters:

  • params (String) (defaults to: {})

    name of the object to query

  • params (Hash) (defaults to: {})

    parameters to initialize query with

Options Hash (params):



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

#filterGroupingExpression

top level GroupingExpression for query

Returns:



10
11
12
# File 'lib/atomsphere/query.rb', line 10

def filter
  @filter
end

#object_typeString

name of the object to query

Returns:

  • (String)

    the current value of object_type



10
11
12
# File 'lib/atomsphere/query.rb', line 10

def object_type
  @object_type
end

#pageInteger (readonly)

the number of pages retrieved

Returns:

  • (Integer)

    the current value of page



10
11
12
# File 'lib/atomsphere/query.rb', line 10

def page
  @page
end

#result_pagesArray<Api::Response> (readonly)

array of api responses for each page retrieved

Returns:



10
11
12
# File 'lib/atomsphere/query.rb', line 10

def result_pages
  @result_pages
end

Instance Method Details

#all_resultsArray<Hash>

runs #next_page to retrieve all #result_pages until #last_page? is ‘false`, and then returns all rows

Returns:

  • (Array<Hash>)

    Array of returned rows as hashes



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`

Returns:

  • (true, false)


71
72
73
# File 'lib/atomsphere/query.rb', line 71

def last_page?
  !page.eql?(0) && query_token.nil?
end

#next_pageApi::Response, false

retrieve the next page for the query

Returns:



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

#resultsArray<Hash>

returns rows from #result_pages that have been retrieved

Returns:

  • (Array<Hash>)

    Array of returned rows as hashes



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

#runObject

See Also:



51
52
53
# File 'lib/atomsphere/query.rb', line 51

def run
  next_page
end

#to_hashHash

validates all parameters with #validate! and returns a hash of the query that will be sent to the boomi api

Returns:

  • (Hash)

    hash representation of query that will be sent to the boomi api

See Also:



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_jsonString

query json that will be sent to the boomi api

Returns:

  • (String)

    JSON query string

See Also:



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

Returns:

  • (true, false)


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