Class: Garage::Strategy::AccessToken

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs) ⇒ AccessToken

Returns a new instance of AccessToken.



7
8
9
10
11
12
# File 'lib/garage/strategy/access_token.rb', line 7

def initialize(attrs)
  @raw_response = attrs
  @scope, @token, @token_type = attrs[:scope], attrs[:token], attrs[:token_type]
  @application_id, @resource_owner_id = attrs[:application_id], attrs[:resource_owner_id]
  @expired_at, @revoked_at = attrs[:expired_at], attrs[:revoked_at]
end

Instance Attribute Details

#raw_responseObject (readonly)

Returns the value of attribute raw_response.



5
6
7
# File 'lib/garage/strategy/access_token.rb', line 5

def raw_response
  @raw_response
end

#scopeObject (readonly)

Returns the value of attribute scope.



5
6
7
# File 'lib/garage/strategy/access_token.rb', line 5

def scope
  @scope
end

#tokenObject

Returns the value of attribute token.



4
5
6
# File 'lib/garage/strategy/access_token.rb', line 4

def token
  @token
end

#token_typeObject (readonly)

Returns the value of attribute token_type.



5
6
7
# File 'lib/garage/strategy/access_token.rb', line 5

def token_type
  @token_type
end

Instance Method Details

#acceptable?(required_scopes) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/garage/strategy/access_token.rb', line 38

def acceptable?(required_scopes)
  accessible? && includes_scope?(required_scopes)
end

#accessible?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/garage/strategy/access_token.rb', line 42

def accessible?
  !expired? && !revoked?
end

#application_idObject



14
15
16
# File 'lib/garage/strategy/access_token.rb', line 14

def application_id
  @application_id.try(:to_i)
end

#expired?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/garage/strategy/access_token.rb', line 50

def expired?
  !!expired_at.try(:past?)
end

#expired_atObject



22
23
24
25
26
# File 'lib/garage/strategy/access_token.rb', line 22

def expired_at
  @expired_at.present? ? Time.zone.parse(@expired_at) : nil
rescue ArgumentError, TypeError
  nil
end

#includes_scope?(required_scopes) ⇒ Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/garage/strategy/access_token.rb', line 54

def includes_scope?(required_scopes)
  required_scopes.blank? || required_scopes.any? { |s| scopes.exists?(s) }
end

#resource_owner_idObject



18
19
20
# File 'lib/garage/strategy/access_token.rb', line 18

def resource_owner_id
  @resource_owner_id.try(:to_i)
end

#revoked?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/garage/strategy/access_token.rb', line 46

def revoked?
  !!revoked_at.try(:past?)
end

#revoked_atObject



28
29
30
31
32
# File 'lib/garage/strategy/access_token.rb', line 28

def revoked_at
  @revoked_at.present? ? Time.zone.parse(@revoked_at) : nil
rescue ArgumentError, TypeError
  nil
end

#scopesObject



34
35
36
# File 'lib/garage/strategy/access_token.rb', line 34

def scopes
  scope.try(:split, ' ')
end