Class: CopyleaksApi::AccessToken
- Inherits:
-
Object
- Object
- CopyleaksApi::AccessToken
- Defined in:
- lib/copyleaks_api/access_token.rb
Instance Attribute Summary collapse
-
#created_at ⇒ Object
readonly
Returns the value of attribute created_at.
-
#expire_at ⇒ Object
readonly
Returns the value of attribute expire_at.
Instance Method Summary collapse
-
#fresh? ⇒ Boolean
predicate method to check if token is not expired.
-
#initialize(cloud, email, api_key) ⇒ AccessToken
constructor
constructor.
-
#login ⇒ Object
get token for given email and api_key pair.
-
#token ⇒ Object
return token string.
Constructor Details
#initialize(cloud, email, api_key) ⇒ AccessToken
constructor
9 10 11 12 13 14 |
# File 'lib/copyleaks_api/access_token.rb', line 9 def initialize(cloud, email, api_key) @cloud = cloud @email = email @api_key = api_key login end |
Instance Attribute Details
#created_at ⇒ Object (readonly)
Returns the value of attribute created_at.
6 7 8 |
# File 'lib/copyleaks_api/access_token.rb', line 6 def created_at @created_at end |
#expire_at ⇒ Object (readonly)
Returns the value of attribute expire_at.
6 7 8 |
# File 'lib/copyleaks_api/access_token.rb', line 6 def expire_at @expire_at end |
Instance Method Details
#fresh? ⇒ Boolean
predicate method to check if token is not expired
17 18 19 |
# File 'lib/copyleaks_api/access_token.rb', line 17 def fresh? DateTime.now.new_offset(0) < @expire_at end |
#login ⇒ Object
get token for given email and api_key pair
28 29 30 31 32 33 34 |
# File 'lib/copyleaks_api/access_token.rb', line 28 def login res = @cloud.api.post('account/login-api', { Email: @email, ApiKey: @api_key }.to_json) @token = res['access_token'] @created_at = DateTime.parse(res['.issued']) @expire_at = DateTime.parse(res['.expires']) @token end |
#token ⇒ Object
return token string
22 23 24 25 |
# File 'lib/copyleaks_api/access_token.rb', line 22 def token return @token if fresh? login end |