Module: FacebookAds::Test::Fixtures

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

Instance Method Summary collapse

Instance Method Details

#ad_accountObject



12
13
14
# File 'lib/facebook_ads/test/fixtures.rb', line 12

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

#create_ad(params = {}) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/facebook_ads/test/fixtures.rb', line 22

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



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/facebook_ads/test/fixtures.rb', line 56

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



16
17
18
19
20
# File 'lib/facebook_ads/test/fixtures.rb', line 16

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

#create_campaign(params = {}) ⇒ Object



45
46
47
48
49
50
51
52
53
54
# File 'lib/facebook_ads/test/fixtures.rb', line 45

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



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/facebook_ads/test/fixtures.rb', line 73

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



38
39
40
41
42
43
# File 'lib/facebook_ads/test/fixtures.rb', line 38

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

#randomize(string) ⇒ Object



90
91
92
# File 'lib/facebook_ads/test/fixtures.rb', line 90

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