Class: AutotaskApi::Entity

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

Class Method Summary collapse

Class Method Details

.all(client = AutotaskApi.client, id = 1) ⇒ Object



4
5
6
# File 'lib/autotask_api/entity.rb', line 4

def self.all(client = AutotaskApi.client, id = 1)
  where(Condition.new(Expression.new('id', 'GreaterThanorEquals', id)), client)
end

.clean_results(results) ⇒ Object

Parameters:

  • results (Array(Hash))


40
41
42
43
44
45
46
47
# File 'lib/autotask_api/entity.rb', line 40

def self.clean_results(results)
  results.each do |record|
    record.update(record) { |_k, v| v == { :"@xsi:type" => "xsd:string" } ? nil : v }
      .delete('@xsi:type'.to_sym)
  end

  return results
end

.expression(value) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'lib/autotask_api/entity.rb', line 28

def self.expression(value)
  case value.class
  when TrueClass
    1
  when FalseClass
    0
  else
    value
  end
end

.where(condition, client = AutotaskApi.client) ⇒ Object

Parameters:

  • conditions (Hash)


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/autotask_api/entity.rb', line 9

def self.where(condition, client = AutotaskApi.client)
  query = Query.new(self::NAME, condition, client)

  begin
    response = query.fetch
  rescue RuntimeError => e
    raise Error.new(e.message)
  end

  # return empty collection if there were no results
  return EntityCollection.new(self, [], condition, client) if response[:entity_results].nil?

  results = response[:entity_results][:entity]
  results = [results] unless results.is_a?(Array)
  results = clean_results(results)

  EntityCollection.new(self, results, condition, client)
end