Class: Adapi::BudgetOrder

Inherits:
Api
  • Object
show all
Defined in:
lib/adapi/budget_order.rb

Overview

BudgetOrderService

Constant Summary collapse

ATTRIBUTES =

PS: id is only read only-value

[ :billing_account_id, :id, :spending_limit, :start_date_time, :end_date_time ]

Constants inherited from Api

Api::API_EXCEPTIONS, Api::LOGGER

Instance Attribute Summary

Attributes inherited from Api

#adwords, #id, #params, #service, #status, #version, #xsi_type

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Api

#[], #[]=, #check_for_errors, create, #mutate, #new?, #persisted?, #store_errors, to_micro_units, #to_param, update

Constructor Details

#initialize(params = {}) ⇒ BudgetOrder

Returns a new instance of BudgetOrder.



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/adapi/budget_order.rb', line 23

def initialize(params = {})
  params.symbolize_keys!

  params[:service_name] = :BudgetOrderService
  
  @xsi_type = 'BudgetOrder'

  ATTRIBUTES.each do |param_name|
    self.send("#{param_name}=", params[param_name])
  end

  super(params)
end

Class Method Details

.findObject



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/adapi/budget_order.rb', line 86

def self.find

  select_fields = [
    :billing_account_id, 
    :id, 
    :spending_limit, 
    :start_date_time, 
    :end_date_time
  ].collect{|f| f.to_s.camelize }

  selector = {
    :fields => select_fields,
    :ordering => [],
    :predicates => []
  }

  response = BudgetOrder.new.service.get(selector)      

  response[:entries].collect{|e| BudgetOrder.new(e)}
end

Instance Method Details

#attributesObject Also known as: to_hash



14
15
16
# File 'lib/adapi/budget_order.rb', line 14

def attributes
  super.merge Hash[ ATTRIBUTES.map { |k| [k, self.send(k)] } ]
end

#createObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/adapi/budget_order.rb', line 37

def create
  return false unless self.valid?      
  
  operand = {
    :billing_account_id => self.send(:billing_account_id),
    :start_date_time => fix_time(self.send(:start_date_time)),
    :end_date_time => fix_time(self.send(:end_date_time))
  }

  if self.send(:spending_limit).is_a?(Hash)
    operand[:spending_limit] = self.send(:spending_limit)
  else
    operand[:spending_limit] = { micro_amount: Api.to_micro_units(self.send(:spending_limit)) }
  end

  response = self.mutate( 
    operator: 'ADD', 
    operand: operand
  )

  check_for_errors(self)

  self.id = response[:value].first[:id] rescue nil
end

#fix_time(a_time) ⇒ Object

TODO this is potentially brittle. update and add tests



108
109
110
# File 'lib/adapi/budget_order.rb', line 108

def fix_time(a_time)
  Time.parse(a_time.to_s).strftime("%Y%m%d %H%M%S Europe/Prague")
end

#update(params = {}) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/adapi/budget_order.rb', line 62

def update(params = {})
  return false unless self.valid?      
  
  operand = {
    :id => self.send(:id),
    :billing_account_id => self.send(:billing_account_id),
    :start_date_time => fix_time(self.send(:start_date_time)),
    :end_date_time => fix_time(self.send(:end_date_time))
  }

  if self.send(:spending_limit).is_a?(Hash)
    operand[:spending_limit] = self.send(:spending_limit)
  else
    operand[:spending_limit] = { micro_amount: Api.to_micro_units(self.send(:spending_limit)) }
  end

  response = self.mutate(
    operator: 'SET', 
    operand: operand
  )      

  check_for_errors(self)
end