Class: SnapchatApi::Client
- Inherits:
-
Object
- Object
- SnapchatApi::Client
- Defined in:
- lib/snapchat_api/client.rb
Constant Summary collapse
- ADS_HOST =
"https://adsapi.snapchat.com"- ACCOUNTS_HOST =
"https://accounts.snapchat.com"- ADS_CURRENT_API_PATH =
"v1"- ADS_URL =
"#{ADS_HOST}/#{ADS_CURRENT_API_PATH}"
Instance Attribute Summary collapse
-
#access_token ⇒ Object
Returns the value of attribute access_token.
-
#client_id ⇒ Object
Returns the value of attribute client_id.
-
#client_secret ⇒ Object
Returns the value of attribute client_secret.
-
#debug ⇒ Object
Returns the value of attribute debug.
-
#redirect_uri ⇒ Object
Returns the value of attribute redirect_uri.
-
#refresh_token ⇒ Object
Returns the value of attribute refresh_token.
Instance Method Summary collapse
- #accounts ⇒ Object
- #ad_squads ⇒ Object
- #ads ⇒ Object
- #campaigns ⇒ Object
- #connection ⇒ Object
- #creatives ⇒ Object
- #get_authorization_url(scope: "snapchat-marketing-api") ⇒ Object
- #get_tokens(code:) ⇒ Object
-
#initialize(client_id:, client_secret:, redirect_uri: nil, access_token: nil, refresh_token: nil, debug: false) ⇒ Client
constructor
A new instance of Client.
- #media ⇒ Object
- #refresh_tokens! ⇒ Object
- #request(method, path, params = {}, headers = {}) ⇒ Object
Constructor Details
#initialize(client_id:, client_secret:, redirect_uri: nil, access_token: nil, refresh_token: nil, debug: false) ⇒ Client
Returns a new instance of Client.
17 18 19 20 21 22 23 24 |
# File 'lib/snapchat_api/client.rb', line 17 def initialize(client_id:, client_secret:, redirect_uri: nil, access_token: nil, refresh_token: nil, debug: false) @client_id = client_id @client_secret = client_secret @redirect_uri = redirect_uri @access_token = access_token @refresh_token = refresh_token @debug = debug end |
Instance Attribute Details
#access_token ⇒ Object
Returns the value of attribute access_token.
9 10 11 |
# File 'lib/snapchat_api/client.rb', line 9 def access_token @access_token end |
#client_id ⇒ Object
Returns the value of attribute client_id.
9 10 11 |
# File 'lib/snapchat_api/client.rb', line 9 def client_id @client_id end |
#client_secret ⇒ Object
Returns the value of attribute client_secret.
9 10 11 |
# File 'lib/snapchat_api/client.rb', line 9 def client_secret @client_secret end |
#debug ⇒ Object
Returns the value of attribute debug.
9 10 11 |
# File 'lib/snapchat_api/client.rb', line 9 def debug @debug end |
#redirect_uri ⇒ Object
Returns the value of attribute redirect_uri.
9 10 11 |
# File 'lib/snapchat_api/client.rb', line 9 def redirect_uri @redirect_uri end |
#refresh_token ⇒ Object
Returns the value of attribute refresh_token.
9 10 11 |
# File 'lib/snapchat_api/client.rb', line 9 def refresh_token @refresh_token end |
Instance Method Details
#accounts ⇒ Object
111 112 113 |
# File 'lib/snapchat_api/client.rb', line 111 def accounts @accounts ||= SnapchatApi::Resources::Account.new(self) end |
#ad_squads ⇒ Object
119 120 121 |
# File 'lib/snapchat_api/client.rb', line 119 def ad_squads @ad_squads ||= SnapchatApi::Resources::AdSquad.new(self) end |
#ads ⇒ Object
131 132 133 |
# File 'lib/snapchat_api/client.rb', line 131 def ads @ads ||= SnapchatApi::Resources::Ad.new(self) end |
#campaigns ⇒ Object
115 116 117 |
# File 'lib/snapchat_api/client.rb', line 115 def campaigns @campaigns ||= SnapchatApi::Resources::Campaign.new(self) end |
#connection ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/snapchat_api/client.rb', line 26 def connection @connection ||= Faraday.new(url: ADS_URL) do |conn| conn.headers["Authorization"] = "Bearer #{@access_token}" conn.headers["Content-Type"] = "application/json" conn..timeout = 60 conn..open_timeout = 30 conn.request :multipart conn.use Faraday::FollowRedirects::Middleware conn.use Faraday::Retry::Middleware, max: 3 conn.response :json conn.response :logger if @debug conn.adapter Faraday.default_adapter end end |
#creatives ⇒ Object
127 128 129 |
# File 'lib/snapchat_api/client.rb', line 127 def creatives @creatives ||= SnapchatApi::Resources::Creative.new(self) end |
#get_authorization_url(scope: "snapchat-marketing-api") ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/snapchat_api/client.rb', line 84 def (scope: "snapchat-marketing-api") params = { client_id: @client_id, redirect_uri: redirect_uri, response_type: "code", scope: scope, state: state } "https://accounts.snapchat.com/accounts/oauth2/auth?#{URI.encode_www_form(params)}" end |
#get_tokens(code:) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/snapchat_api/client.rb', line 96 def get_tokens(code:) response = Faraday.post("#{ACCOUNTS_HOST}/login/oauth2/access_token") do |req| req.headers["Content-Type"] = "application/x-www-form-urlencoded" req.body = URI.encode_www_form({ client_id: @client_id, client_secret: @client_secret, code: code, grant_type: "authorization_code", redirect_uri: redirect_uri }) end handle_response(response) JSON.parse(response.body) end |
#media ⇒ Object
123 124 125 |
# File 'lib/snapchat_api/client.rb', line 123 def media @media ||= SnapchatApi::Resources::Media.new(self) end |
#refresh_tokens! ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/snapchat_api/client.rb', line 67 def refresh_tokens! response = Faraday.post("#{ACCOUNTS_HOST}/login/oauth2/access_token") do |req| req.headers["Content-Type"] = "application/x-www-form-urlencoded" req.body = URI.encode_www_form( client_id: @client_id, client_secret: @client_secret, refresh_token: @refresh_token, grant_type: "refresh_token" ) end handle_response(response) body = JSON.parse(response.body) @access_token = body["access_token"] @refresh_token = body["refresh_token"] end |
#request(method, path, params = {}, headers = {}) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/snapchat_api/client.rb', line 46 def request(method, path, params = {}, headers = {}) response = connection.run_request(method, path, nil, headers) do |req| case method when :get, :delete req.params = params when :post, :put if headers["Content-Type"] == "multipart/form-data" req..timeout = 120 req.body = {} params.each do |key, value| req.body[key.to_sym] = value end else req.body = JSON.generate(params) unless params.empty? end end end handle_response(response) end |