Skip to content

Commit

Permalink
add AdParamService
Browse files Browse the repository at this point in the history
  • Loading branch information
Johannes Kaefer committed Jan 2, 2012
1 parent f0f2769 commit b650d5f
Showing 1 changed file with 87 additions and 0 deletions.
87 changes: 87 additions & 0 deletions lib/adapi/ad_param.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# encoding: utf-8

module Adapi
class AdParam < Api
attr_accessor :ad_group_id, :criterion_id, :insertion_text, :param_index

#validates_presence_of :ad_group_id

def attributes

end

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

%w{ ad_group_id criterion_id }.each do |param_name|
self.send "#{param_name}=", params[param_name.to_sym]
end

super(params)
end

# ad-specific mutate wrapper, deals with PolicyViolations for ads
#
def mutate(operation)
operation = [operation] unless operation.is_a?(Array)

# fix to save space during specifyng operations
operation = operation.map do |op|
op[:operand].delete(:status) if op[:operand][:status].nil?
op
end

begin
response = @service.mutate(operation)

rescue AdsCommon::Errors::HttpError => e
self.errors.add(:base, e.message)

# traps any exceptions raised by AdWords API
rescue AdwordsApi::Errors::ApiException => e
# return PolicyViolations so they can be sent again
e.errors.each do |error|
if (error[:api_error_type] == 'PolicyViolationError') && error[:is_exemptable]
self.errors.add(error[:api_error_type], error[:key])
else
# otherwise, just report the errors
self.errors.add( "[#{self.xsi_type.underscore}]", "#{error[:error_string]} @ #{error[:field_path]}")
end
end
end

response
end

# if nothing else than single number or string at the input, assume it's an
# id and we want to find campaign by id
#
def self.find(params = {})
params.symbolize_keys!

predicates = [ :AdGroupId ].map do |param_name|
if params[param_name]
{:field => param_name.to_s.camelcase, :operator => 'IN', :values => params[param_name] }
end
end.compact

# TODO display the rest of the data
# TODO get NetworkSetting - setting as in fields doesn't work
selector = {
:fields => ['AdGroupId', 'CriterionId', 'InsertionText', 'ParamIndex'],
:ordering => [{:field => 'AdGroupId', :sort_order => 'ASCENDING'}],
:predicates => predicates
}

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

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

response.map! do |ad_params_data|
AdParams.new(ad_params_data)
end

response
end
end
end

0 comments on commit b650d5f

Please sign in to comment.