Skip to content

Commit

Permalink
Constants dynamically created. Some tests in CampaignManagement
Browse files Browse the repository at this point in the history
  • Loading branch information
jplopez committed Jan 3, 2014
1 parent b588768 commit bf7ef5e
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 46 deletions.
2 changes: 1 addition & 1 deletion lib/bing-ads-api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

require 'savon'
require 'bing-ads-api/config'
require 'bing-ads-api/constants'
require 'bing-ads-api/service'
require 'bing-ads-api/client_proxy'
require 'bing-ads-api/data_object'

require 'bing-ads-api/constants'

# Require services
Dir[File.join(File.dirname(__FILE__), 'bing-ads-api', 'service', '*.rb')].each { |file| require file }
Expand Down
10 changes: 5 additions & 5 deletions lib/bing-ads-api.yml
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ constants:
monthly_budget_spend_until_depleted: 'MonthlyBudgetSpendUntilDepleted'
daily_budget_accelerated: 'DailyBudgetAccelerated'
daily_budget_standard: 'DailyBudgetStandard'
campaign_status:
campaign_statuses:
active: 'Active'
paused: 'Paused'
budget_paused: 'BudgetPaused'
Expand All @@ -147,12 +147,12 @@ constants:
pricing_model:
cpc: 'Cpc'
cpm: 'Cpm'
ad_group_status:
ad_group_statuses:
draft: 'Draft'
active: 'Active'
paused: 'Paused'
deleted: 'Deleted'
ad_status:
ad_statuses:
inactive: 'Inactive'
Active: 'Active'
paused: 'Paused'
Expand All @@ -163,11 +163,11 @@ constants:
mobile: 'Mobile'
rich_search: 'RichSearch'
product: 'Product'
keyword_editorial_status:
keyword_editorial_statuses:
active: 'Active'
disapproved: 'Disapproved'
inactive: 'Inactive'
keyword_status:
keyword_statuses:
active: 'Active'
paused: 'Paused'
deleted: 'Deleted'
Expand Down
78 changes: 60 additions & 18 deletions lib/bing-ads-api/constants.rb
Original file line number Diff line number Diff line change
@@ -1,32 +1,74 @@
# -*- encoding : utf-8 -*-

module BingAdsApi

# Public : Utility class for TimeZones values
#
# Example
# BingAdsApi::TimeZone.SANTIAGO
# # => 'Santiago'
#
# Author [email protected]
module TimeZone

module CommonConstants
class <<self
BingAdsApi::Config.instance.common_constants.each do |key, value|

define_method("#{key.upcase}") do |constant=nil|
value[constant] if constant
value
BingAdsApi::Config.instance.common_constants['time_zones'].each do |key, value|
TimeZone.const_set(key.upcase, value)
end

end

# Public : Utility class for AdLanguages values
#
# Example
# BingAdsApi::AdLanguages.SPANISH
# # => 'Spanish'
# BingAdsApi::AdLanguages.SPANISH_CODE
# # => 'ES'
#
# Author [email protected]
module AdLanguage

BingAdsApi::Config.instance.common_constants['ad_languages'].each do |key, value|
if key == 'codes'
value.each do |code_key, code_value|
AdLanguage.const_set("#{code_key.upcase}_CODE", code_value)
end

else
AdLanguage.const_set(key.upcase, value)
end
end
end

end


module CampaignManagementConstants
class <<self
BingAdsApi::Config.instance.campaign_management_constants.each do |key, value|

define_method("#{key.upcase}") do |constant=nil|
value[constant.to_s] if constant
value
end

## Dynamic classes for campaign management constants
BingAdsApi::Config.instance.campaign_management_constants.each do |const_key, const_value|

const_module = Module.new do
# Dynamically create Constants classes for each value found
const_value.each do |key, value|
self.const_set(key.upcase, value)
end
end

end
BingAdsApi.const_set(const_key.classify, const_module)

end


## Dynamic classes for reporting constants
BingAdsApi::Config.instance.reporting_constants.each do |const_key, const_value|

const_class = Class.new(Object) do
# Dynamically create Constants classes for each value found
const_value.each do |key, value|
self.const_set(key.upcase, value)
end

end
BingAdsApi.const_set(const_key.classify, const_class)

end

end
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
module BingAdsApi

class Campaign < BingAdsApi::DataObject
include BingAdsApi::CommonConstants
include BingAdsApi::CampaignManagementConstants
include BingAdsApi::TimeZone
include BingAdsApi::BudgetLimitType
include BingAdsApi::CampaignStatus
include BingAdsApi::PricingModel


attr_accessor :budget_type,
:conversion_tracking_enabled,
Expand Down
15 changes: 0 additions & 15 deletions lib/bing-ads-api/data/common.rb

This file was deleted.

2 changes: 2 additions & 0 deletions lib/bing-ads-api/data_object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,7 @@ def to_hash
self.instance_variables.each {|var| hash[var.to_s.delete("@")] = self.instance_variable_get(var) }
return hash
end


end
end
9 changes: 9 additions & 0 deletions lib/bing-ads-api/service/campaign_management.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@ def initialize(options={})
def get_campaigns_by_account_id(account_id)
response = call(:get_campaigns_by_account_id,
{account_id: account_id})
response_hash = get_response_hash(response, __method__)
puts "response_hash"
puts response_hash
campaigns = []
response_hash[:campaigns][:campaign].each do |camp_hash|
campaigns << BingAdsApi::Campaign.new(camp_hash)
end
puts "campaigns"
puts campaigns.inspect
return get_response_hash(response, __method__)
end

Expand Down
10 changes: 5 additions & 5 deletions test/campaign_management_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,23 @@ def setup
test "get campaigns by account" do
response = @service.get_campaigns_by_account_id(@options[:account_id])
assert !response.nil?, "No response received"
assert !response.is_a?(Hash), "No Hash as response received"
assert response.is_a?(Hash), "No Hash as response received"
end

test "add campaigns" do
campaigns = [
BingAdsApi::Campaign.new(
:budget_type => BingAdsApi::Campaign.BUDGET_LIMIT_TYPE['daily_budget_standard'],
:budget_type => BingAdsApi::Campaign::DAILY_BUDGET_STANDARD,
:conversion_tracking_enabled => "false",
:daily_budget => 2000,
:daylight_saving => "false",
:description => "Some Campaign",
:monthly_budget => 5400,
:name => "Some campaign",
:status => BingAdsApi::Campaign.CAMPAIGN_STATUS['paused'],
:time_zone => BingAdsApi::Campaign.TIME_ZONES['santiago']),
:status => BingAdsApi::Campaign::PAUSED,
:time_zone => BingAdsApi::Campaign::SANTIAGO),
]
response = service.add_campaigns(options[:account_id], campaigns)
response = @service.add_campaigns(@options[:account_id], campaigns)
puts "response.inspect"
puts response.inspect
end
Expand Down

0 comments on commit bf7ef5e

Please sign in to comment.