Skip to content

Commit

Permalink
Merge pull request nov#196 from blinqmedia/support_redownload
Browse files Browse the repository at this point in the history
Support redownload
  • Loading branch information
nov committed Feb 22, 2012
2 parents 8bac44f + 7bcdb8c commit 9207502
Show file tree
Hide file tree
Showing 13 changed files with 339 additions and 7 deletions.
22 changes: 19 additions & 3 deletions lib/fb_graph/ad_campaign.rb
Expand Up @@ -2,16 +2,32 @@ module FbGraph
class AdCampaign < Node
include Connections::AdGroups

attr_accessor :campaign_id, :account_id, :name, :start_time, :end_time, :daily_budget, :campaign_status, :lifetime_budget
attr_accessor :campaign_id, :account_id, :name, :start_time, :end_time, :updated_time, :daily_budget, :daily_imps, :campaign_status, :lifetime_budget

def initialize(identifier, attributes = {})
super
set_attrs(attributes)
end

def update(options)
response = super(options)

if [1, "1", true].include?(options.symbolize_keys[:redownload])
attributes = options.merge(response[:data][:campaigns][identifier].symbolize_keys)
set_attrs(attributes)
end

response
end

protected

%w(campaign_id account_id name daily_budget campaign_status lifetime_budget).each do |field|
def set_attrs(attributes)
%w(campaign_id account_id name daily_budget daily_imps campaign_status lifetime_budget).each do |field|
send("#{field}=", attributes[field.to_sym])
end

%w(start_time end_time).each do |field|
%w(start_time end_time updated_time).each do |field|
if val = attributes[field.to_sym]
# Handles integer timestamps and ISO8601 strings
time = Time.parse(val) rescue Time.at(val.to_i)
Expand Down
18 changes: 18 additions & 0 deletions lib/fb_graph/ad_group.rb
Expand Up @@ -5,7 +5,25 @@ class AdGroup < Node

def initialize(identifier, attributes = {})
super
set_attrs(attributes)
end

# We override update to handle the "redownload" parameter
# If redownload is specified, the FbGraph::AdGroup object will be updated with the data returned from Facebook.
def update(options)
response = super(options)

if [1, "1", true].include?(options.symbolize_keys[:redownload])
attributes = options.merge(response[:data][:adgroups][identifier].symbolize_keys)
set_attrs(attributes)
end

response
end

protected

def set_attrs(attributes)
%w(ad_id campaign_id name adgroup_status bid_type max_bid targeting creative creative_ids adgroup_id bid_info disapprove_reason_descriptions).each do |field|
send("#{field}=", attributes[field.to_sym])
end
Expand Down
11 changes: 10 additions & 1 deletion lib/fb_graph/connections/ad_campaigns.rb
Expand Up @@ -12,9 +12,18 @@ def ad_campaigns(options = {})

def ad_campaign!(options = {})
ad_campaign = post options.merge(:connection => :adcampaigns)
AdCampaign.new ad_campaign[:id], options.merge(ad_campaign).merge(

ad_campaign_id = ad_campaign[:id]

merged_attrs = options.merge(
:access_token => options[:access_token] || self.access_token
)

if [1, "1", true].include?(options.symbolize_keys[:redownload])
merged_attrs.merge!(ad_campaign[:data][:campaigns][ad_campaign_id].symbolize_keys)
end

AdCampaign.new ad_campaign_id, merged_attrs
end
end
end
Expand Down
15 changes: 12 additions & 3 deletions lib/fb_graph/connections/ad_groups.rb
Expand Up @@ -14,11 +14,20 @@ def ad_groups(options = {})
# cannot be created via the AdCampaign connection
def ad_group!(options = {})
ad_group = post options.merge(:connection => :adgroups)
AdGroup.new ad_group[:id], options.merge(ad_group).merge(

adgroup_id = ad_group[:id]

merged_attrs = options.merge(
:access_token => options[:access_token] || self.access_token,
:ad_id => ad_group[:id].to_i,
:adgroup_id => ad_group[:id].to_i
:ad_id => adgroup_id.to_i,
:adgroup_id => adgroup_id.to_i
)

if [1, "1", true].include?(options.symbolize_keys[:redownload])
merged_attrs.merge!(ad_group[:data][:adgroups][adgroup_id].symbolize_keys)
end

AdGroup.new ad_group[:id], merged_attrs
end
end
end
Expand Down
36 changes: 36 additions & 0 deletions spec/fb_graph/ad_campaign_spec.rb
Expand Up @@ -59,3 +59,39 @@
end
end
end

describe FbGraph::AdCampaign, '.update' do
context "without the redownload parameter" do
it "should return true from facebook" do
mock_graph :post, '6003590469668', 'true', :name => "New Name" do
attributes = {
:id => '6003590469668',
:name => "Original Name"
}
ad_campaign = FbGraph::AdCampaign.new(attributes.delete(:id), attributes)
ad_campaign.update(:name => "New Name").should be_true

end
end
end

context "with the redownload parameter" do
it "should update the AdCampaign with the new data from facebook" do
mock_graph :post, "6004167532222", 'ad_campaigns/test_ad_campaign_update_with_redownload', :name => "New Name", :redownload => true do
attributes = {
:id => "6004167532222",
:campaign_status => 1,
:name => "New Name"
}

ad_campaign = FbGraph::AdCampaign.new(attributes.delete(:id), attributes)
ad_campaign.campaign_status.should == 1

ad_campaign.update(:name => "New Name", :redownload => true)
ad_campaign.name.should == "New Name"

ad_campaign.campaign_status.should == 2
end
end
end
end
39 changes: 39 additions & 0 deletions spec/fb_graph/ad_group_spec.rb
Expand Up @@ -51,3 +51,42 @@
end
end
end

describe FbGraph::AdGroup, '.update' do
context "without the redownload parameter" do
it "should return true from facebook" do
mock_graph :post, '6003590469668', 'true', :max_bid => 500 do
attributes = {
:id => '6003590469668',
:max_bid => 1000
}
ad_group = FbGraph::AdGroup.new(attributes.delete(:id), attributes)
ad_group.update(:max_bid => 500).should be_true

end
end
end

context "with the redownload parameter" do
it "should update the AdGroup with the new data from facebook" do
mock_graph :post, "6004165047777", 'ad_groups/test_ad_group_update_with_redownload', :max_bid => 500, :redownload => true do
attributes = {
:id => "6004165047777",
:adgroup_id => "6004165047777",
:adgroup_status => 1,
:max_bid => 1000
}

ad_group = FbGraph::AdGroup.new(attributes.delete(:id), attributes)
ad_group.adgroup_status.should == 1

ad_group.update(:max_bid => 500, :redownload => true)

ad_group.max_bid.should == "500"

# Our test assumes that adgroup_status has changed on Facebook's side and is passed back different
ad_group.adgroup_status.should == 4
end
end
end
end
36 changes: 36 additions & 0 deletions spec/fb_graph/connections/ad_campaigns_spec.rb
@@ -0,0 +1,36 @@
require 'spec_helper'
describe FbGraph::Connections::AdCampaigns, '#ad_campaign!' do
context 'when included by FbGraph::AdAccount' do
it 'should return generated ad_account' do
mock_graph :post, 'act_22334455/adcampaigns', 'ad_accounts/ad_campaigns/post_with_valid_access_token' do
ad_campaign = FbGraph::AdAccount.new('act_22334455', :access_token => 'valid').ad_campaign!(
:name => "Campaign 1",
:daily_budget => 500
)

ad_campaign.identifier.should == "6004167532222"
end
end

it 'should handle the redownload parameter' do
mock_graph :post, 'act_22334455/adcampaigns', 'ad_accounts/ad_campaigns/post_with_redownload' do
ad_campaign = FbGraph::AdAccount.new('act_22334455', :access_token => 'valid').ad_campaign!(
:name => "Campaign 1",
:daily_budget => 500,
:redownload => true
)

ad_campaign.identifier.should == "6004167532222"
ad_campaign.account_id.should == 22334455
ad_campaign.name.should == "Campaign 1"
ad_campaign.daily_budget.should == 500
ad_campaign.campaign_status.should == 2
ad_campaign.daily_imps.should == 0
ad_campaign.start_time.should == Time.at(1330282800)
ad_campaign.end_time.should == Time.at(1330588800)
ad_campaign.updated_time.should == Time.at(1329850926)
end
end
end
end

26 changes: 26 additions & 0 deletions spec/fb_graph/connections/ad_groups_spec.rb
Expand Up @@ -62,6 +62,32 @@
ad_group.end_time.should == Time.parse("2011-09-20T16:00:00-04:00")
end
end

it 'should handle the redownload parameter' do
mock_graph :post, 'act_22334455/adgroups', 'ad_accounts/ad_groups/post_with_redownload' do
ad_group = FbGraph::AdAccount.new('act_22334455', :access_token => 'valid').ad_group!(
:name => "Test Ad 2",
:campaign_id => 11223344,
:bid_type => 1,
:max_bid => 100,
:start_time => Time.parse("2012-02-20T00:00:00Z"),
:end_time => Time.parse("2012-02-23T00:00:00Z"),
:redownload => true
)

ad_group.identifier.should == "22334455"
ad_group.campaign_id.should == 11223344
ad_group.name.should == "Test Ad 2"
ad_group.bid_type.should == 1
# Not sure why...but Facebook returns max_bid as a String
ad_group.max_bid.should == "100"
ad_group.start_time.should == Time.parse("2012-02-20T00:00:00Z")
ad_group.end_time.should == Time.parse("2012-02-23T00:00:00Z")

# ad_status is not sent, only received
ad_group.adgroup_status.should == 4
end
end
end
end

19 changes: 19 additions & 0 deletions spec/mock_json/ad_accounts/ad_campaigns/post_with_redownload.json
@@ -0,0 +1,19 @@
{
"id": "6004167532222",
"data": {
"campaigns": {
"6004167532222": {
"account_id": 22334455,
"campaign_id": 6004167532222,
"name": "Campaign 1",
"daily_budget": 500,
"campaign_status": 2,
"daily_imps": 0,
"id": "6004167532222",
"start_time": 1330282800,
"end_time": 1330588800,
"updated_time": 1329850926
}
}
}
}
@@ -0,0 +1,3 @@
{
"id": "6004167532222"
}
51 changes: 51 additions & 0 deletions spec/mock_json/ad_accounts/ad_groups/post_with_redownload.json
@@ -0,0 +1,51 @@
{
"data": {
"adgroups": {
"22334455": {
"name": "Test Ad 2",
"adgroup_id": 22334455,
"ad_id": 22334455,
"creative_ids": [
22334455
],
"end_time": 1329955200,
"campaign_id": 11223344,
"account_id": 99887766,
"ad_status": 4,
"id": "22334455",
"max_bid": "100",
"bid_info": {
"1": "100"
},
"bid_type": 1,
"updated_time": 1329766354,
"adgroup_status": 4,
"targeting": {
"countries": [
"US"
]
},
"start_time": 1329696000
}
},
"creatives": {
"22334455": {
"alt_view_tags": [],
"name": "asdfasdf",
"creative_id": "22334455",
"title": "asdfasdf",
"body": "fsadfasf",
"image_url": "https://fbcdn-photos-a.akamaihd.net/photos-ak-snc1/v41818/flyers/20/8/13297657711344684017_1_bd2a5cd2.jpg",
"view_tag": "",
"count_current_adgroups": 2,
"id": "22334455",
"type": 1,
"preview_url": "http://www.facebook.com/ads/api/creative_preview.php?cid=22334455",
"run_status": 1,
"image_hash": "6dda3e0bd951ff99cd474acf58a91cf4",
"link_url": "http://example.org"
}
}
},
"id": "22334455"
}
@@ -0,0 +1,19 @@
{
"result": true,
"data": {
"campaigns": {
"6004167532222": {
"account_id": 32128888,
"campaign_id": 6004167532222,
"name": "New Name",
"daily_budget": 500,
"campaign_status": 2,
"daily_imps": 0,
"id": "6004167532222",
"start_time": 1330282800,
"end_time": 1330588800,
"updated_time": 1329853694
}
}
}
}

0 comments on commit 9207502

Please sign in to comment.