Class: Redd::OAuth2Access

Inherits:
Object
  • Object
show all
Defined in:
lib/redd/oauth2_access.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response) ⇒ OAuth2Access

Returns a new instance of OAuth2Access.



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/redd/oauth2_access.rb', line 15

def initialize(response)
  @access_token = response[:access_token]
  @refresh_token = response[:refresh_token]
  @scope = response[:scope].split(",").map { |s| s.to_sym }
  @duration = @refresh_token ? :permanent : :temporary
  @expires_at =
    if response[:expires_at]
      Time.at(response[:expires_at])
    else	            
      Time.now + response[:expires_in]
    end
end

Instance Attribute Details

#access_tokenObject (readonly)

Returns the value of attribute access_token.



5
6
7
# File 'lib/redd/oauth2_access.rb', line 5

def access_token
  @access_token
end

#durationObject (readonly)

Returns the value of attribute duration.



11
12
13
# File 'lib/redd/oauth2_access.rb', line 11

def duration
  @duration
end

#expires_atObject (readonly)

Returns the value of attribute expires_at.



13
14
15
# File 'lib/redd/oauth2_access.rb', line 13

def expires_at
  @expires_at
end

#refresh_tokenObject (readonly)

Returns the value of attribute refresh_token.



7
8
9
# File 'lib/redd/oauth2_access.rb', line 7

def refresh_token
  @refresh_token
end

#scopeObject (readonly)

Returns the value of attribute scope.



9
10
11
# File 'lib/redd/oauth2_access.rb', line 9

def scope
  @scope
end

Class Method Details

.from_json(json) ⇒ Object



47
48
49
50
# File 'lib/redd/oauth2_access.rb', line 47

def self.from_json(json)
  hash = MultiJson.load(json, symbolize_keys: true)
  new(hash)
end

Instance Method Details

#expired?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/redd/oauth2_access.rb', line 34

def expired?
  Time.now > @expires_at
end

#refresh(response) ⇒ Object



28
29
30
31
32
# File 'lib/redd/oauth2_access.rb', line 28

def refresh(response)
  @access_token = response[:access_token]
  @expires_at = Time.now + response[:expires_in]
  self
end

#to_jsonObject



38
39
40
41
42
43
44
45
# File 'lib/redd/oauth2_access.rb', line 38

def to_json
  MultiJson.dump(
    access_token: @access_token,
    refresh_token: @refresh_token,
    scope: @scope.join(","),
    expires_at: @expires_at.to_i
  )
end