Class: AutotaskApi::Query

Inherits:
Object
  • Object
show all
Defined in:
lib/autotask_api/query.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(entity, condition = nil, client = AutotaskApi.client) ⇒ Query

Returns a new instance of Query.



5
6
7
8
9
# File 'lib/autotask_api/query.rb', line 5

def initialize(entity, condition = nil, client = AutotaskApi.client)
  @entity = entity
  @client = client
  @condition = condition
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



3
4
5
# File 'lib/autotask_api/query.rb', line 3

def client
  @client
end

#conditionObject

Returns the value of attribute condition.



3
4
5
# File 'lib/autotask_api/query.rb', line 3

def condition
  @condition
end

#entityObject

Returns the value of attribute entity.



3
4
5
# File 'lib/autotask_api/query.rb', line 3

def entity
  @entity
end

Instance Method Details

#fetchObject



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/autotask_api/query.rb', line 11

def fetch
  response = client.call :query, query_string

  result = response.body[:query_response][:query_result]

  if result[:return_code].to_i == -1
    raise result[:errors][:atws_error][:message]
  else
    return result
  end
end

#query_stringObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/autotask_api/query.rb', line 23

def query_string
  Nokogiri::XML::Builder.new do
    sXML do
      cdata(
        Nokogiri::XML::Builder.new do |xml|
          xml.queryxml do
            xml.entity entity
            xml.query do
              condition.to_xml(xml)
            end
          end
        end.doc.root
      )
    end
  end.doc.root.to_s
end