twitter_ads 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/twitter_ads.rb +188 -0
- metadata +47 -0
data/lib/twitter_ads.rb
ADDED
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
#
|
|
2
|
+
# @ Seevibes 2015
|
|
3
|
+
# Author Thomas Landspurg
|
|
4
|
+
#
|
|
5
|
+
# thomas@seevibes.com
|
|
6
|
+
#
|
|
7
|
+
#
|
|
8
|
+
require 'oauth'
|
|
9
|
+
require 'json'
|
|
10
|
+
|
|
11
|
+
ADS_API_ENDPOINT="ads-api.twitter.com"
|
|
12
|
+
TRACE=false
|
|
13
|
+
# Usage:
|
|
14
|
+
# Initialisation
|
|
15
|
+
#
|
|
16
|
+
# ads=TwitterAds::Client.new({consumer_key :"YOUR_CONSUER_KEY",
|
|
17
|
+
# consumer_secret:"YOUR CONSUMER SECRET",
|
|
18
|
+
# access_token :"YOUR ACCESS TOKEN",
|
|
19
|
+
# access_secret :"YOUR ACCESS SECRET"})
|
|
20
|
+
# get the list of accounts:
|
|
21
|
+
# ads.accounts
|
|
22
|
+
#
|
|
23
|
+
# get info on a specfic account
|
|
24
|
+
# account=ads.account[account_id]
|
|
25
|
+
#
|
|
26
|
+
# get tailored_audience change
|
|
27
|
+
# accounts.tailored_audience_changes
|
|
28
|
+
#
|
|
29
|
+
# get tailored audience change on a specific list
|
|
30
|
+
# accounts.tailoered_audience_changes list_id
|
|
31
|
+
#
|
|
32
|
+
# if the method does not exist, the easiest it to do:
|
|
33
|
+
# client.get "/accounts/ACCOUNT_ID",params
|
|
34
|
+
#
|
|
35
|
+
# same with post, put, delete
|
|
36
|
+
#
|
|
37
|
+
module TwitterAds
|
|
38
|
+
UnauthorizedAccess="UNAUTHORIZED_ACCESS"
|
|
39
|
+
class AdsError < StandardError
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
# Common class to manage access to recourse
|
|
43
|
+
# Need access to client, prefix, and ops available on this ressource
|
|
44
|
+
#
|
|
45
|
+
# provide operation oauth autenthificated operation (get/post/put/delete)
|
|
46
|
+
# as well as dynamically discovered opertions, using the ops instance variable
|
|
47
|
+
#
|
|
48
|
+
class RestRessource
|
|
49
|
+
@prefix=""
|
|
50
|
+
attr:access_token,:prefix
|
|
51
|
+
|
|
52
|
+
# Utility funciton to do a get on the RES API
|
|
53
|
+
def get action = "" , params = nil
|
|
54
|
+
do_request :get,action,params
|
|
55
|
+
end
|
|
56
|
+
def post action , params = nil
|
|
57
|
+
do_request :post,action,params
|
|
58
|
+
end
|
|
59
|
+
def put action , params = nil
|
|
60
|
+
do_request :put,action,params
|
|
61
|
+
end
|
|
62
|
+
def delete action = "", params = nil
|
|
63
|
+
do_request :delete,action,params
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def do_request verb,action,params
|
|
67
|
+
url="https://#{ADS_API_ENDPOINT}/0/#{prefix}#{action}"
|
|
68
|
+
if url[-1] == "/" then url = url[0..-2] end
|
|
69
|
+
puts "Doing request:#{verb} #{prefix} #{action} #{params} URL:#{url}" if TRACE
|
|
70
|
+
res=::JSON.parse @client.access_token.request(verb,url,params).body
|
|
71
|
+
if res["errors"]
|
|
72
|
+
raise AdsError,res["errors"].first["code"]
|
|
73
|
+
end
|
|
74
|
+
res["data"]
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
# Dynamic check of methods
|
|
80
|
+
# tab_ops contain the list of allowed method and verbs
|
|
81
|
+
# prefix the prefix to add to the method
|
|
82
|
+
#
|
|
83
|
+
def check_method tab_ops,prefix,method_sym,do_call,*arguments, &block
|
|
84
|
+
method_sym = method_sym.id2name
|
|
85
|
+
verb=:get
|
|
86
|
+
[:post,:get,:delete,:put].each do |averb|
|
|
87
|
+
if method_sym.start_with? averb.id2name
|
|
88
|
+
verb = averb
|
|
89
|
+
method_sym[averb.id2name+"_"]=""
|
|
90
|
+
break
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
if tab_ops[verb].include? method_sym.to_sym
|
|
94
|
+
if do_call
|
|
95
|
+
method = prefix+method_sym
|
|
96
|
+
params = arguments.first
|
|
97
|
+
if params.first && params.first.class!=Hash
|
|
98
|
+
method += "/#{params.shift}"
|
|
99
|
+
end
|
|
100
|
+
return do_request verb,method,params.shift
|
|
101
|
+
else
|
|
102
|
+
return nil
|
|
103
|
+
end
|
|
104
|
+
end
|
|
105
|
+
nil
|
|
106
|
+
end
|
|
107
|
+
def method_missing(method_sym, *arguments, &block)
|
|
108
|
+
# the first argument is a Symbol, so you need to_s it if you want to pattern match
|
|
109
|
+
if (res = check_method(@ops,"",method_sym,true,arguments,block))==nil
|
|
110
|
+
super
|
|
111
|
+
else
|
|
112
|
+
res
|
|
113
|
+
end
|
|
114
|
+
end
|
|
115
|
+
def respond_to? method_sym
|
|
116
|
+
if check_method(@ops,"",method_sym.to_sym,false,"",nil)
|
|
117
|
+
return true
|
|
118
|
+
end
|
|
119
|
+
super
|
|
120
|
+
end
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
class Client < RestRessource
|
|
124
|
+
attr_reader :config,:access_token,:ops
|
|
125
|
+
|
|
126
|
+
def initialize params
|
|
127
|
+
@config=params
|
|
128
|
+
@ops={:get=>[:bidding_rules,:iab_categories]}
|
|
129
|
+
|
|
130
|
+
consumer = OAuth::Consumer.new(
|
|
131
|
+
params[:consumer_key],params[:consumer_secret],
|
|
132
|
+
:site => "https://#{ADS_API_ENDPOINT}")
|
|
133
|
+
consumer.http.use_ssl = true
|
|
134
|
+
consumer.http.set_debug_output(STDERR)
|
|
135
|
+
consumer.http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
|
136
|
+
@client=self # To manage rest resource
|
|
137
|
+
@access_token = OAuth::AccessToken.new(consumer, params[:access_token],params[:access_secret])
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
# return the list of available accounts
|
|
141
|
+
def accounts
|
|
142
|
+
if !@cached_accounts
|
|
143
|
+
@cached_accounts = get("accounts").map{|account| Account.new(self,account)}
|
|
144
|
+
end
|
|
145
|
+
@cached_accounts
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
# Create an account based on his id
|
|
149
|
+
def account account_id
|
|
150
|
+
TwitterAds::Account.new(self,{"id"=>account_id})
|
|
151
|
+
end
|
|
152
|
+
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
class Account < RestRessource
|
|
156
|
+
|
|
157
|
+
attr_reader :id,:name
|
|
158
|
+
def initialize client,account
|
|
159
|
+
@client = client
|
|
160
|
+
@account = account
|
|
161
|
+
init
|
|
162
|
+
@prefix = "accounts/#{@id}/"
|
|
163
|
+
@ops ={:get =>[:promoted_accounts,:promoted_tweets,:tailored_audience_changes,
|
|
164
|
+
:targeting_criteria,:app_lists,:campaigns,:funding_instruments,
|
|
165
|
+
:line_items,:promoted_accounts,:promotable_users,:reach_estimate,
|
|
166
|
+
:targeting_suggestions],
|
|
167
|
+
:post =>[:tailored_audiences,:tailored_audience_changes,:campaigns],
|
|
168
|
+
:put =>[:campaigns,:promoted_tweets,:targeting_criteria,"tailored_audiences__global_opt_out"],
|
|
169
|
+
:delete=>[:tailored_audiences,:campaigns,:promoted_tweets,:targeting_criteria]
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
end
|
|
173
|
+
def init
|
|
174
|
+
@id = @account["id"]
|
|
175
|
+
@name = @account["name"]
|
|
176
|
+
end
|
|
177
|
+
|
|
178
|
+
def as_json
|
|
179
|
+
return @account
|
|
180
|
+
end
|
|
181
|
+
|
|
182
|
+
def refresh
|
|
183
|
+
@account = get("")
|
|
184
|
+
init
|
|
185
|
+
end
|
|
186
|
+
|
|
187
|
+
end
|
|
188
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: twitter_ads
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.1
|
|
5
|
+
prerelease:
|
|
6
|
+
platform: ruby
|
|
7
|
+
authors:
|
|
8
|
+
- Thomas Landspurg
|
|
9
|
+
autorequire:
|
|
10
|
+
bindir: bin
|
|
11
|
+
cert_chain: []
|
|
12
|
+
date: 2015-05-22 00:00:00.000000000 Z
|
|
13
|
+
dependencies: []
|
|
14
|
+
description: ! ' '
|
|
15
|
+
email: thomas@seevibes.com
|
|
16
|
+
executables: []
|
|
17
|
+
extensions: []
|
|
18
|
+
extra_rdoc_files: []
|
|
19
|
+
files:
|
|
20
|
+
- lib/twitter_ads.rb
|
|
21
|
+
homepage: http://rubygems.org/gems/twitter_ads
|
|
22
|
+
licenses:
|
|
23
|
+
- MIT
|
|
24
|
+
post_install_message:
|
|
25
|
+
rdoc_options: []
|
|
26
|
+
require_paths:
|
|
27
|
+
- lib
|
|
28
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
29
|
+
none: false
|
|
30
|
+
requirements:
|
|
31
|
+
- - ! '>='
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: '0'
|
|
34
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
35
|
+
none: false
|
|
36
|
+
requirements:
|
|
37
|
+
- - ! '>='
|
|
38
|
+
- !ruby/object:Gem::Version
|
|
39
|
+
version: '0'
|
|
40
|
+
requirements: []
|
|
41
|
+
rubyforge_project:
|
|
42
|
+
rubygems_version: 1.8.25
|
|
43
|
+
signing_key:
|
|
44
|
+
specification_version: 3
|
|
45
|
+
summary: Simplify access to TwitterAds API
|
|
46
|
+
test_files: []
|
|
47
|
+
has_rdoc:
|