Class: CopyleaksApi::AccessToken

Inherits:
Object
  • Object
show all
Defined in:
lib/copyleaks_api/access_token.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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
  
end

Instance Attribute Details

#created_atObject (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_atObject (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

Returns:

  • (Boolean)


17
18
19
# File 'lib/copyleaks_api/access_token.rb', line 17

def fresh?
  DateTime.now.new_offset(0) < @expire_at
end

#loginObject

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 
  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

#tokenObject

return token string



22
23
24
25
# File 'lib/copyleaks_api/access_token.rb', line 22

def token
  return @token if fresh?
  
end