Master (GitLab CI)
### Master (Solano CI)
Adparlor::Facebook
The adparlor-facebook gem is a ruby library for interacting with the Facebook ads API. Supporting both REST and batch requests.
Installation
Add this line to your application's Gemfile:
gem 'adparlor-facebook', git: 'https://gitlab.ak-networks.com/adparlor/facebook_ruby_ads_sdk.git'
or locally for development
bundle config local.adparlor-facebook ~/Projects/facebook_ruby_ads_sdk
gem 'adparlor-facebook', branch: 'master'
And then execute:
$ bundle
Usage
Graph API
Once an access token is obtained https://developers.facebook.com/docs/facebook-login/access-tokens you can begin
making requests to the GraphApi.
account = Adparlor::Facebook::GraphApi::AdAccount.get(account_id, access_token: 'access_token')
account.name
account.account_status
creative = Adparlor::Facebook::GraphApi::AdCreative.get(creative_id, access_token: 'access_token')
creative.id
creative = Adparlor::Facebook::GraphApi::AdCreative.get(creative_id, access_token: 'access_token', fields: %w(body image_hash image_url ...))
creative = Adparlor::Facebook::GraphApi::AdCreative.get(creative_id, access_token: 'access_token', fields: Adparlor::Facebook::GraphApi::AdCreative.fields(:all))
creative.id
creative.name
creative.thumbnail_url
account = Adparlor::Facebook::GraphApi::AdAccount.get(account_id, access_token: 'access_token')
account.adcreatives.all
account.adimages.all
account.campaigns.all
account.adcreatives.all.first
account.adcreatives.all(Adparlor::Facebook::GraphApi::AdCreative.fields(:all))).first
account.adimages.delete(hash: '6aec0dd976393abfad84e8f2511960a8')
account.adimages.create(source: 'http://www.example.com/url/to/image.png')
account.adimages.create(source: '/path/to/image.png')
Batch requests
If making large numbers of requests at the same time or requests that depend on each other the batch object can be used.
account = Adparlor::Facebook::GraphApi::AdAccount.get(account_id, access_token: 'access_token')
response = account.batch do |batch|
batch.get account.id, AdImage, fields: [:id, :name, :hash]
batch.get account.id, Campaign, access_token: account.access_token
batch.get account2.id, AdImage, access_token: account2.access_token, fields: [:id, :name, :height]
end
response
response = Adparlor::Facebook::GraphObject.new.batch do |batch|
batch.get nil, AdCreative, id: '6666666666666', access_token: 'access_token'
batch.get nil, AdCreative, id: '7777777777777', access_token: 'access_token'
end
response = account.batch do |batch|
batch.post account.id, AdImage, source: 'http://www.example.com/url/for/image.png'
batch.post account2.id, AdImage, source: 'http://www.example.com/url/for/othe-image.png', access_token: account2.access_token
end
response
response = account.batch do |batch|
batch.delete account.id, AdImage, hash: 'bee06e2e03f9b5a32419855e2c7a4225'
batch.delete account.id, AdImage, hash: 'd8ebd0caddecb512ec3b782ae9498e13'
end
response = account.batch do |batch|
batch.post account.id, AdImage, source: 'http://www.example.com/url/for/image.png'
batch.post account2.id, AdImage, source: 'http://www.example.com/url/for/othe-image.png', access_token: account2.access_token
batch.get account.id, AdImage, fields: [:id, :name, :hash]
batch.get account.id, Campaign
end
response
response = account.batch do |batch|
batch.post account.id, Campaign,
{ name: 'A Test Campaign', objective: 'LOCAL_AWARENESS', status: 'PAUSED' },
name: 'create_campaign'
batch.post account.id, AdSet,
name: 'A Test AdSet', daily_budget: 135, start_time: '00:05:00',
campaign_id: '{result=create_campaign:$.id}', bid_amount: 150,
billing_event: 'IMPRESSIONS', optimization_goal: 'REACH',
targeting: {
geo_locations: { cities: [{ key: '2418956', radius: 12, distance_unit: 'mile' }] },
page_types: %w(desktopfeed mobilefeed)
}, status: 'PAUSED',
promoted_object: { page_id: your_page_id }
end
response
Development
After checking out the repo, run bin/setup
to install dependencies. Then, run rake spec
to run the tests. You can also run bin/console
for an interactive prompt that will allow you to experiment.
License
The gem is available as open source under the terms of the MIT License.
TODO
- Create requests should probably return an instance of the object instead of the Faraday body
- ?Delete requests should return true if deleted or error message?
- Continue to add endpoints
- More configurable tests for testing live instead of the vcr cassettes.