Class: Redd::Models::Access

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

Overview

Note:

This model also supports an additional key, called `:created_at` which is a UNIX time representing the time the access was created. The default value is the time the object was initialized.

Models access_token and related keys.

Instance Attribute Summary collapse

Attributes inherited from Model

#client

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Access

Create a non-lazily initialized Access.

Examples:

access = Redd::Models::Access.new(access_token: ...)

Parameters:

  • attributes (Hash) (defaults to: {})

    the access's attributes


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

def initialize(attributes = {})
  super(nil, attributes)
  @creation_time = Time.now
end

Instance Attribute Details

#access_tokenString (readonly)

Returns the access token.

Returns:

  • (String)

    the access token


35
# File 'lib/redd/models/access.rb', line 35

property :access_token

#created_atTime (readonly)

Returns the time the access was created.

Returns:

  • (Time)

    the time the access was created


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

property :created_at, default: ->() { @creation_time }

#expires_inInteger (readonly)

Returns the number of seconds before the access expires.

Returns:

  • (Integer)

    the number of seconds before the access expires


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

property :expires_in

#refresh_tokenString (readonly)

Returns the (optional) refresh token.

Returns:

  • (String)

    the (optional) refresh token


39
# File 'lib/redd/models/access.rb', line 39

property :refresh_token, :nil

#scopeArray<String> (readonly)

Returns the scopes that the user is allowed to access.

Returns:

  • (Array<String>)

    the scopes that the user is allowed to access


51
# File 'lib/redd/models/access.rb', line 51

property :scope, with: ->(scope) { scope.split(' ') }

Instance Method Details

#expired?(grace_period = 60) ⇒ Boolean

Whether the access has expired.

Parameters:

  • grace_period (Integer) (defaults to: 60)

    the grace period where the model expires early

Returns:

  • (Boolean)

    whether the access has expired


24
25
26
# File 'lib/redd/models/access.rb', line 24

def expired?(grace_period = 60)
  Time.now > read_attribute(:created_at) + read_attribute(:expires_in) - grace_period
end

#permanent?Boolean

Returns whether the access can be refreshed.

Returns:

  • (Boolean)

    whether the access can be refreshed


29
30
31
# File 'lib/redd/models/access.rb', line 29

def permanent?
  read_attribute(:refresh_token).nil?
end