Class: ExactTargetRest::Authorization
- Inherits:
-
Object
- Object
- ExactTargetRest::Authorization
- 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
-
#access_token ⇒ Object
readonly
Returns the value of attribute access_token.
-
#expires_in ⇒ Object
readonly
Returns the value of attribute expires_in.
Instance Method Summary collapse
-
#authorize! ⇒ Object
Execute authorization, keeps an access token and returns the result.
-
#authorized? ⇒ Boolean
Already authorized and NOT expired?.
-
#initialize(client_id, client_secret) ⇒ Authorization
constructor
New authorization (it does not trigger REST yet).
-
#with_authorization {|access_token| ... } ⇒ Object
Guarantee the block to run authorized.
Constructor Details
#initialize(client_id, client_secret) ⇒ Authorization
New authorization (it does not trigger REST yet).
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_token ⇒ Object (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_in ⇒ Object (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 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?
46 47 48 |
# File 'lib/exact_target_rest/authorization.rb', line 46 def @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.
25 26 27 28 |
# File 'lib/exact_target_rest/authorization.rb', line 25 def unless yield @access_token end |