Skip to content

Commit de97a98

Browse files
authored
Add support for Tailored Audience Permissions (#202)
* Add support for Tailored Audience Permissions * bump version
1 parent a6af521 commit de97a98

3 files changed

Lines changed: 145 additions & 1 deletion

File tree

examples/audience_permissions.rb

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# frozen_string_literal: true
2+
# Copyright (C) 2019 Twitter, Inc.
3+
4+
require 'digest'
5+
require 'twitter-ads'
6+
include TwitterAds::Enum
7+
8+
CONSUMER_KEY = 'your consumer key'
9+
CONSUMER_SECRET = 'your consumer secret'
10+
ACCESS_TOKEN = 'user access token'
11+
ACCESS_TOKEN_SECRET = 'user access token secret'
12+
ADS_ACCOUNT = 'ads account id'
13+
14+
# initialize the twitter ads api client
15+
client = TwitterAds::Client.new(
16+
CONSUMER_KEY,
17+
CONSUMER_SECRET,
18+
ACCESS_TOKEN,
19+
ACCESS_TOKEN_SECRET
20+
)
21+
22+
# load up the account instance
23+
account = client.accounts(ADS_ACCOUNT)
24+
25+
tailored_audience_id = '36n4f'
26+
27+
# fetch all permissions
28+
permissions = TwitterAds::TailoredAudiencePermission.all(account, tailored_audience_id)
29+
30+
permissions.each { |data|
31+
p data.id
32+
p data.permission_level
33+
p data.granted_account_id
34+
}
35+
36+
# create instance
37+
permission = TwitterAds::TailoredAudiencePermission.new(account)
38+
39+
# set required params
40+
permission.tailored_audience_id = tailored_audience_id
41+
permission.granted_account_id = '18ce54uvbwu'
42+
permission.permission_level = PermissionLevel::READ_ONLY
43+
44+
# set permission
45+
response = permission.save
46+
47+
# delete permission
48+
permission.id = response.id
49+
permission.delete!

lib/twitter-ads/audiences/tailored_audience.rb

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,4 +138,99 @@ def users(params)
138138
end
139139

140140
end
141+
142+
class TailoredAudiencePermission
143+
144+
include TwitterAds::DSL
145+
include TwitterAds::Resource
146+
147+
attr_reader :account
148+
149+
# read-only
150+
property :created_at, type: :time, read_only: true
151+
property :updated_at, type: :time, read_only: true
152+
property :deleted, type: :bool, read_only: true
153+
154+
property :id
155+
property :tailored_audience_id
156+
property :granted_account_id
157+
property :permission_level
158+
159+
RESOURCE_COLLECTION = "/#{TwitterAds::API_VERSION}/" \
160+
'accounts/%{account_id}/tailored_audiences/' \
161+
'%{tailored_audience_id}/permissions' # @api private
162+
RESOURCE = "/#{TwitterAds::API_VERSION}/" \
163+
'accounts/%{account_id}/tailored_audiences/' \
164+
'%{tailored_audience_id}/permissions/%{id}' # @api private
165+
166+
def initialize(account)
167+
@account = account
168+
self
169+
end
170+
171+
class << self
172+
173+
# Retrieve details for some or
174+
# all permissions associated with the specified tailored audience.
175+
#
176+
# @exapmle
177+
# permissions = TailoredAudiencePermission.all(account, '36n4f')
178+
#
179+
# @param account [Account] The account object instance.
180+
# @param tailored_audience_id [String] The tailored audience id.
181+
#
182+
# @since 5.2.0
183+
#
184+
# @return [TailoredAudiencePermission] The tailored audience permission instance.
185+
def all(account, tailored_audience_id, opts = {})
186+
params = {}.merge!(opts)
187+
resource = RESOURCE_COLLECTION % {
188+
account_id: account.id,
189+
tailored_audience_id: tailored_audience_id
190+
}
191+
request = Request.new(account.client, :get, resource, params: params)
192+
Cursor.new(self, request, init_with: [account])
193+
end
194+
195+
end
196+
197+
# Saves or updates the current object instance
198+
# depending on the presence of `object.tailored_audience_id`.
199+
#
200+
# @exapmle
201+
# object.save
202+
#
203+
# @since 5.2.0
204+
#
205+
# @return [self] Returns the instance refreshed from the API.
206+
def save
207+
resource = RESOURCE_COLLECTION % {
208+
account_id: account.id,
209+
tailored_audience_id: tailored_audience_id
210+
}
211+
params = to_params
212+
response = Request.new(account.client, :post, resource, params: params).perform
213+
from_response(response.body[:data])
214+
end
215+
216+
# Deletes the current or specified tailored audience permission.
217+
#
218+
# @example
219+
# object.delete!
220+
#
221+
# Note: calls to this method are destructive and irreverisble.
222+
#
223+
# @since 5.2.0
224+
#
225+
# @return [self] Returns the instance refreshed from the API.
226+
def delete!
227+
resource = RESOURCE % {
228+
account_id: account.id,
229+
tailored_audience_id: tailored_audience_id,
230+
id: @id
231+
}
232+
response = Request.new(account.client, :delete, resource).perform
233+
from_response(response.body[:data])
234+
end
235+
end
141236
end

lib/twitter-ads/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
# Copyright (C) 2019 Twitter, Inc.
33

44
module TwitterAds
5-
VERSION = '5.1.0'
5+
VERSION = '5.2.0'
66
end

0 commit comments

Comments
 (0)