class TubemogulApi::Connection

Constants

TUBEMOGUL_API_URL

Attributes

config[R]

Public Class Methods

new(config = {}) click to toggle source
# File lib/tubemogul_api/connection.rb, line 8
def initialize(config = {})
  @config = config
end

Public Instance Methods

connection() click to toggle source
# File lib/tubemogul_api/connection.rb, line 12
def connection
  @connection ||= connection_builder do |conn|
    conn.authorization 'Bearer', token
  end
end
get(uri_suffix, params = {}) click to toggle source
# File lib/tubemogul_api/connection.rb, line 18
def get(uri_suffix, params = {})
  connection.get(uri_suffix, params)
end
post(uri_suffix, params = {}) click to toggle source
# File lib/tubemogul_api/connection.rb, line 22
def post(uri_suffix, params = {})
  connection.post(uri_suffix, params)
end

Private Instance Methods

client_id() click to toggle source
# File lib/tubemogul_api/connection.rb, line 32
def client_id
  config.fetch(:client_id, ENV['TUBEMOGUL_CLIENT_ID'])
end
connection_builder() { |conn| ... } click to toggle source
# File lib/tubemogul_api/connection.rb, line 56
def connection_builder
  Faraday.new(url) do |conn|
    conn.request :json
    conn.response :json, content_type: /\bjson$/
    conn.use TubemogulApi::Faraday::Response::RaiseHttpError
    conn.adapter Faraday.default_adapter
    yield conn
  end
end
fetch_authorization_token() click to toggle source
# File lib/tubemogul_api/connection.rb, line 44
def fetch_authorization_token
  connection = connection_builder do |conn|
    conn.basic_auth client_id, secret_key
    conn.headers['Content-Type'] = 'application/x-www-form-urlencoded'
    conn.headers['Cache-Control'] = 'no-cache'
  end

  response = connection.run_request(:post, 'oauth/token', 'grant_type=client_credentials', nil)

  response.body['token']
end
secret_key() click to toggle source
# File lib/tubemogul_api/connection.rb, line 36
def secret_key
  config.fetch(:secret_key, ENV['TUBEMOGUL_SECRET_KEY'])
end
token() click to toggle source
# File lib/tubemogul_api/connection.rb, line 40
def token
  @token ||= fetch_authorization_token
end
url() click to toggle source
# File lib/tubemogul_api/connection.rb, line 28
def url
  config.fetch(:url, TUBEMOGUL_API_URL)
end