Class: Adapi::ManagedCustomer

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

Overview

Account

Constant Summary collapse

ATTRIBUTES =

PS: lots of these attributes are read-only

[ :name, :login, :company_name, :customer_id, 
:can_manage_clients, :currency_code, :date_time_zone ]

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 = {}) ⇒ ManagedCustomer

Returns a new instance of ManagedCustomer.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/adapi/managed_customer.rb', line 25

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

  params[:service_name] = :ManagedCustomerService
  
  # this model uses the latest version of AdWords API. 
  # the rest of the model still use v201109_1 
  params[:api_version] = :v201206

  @xsi_type = 'ManagedCustomer'

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

  super(params)
end

Class Method Details

.find(amount = :first, 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/adapi/managed_customer.rb', line 62

def self.find(amount = :first, params = {})
  # find single campaign by id
  if params.empty? and not amount.is_a?(Symbol)
    params[:customer_id] = amount.to_i
    amount = :first
  end

  params.symbolize_keys!
  first_only = (amount.to_sym == :first)

  # create predicate from supported search parameters
  predicates = [ :name, :customer_id, :currency_code ].map do |param_name|
    if params[param_name]
      { :field => param_name.to_s.camelcase, :operator => 'IN', :values => Array( params[param_name] ) }
    end
  end.compact

  # REFACTOR take fields from attributes using some common method
  select_fields = %w{ Name Login companyName customerId 
    canManageClients currencyCode dateTimeZone }

  selector = {
    :fields => select_fields,
    # :ordering => [ { field: 'Name', sort_order: 'ASCENDING' } ],
    :predicates => predicates
  }

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

  response = (response and response[:entries]) ? response[:entries] : []

#      response.map! do |data|
#        ManagedCustomer.new(data.merge( :customer_id => data[:customer_id] )
#        TextAd.new(data[:ad].merge(:ad_group_id => data[:ad_group_id], :status => data[:status]))
#      end

  first_only ? response.first : response
end

Instance Method Details

#attributesObject Also known as: to_hash



16
17
18
# File 'lib/adapi/managed_customer.rb', line 16

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

#createObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/adapi/managed_customer.rb', line 43

def create
  return false unless self.valid?      
  
  operand = Hash[
    [ :name, :currency_code, :date_time_zone ].map do |k|
      [ k.to_sym, self.send(k) ] if self.send(k)
    end.compact
  ]

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

  check_for_errors(self)

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