class ExactTargetRest::Authorization

An OAUTH2 REST authorization for ExactTarget API.

You can create “Client ID” and “Client Secret” in ExactTarget App Center (appcenter-auth.exacttargetapps.com).

Attributes

access_token[R]
expires_at[R]
expires_in[R]

Public Class Methods

new(client_id, client_secret) click to toggle source

New authorization (it does not trigger REST yet).

@param client_id [String] Client ID @param client_secret [String] Client Secret

# File lib/exact_target_rest/authorization.rb, line 15
def initialize(client_id, client_secret)
  @client_id, @client_secret = client_id, client_secret
end

Public Instance Methods

authorize!() click to toggle source

Execute authorization, keeps an access token and returns the result

# File lib/exact_target_rest/authorization.rb, line 32
def authorize!
  resp = endpoint.post do |p|
    p.body = {clientId: @client_id,
              clientSecret: @client_secret}
  end
  if resp.success?
    @access_token = resp.body['accessToken']
    @expires_in = resp.body['expiresIn']
    @expires_at = Time.now + @expires_in
    self
  else
    fail NotAuthorizedError
  end
end
authorized?() click to toggle source

Already authorized and NOT expired?

# File lib/exact_target_rest/authorization.rb, line 48
def authorized?
  @access_token && @expires_at > Time.now
end
with_authorization() { |access_token| ... } click to toggle source

Guarantee the block to run authorized.

If not yet authorized, it runs authorization. If authorization is expired, it renews it.

@yield [access_token] Block to be executed @yieldparam access_token [String] Access token used to authorize a request

# File lib/exact_target_rest/authorization.rb, line 26
def with_authorization
  authorize! unless authorized?
  yield @access_token
end

Protected Instance Methods

endpoint() click to toggle source
# File lib/exact_target_rest/authorization.rb, line 54
def endpoint
  Faraday.new(url: AUTH_URL) do |f|
    f.request :json
    f.response :json, content_type: /\bjson$/
    f.adapter FARADAY_ADAPTER
  end
end