Class: Adapi::AdGroup
Constant Summary collapse
- ATTRIBUTES =
[ :id, :campaign_id, :name, :status, :bids, :keywords, :ads ]
Constants inherited from Api
Adapi::Api::API_EXCEPTIONS, Adapi::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
- #attributes ⇒ Object (also: #to_hash)
-
#bids=(params = {}) ⇒ Object
convert bids to GoogleApi format.
- #create ⇒ Object
-
#delete ⇒ Object
PS: perhaps also change the ad_group name when deleting.
-
#find_ads(first_only = false) ⇒ Object
TODO find all types of ads.
- #find_keywords(first_only = false) ⇒ Object
-
#initialize(params = {}) ⇒ AdGroup
constructor
A new instance of AdGroup.
- #update(params = {}) ⇒ Object
Methods inherited from Api
#[], #[]=, #check_for_errors, create, #mutate, #new?, #persisted?, #store_errors, to_micro_units, #to_param, update
Constructor Details
#initialize(params = {}) ⇒ AdGroup
Returns a new instance of AdGroup.
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/adapi/ad_group.rb', line 23 def initialize(params = {}) params[:service_name] = :AdGroupService @xsi_type = 'AdGroup' ATTRIBUTES.each do |param_name| self.send("#{param_name}=", params[param_name]) end @keywords ||= [] @ads ||= [] super(params) end |
Class Method Details
.find(amount = :all, params = {}) ⇒ Object
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
# File 'lib/adapi/ad_group.rb', line 182 def self.find(amount = :all, params = {}) params.symbolize_keys! first_only = (amount.to_sym == :first) # by default, exclude ad_groups with status DELETED params[:status] ||= %w{ ENABLED PAUSED } raise "Campaign ID is required" unless params[:campaign_id] predicates = [ :campaign_id, :id, :name, :status ].map do |param_name| if params[param_name].present? {:field => param_name.to_s.camelcase, :operator => 'IN', :values => Array( params[param_name] ) } end end.compact select_fields = %w{ Id CampaignId Name Status } # add Bids atributes select_fields += %w{ EnhancedCpcEnabled ProxyKeywordMaxCpc KeywordMaxCpc KeywordContentMaxCpc } selector = { :fields => select_fields, :ordering => [{:field => 'Name', :sort_order => 'ASCENDING'}], :predicates => predicates } response = AdGroup.new.service.get(selector) ad_groups = (response and response[:entries]) ? response[:entries] : [] ad_groups = ad_groups.slice(0,1) if first_only # find keywords and ads ad_groups.map! do |ad_group| ad_group.merge( :keywords => Keyword.shortened(Keyword.find(:all, :ad_group_id => ad_group[:id]).keywords), :ads => Ad::TextAd.find(:all, :ad_group_id => ad_group[:id]).map(&:to_hash) ) end ad_groups.map! { |ad_group| AdGroup.new(ad_group) } first_only ? ad_groups.first : ad_groups end |
Instance Method Details
#attributes ⇒ Object Also known as: to_hash
17 18 19 |
# File 'lib/adapi/ad_group.rb', line 17 def attributes super.merge Hash[ ATTRIBUTES.map { |k| [k, self.send(k)] } ] end |
#bids=(params = {}) ⇒ Object
convert bids to GoogleApi format
can be either string (just xsi_type) or hash (xsi_type with params) although I’m not sure if just string makes sense in this case
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/adapi/ad_group.rb', line 44 def bids=(params = {}) @bids = params if @bids unless @bids.is_a?(Hash) @bids = { :xsi_type => @bids } end # convert bid amounts to micro_amounts [ :proxy_keyword_max_cpc ].each do |k| if @bids[k] and not @bids[k].is_a?(Hash) @bids[k] = { :amount => { :micro_amount => Api.to_micro_units(@bids[k]) } } end end end end |
#create ⇒ Object
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 100 101 102 |
# File 'lib/adapi/ad_group.rb', line 65 def create return false unless self.valid? operand = Hash[ [:campaign_id, :name, :status, :bids].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 = response[:value].first[:id] rescue nil if @keywords.size > 0 keyword = Adapi::Keyword.create( :ad_group_id => @id, :keywords => @keywords ) check_for_errors(keyword, :prefix => "Keyword") end if not @ads.empty? ad = Adapi::Ad::TextAd.create( :ads => @ads.map { |ad_data| ad_data.merge(:ad_group_id => @id) } ) check_for_errors(ad, :prefix => "Ad \"#{ad.headline}\"") end self.errors.empty? rescue AdGroupError => e false end |
#delete ⇒ Object
PS: perhaps also change the ad_group name when deleting
178 179 180 |
# File 'lib/adapi/ad_group.rb', line 178 def delete update(:status => 'DELETED') end |
#find_ads(first_only = false) ⇒ Object
TODO find all types of ads
231 232 233 |
# File 'lib/adapi/ad_group.rb', line 231 def find_ads(first_only = false) Ad::TextAd.find( (first_only ? :first : :all), :ad_group_id => self.id ) end |
#find_keywords(first_only = false) ⇒ Object
226 227 228 |
# File 'lib/adapi/ad_group.rb', line 226 def find_keywords(first_only = false) Keyword.find( (first_only ? :first : :all), :ad_group_id => self.id ) end |
#update(params = {}) ⇒ Object
104 105 106 107 108 109 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 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/adapi/ad_group.rb', line 104 def update(params = {}) # step 1. update core attributes core_attributes = [ :id, :campaign_id, :name, :status, :bids ] # get operand in google format # parse the given params by initialize method... ad_group = Adapi::AdGroup.new(params) # HOTFIX remove :service_name param inserted by initialize method params.delete(:service_name) # ...and load parsed params back into the hash core_params = Hash[ core_attributes.map { |k| [k, ad_group.send(k)] if params[k].present? }.compact ] response = ad_group.mutate( :operator => 'SET', :operand => core_params.merge( :id => @id, :campaign_id => @campaign_id ) ) check_for_errors(ad_group, :store_errors => false) # step 2. update keywords # delete everything and create new keywords if params[:keywords] and not params[:keywords].empty? # delete existing keywords # OPTIMIZE should be all in one request Keyword.find(:all, :ad_group_id => @id).keywords.each do |keyword| Keyword.new(:ad_group_id => @id).delete(keyword[:text][:criterion][:id]) end # create new keywords result = Adapi::Keyword.create( :ad_group_id => @id, :keywords => params[:keywords] ) check_for_errors(result, :prefix => "Keyword") end # step 3. update ads # ads can't be updated, gotta remove them all and add new ads if params[:ads] and not params[:ads].empty? # remove all existing ads # TODO change into class method existing_ads = self.find_ads ad = existing_ads.first.delete( :ad_group_id => @id, :ad_ids => existing_ads.map { |ad| ad.id } ) unless ad # REFACTOR self.errors.add(:base, add.errors.) return false end =begin self.find_ads.each do |ad| unless ad.destroy self.errors.add("Ad \"#{ad.headline}\"", ["cannot be deleted"]) return false end end =end # create new ads ad = Adapi::Ad::TextAd.create( :ads => params[:ads].map { |ad_data| ad_data.merge(:ad_group_id => @id) } ) check_for_errors(ad, :prefix => "Ad \"#{ad.headline}\"") end self.errors.empty? rescue AdGroupError => e false end |