Module: FacebookAds::Test::Fixtures

Included in:
Base
Defined in:
lib/facebook_ads/test/fixtures.rb

Instance Method Summary collapse

Instance Method Details

#ad_accountObject



24
25
26
# File 'lib/facebook_ads/test/fixtures.rb', line 24

def 
  FacebookAds::AdAccount.get(config.)
end

#create_ad(params = {}) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/facebook_ads/test/fixtures.rb', line 34

def create_ad(params = {})
  ad_set ||= create_ad_set.tap { |c| yield c if block_given? }
  creative ||= create_creative.tap { |c| yield c if block_given? }

  params = {
    name: 'My Ad',
    adset_id: ad_set.id,
    creative: {
      creative_id: creative.id,
    },
    status: 'PAUSED',
  }

  ad = .ads.create(params)
end

#create_ad_set(campaign = nil, params = {}) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/facebook_ads/test/fixtures.rb', line 68

def create_ad_set(campaign = nil, params = {})
  # yield generated object to add them to cleanup queue
  campaign ||= create_campaign.tap { |c| yield c if block_given? }

  .adsets.create({
    campaign_id: campaign.id,
    name: randomize('Test AdSet'),
    targeting: {
      geo_locations: { countries: ['US'] }
    },
    optimization_goal: 'IMPRESSIONS',
    billing_event: 'IMPRESSIONS',
    bid_amount: 100,
    daily_budget: 1000,
  })
end

#create_adlabelObject



28
29
30
31
32
# File 'lib/facebook_ads/test/fixtures.rb', line 28

def create_adlabel
  .adlabels.create({
    name: randomize('My ad label')
  })
end

#create_campaign(params = {}) ⇒ Object



57
58
59
60
61
62
63
64
65
66
# File 'lib/facebook_ads/test/fixtures.rb', line 57

def create_campaign(params = {})
  params = {
    name: randomize('Testing Campaign'),
    buying_type: 'AUCTION',
    objective: 'LINK_CLICKS',
    status: 'PAUSED',
  }.merge(params)

  .campaigns.create(params)
end

#create_creative(image = nil, params = {}) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/facebook_ads/test/fixtures.rb', line 85

def create_creative(image = nil, params = {})
  image ||= create_image

  .adcreatives.create({
    name: randomize('Test Creative'),
    object_story_spec: {
      page_id: config.page_id,
      link_data: {
        message: 'try it out',
        link: config.app_url,
        caption: 'www.example.com',
        image_hash: image.hash,
      }
    }
  })
end

#create_imageObject



50
51
52
53
54
55
# File 'lib/facebook_ads/test/fixtures.rb', line 50

def create_image
  images = .adimages.create({
    filename: config.image_path
  })
  images[0]
end

#randomize(string) ⇒ Object



102
103
104
# File 'lib/facebook_ads/test/fixtures.rb', line 102

def randomize(string)
  "#{string} #{SecureRandom.hex}"
end