Class: Redd::Access

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

Overview

A container for the client’s access to their account via OAuth2

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(body) ⇒ Access

Returns a new instance of Access.

Parameters:

  • body (String)

    The response body containing the required info.



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/redd/access.rb', line 23

def initialize(body)
  @access_token = body[:access_token]
  @refresh_token = body[:refresh_token]
  @scope = (body[:scope] ? body[:scope].split(',') : [])
  @expires_at =
    if body[:expires_at]
      Time.at(body[:expires_at])
    else
      Time.now + (body[:expires_in] || 0)
    end
end

Instance Attribute Details

#access_tokenString (readonly)

Returns The access token used to access the users account.

Returns:

  • (String)

    The access token used to access the users account.



8
9
10
# File 'lib/redd/access.rb', line 8

def access_token
  @access_token
end

#expires_atTime (readonly)

Returns The time when the access token expires.

Returns:

  • (Time)

    The time when the access token expires.



20
21
22
# File 'lib/redd/access.rb', line 20

def expires_at
  @expires_at
end

#refresh_tokenString? (readonly)

Returns The refresh token, if the access was permanent.

Returns:

  • (String, nil)

    The refresh token, if the access was permanent.



12
13
14
# File 'lib/redd/access.rb', line 12

def refresh_token
  @refresh_token
end

#scopeArray (readonly)

Returns The scope that the client is allowed to access.

Returns:

  • (Array)

    The scope that the client is allowed to access.



16
17
18
# File 'lib/redd/access.rb', line 16

def scope
  @scope
end

Class Method Details

.from_json(json) ⇒ Access

Create a new instance of the class from the JSON returned from #to_json

Parameters:

  • json (String)

Returns:



71
72
73
74
# File 'lib/redd/access.rb', line 71

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

Instance Method Details

#expired?Boolean

Returns Whether the access has expired.

Returns:

  • (Boolean)

    Whether the access has expired.



46
47
48
# File 'lib/redd/access.rb', line 46

def expired?
  Time.now > (@expires_at + 60)
end

#permanent?Boolean

Returns Whether the access is permanent.

Returns:

  • (Boolean)

    Whether the access is permanent.



41
42
43
# File 'lib/redd/access.rb', line 41

def permanent?
  !temporary?
end

#refreshed!(body) ⇒ Object

Refresh the object with the new response body. This happens when a new access token is requested using a request token.

Parameters:

  • body (Hash)

    The new response body.



53
54
55
56
# File 'lib/redd/access.rb', line 53

def refreshed!(body)
  @access_token = body[:access_token]
  @expires_at = Time.now + body[:expires_in]
end

#temporary?Boolean

Returns Whether the access is temporary.

Returns:

  • (Boolean)

    Whether the access is temporary.



36
37
38
# File 'lib/redd/access.rb', line 36

def temporary?
  !refresh_token
end

#to_jsonString

Returns A JSON version of the data that can be loaded later.

Returns:

  • (String)

    A JSON version of the data that can be loaded later.



59
60
61
62
63
64
65
66
# File 'lib/redd/access.rb', line 59

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