Class: ExactTargetRest::Authorization

Inherits:
Object
  • Object
show all
Defined in:
lib/exact_target_rest/authorization.rb

Overview

An OAUTH2 REST authorization for ExactTarget API.

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client_id, client_secret) ⇒ Authorization

New authorization (it does not trigger REST yet).

Parameters:

  • client_id (String)

    Client ID

  • client_secret (String)

    Client Secret



14
15
16
# File 'lib/exact_target_rest/authorization.rb', line 14

def initialize(client_id, client_secret)
  @client_id, @client_secret = client_id, client_secret
end

Instance Attribute Details

#access_tokenObject (readonly)

Returns the value of attribute access_token.



7
8
9
# File 'lib/exact_target_rest/authorization.rb', line 7

def access_token
  @access_token
end

#expires_inObject (readonly)

Returns the value of attribute expires_in.



8
9
10
# File 'lib/exact_target_rest/authorization.rb', line 8

def expires_in
  @expires_in
end

Instance Method Details

#authorize!Object

Execute authorization, keeps an access token and returns the result



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/exact_target_rest/authorization.rb', line 31

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 = Time.now + resp.body['expiresIn']
    self
  else
    fail NotAuthorizedError
  end
end

#authorized?Boolean

Already authorized and NOT expired?

Returns:

  • (Boolean)


46
47
48
# File 'lib/exact_target_rest/authorization.rb', line 46

def authorized?
  @access_token && @expires_in > Time.now
end

#with_authorization {|access_token| ... } ⇒ Object

Guarantee the block to run authorized.

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

Yields:

Yield Parameters:

  • access_token (String)

    Access token used to authorize a request



25
26
27
28
# File 'lib/exact_target_rest/authorization.rb', line 25

def with_authorization
  authorize! unless authorized?
  yield @access_token
end