Class: Adapi::Ad::TextAd

Inherits:
Adapi::Ad show all
Defined in:
lib/adapi/ad/text_ad.rb

Overview

code.google.com/apis/adwords/docs/reference/latest/AdGroupAdService.TextAd.html

Ad::TextAd == AdGroupAd::TextAd

This model supports both individual and batch create/update of ads. If :ads parameter is not nil, it is considered as array of ads to be updated in batch.

Constant Summary collapse

ATTRIBUTES =
[ :headline, :description1, :description2, :ads ]

Constants inherited from Adapi::Api

Adapi::Api::API_EXCEPTIONS, Adapi::Api::LOGGER

Instance Attribute Summary

Attributes inherited from Adapi::Ad

#ad_group_id, #approval_status, #disapproval_reasons, #display_url, #id, #trademark_disapproved, #url

Attributes inherited from Adapi::Api

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

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Adapi::Ad

#delete, #destroy

Methods inherited from Adapi::Api

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

Constructor Details

#initialize(params = {}) ⇒ TextAd

Returns a new instance of TextAd.



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

def initialize(params = {})
  params[:service_name] = :AdGroupAdService

  @xsi_type = 'TextAd'

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

  super(params)
end

Class Method Details

.find(amount = :all, params = {}) ⇒ Object

Raises:

  • (ArgumentError)


110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/adapi/ad/text_ad.rb', line 110

def self.find(amount = :all, params = {})
  params.symbolize_keys!
  first_only = (amount.to_sym == :first)

  # for ActiveRecord compatibility, we don't use anything besides conditions
  # params for now
  params = params[:conditions] if params[:conditions]

  # we need ad_group_id
  raise ArgumentError, "AdGroup ID is required" unless params[:ad_group_id]
 
  # supported condition parameters: ad_group_id and id
  predicates = [ :ad_group_id, :id ].map do |param_name|
    if params[param_name]
      { :field => param_name.to_s.camelcase, :operator => 'IN', :values => Array( params[param_name] ) }
    end
  end.compact

  selector = {
    :fields => ['Id', 'AdGroupId', 'Headline' ],
    :ordering => [{:field => 'Id', :sort_order => 'ASCENDING'}],
    :predicates => predicates
  }

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

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

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

  # TODO convert to TextAd instances
  first_only ? response.first : response
end

Instance Method Details

#attributesObject Also known as: to_hash



18
19
20
# File 'lib/adapi/ad/text_ad.rb', line 18

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

#createObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/adapi/ad/text_ad.rb', line 40

def create
  @ads = [ self.attributes ] unless @ads 

  operations = []

  @ads.each do |ad_params|

    ad = TextAd.new(ad_params)

    operand = ad.attributes.delete_if do |k|
      [ :campaign_id, :ad_group_id, :id, :status, :ads ].include?(k.to_sym)
    end.symbolize_keys

    operations << {
      :operator => 'ADD',
      :operand => {
        :ad_group_id => ad.ad_group_id,
        :status => ad.status,
        :ad => operand
      }
    }
  end

  self.mutate(operations)

  # check for PolicyViolationErrors, set exemptions and try again
  # do it only once. from my experience with AdWords API, multiple retries are bad practice
  if self.errors[:PolicyViolationError].any?

    self.errors[:PolicyViolationError].each do |e|
      i = e.delete(:operation_index) 
      operations[i][:exemption_requests] = [ { :key => e } ]
    end

    self.errors.clear

    self.mutate(operations)
  end

  return false if self.errors.any?

  # FIXME return ids of newly created ads
  # self.id = response[:value].first[:ad][:id] rescue nil
  
  true
end

#findObject

refresh



106
107
108
# File 'lib/adapi/ad/text_ad.rb', line 106

def find # == refresh
  TextAd.find(:first, :ad_group_id => self.ad_group_id, :id => self.id)
end

#saveObject



36
37
38
# File 'lib/adapi/ad/text_ad.rb', line 36

def save
  self.new? ? self.create : self.update
end

#update(params = {}) ⇒ Object

except for status, we cannot edit ad fields gotta delete an ad and create a new one instead this means that this method returns new ad id!

REFACTOR this method shpould be removed (but that might break something, os let’s keep it here for the moment)



94
95
96
97
98
99
100
101
102
103
104
# File 'lib/adapi/ad/text_ad.rb', line 94

def update(params = {})
  # set attributes for the "updated" ad
  create_attributes = self.attributes.merge(params).symbolize_keys
  create_attributes.delete(:id)

  # delete current ad
  return false unless self.destroy

  # create new add
  TextAd.create(create_attributes)
end