Class: Ably::Models::TokenDetails

Inherits:
Object
  • Object
show all
Includes:
Ably::Modules::ModelCommon
Defined in:
lib/submodules/ably-ruby/lib/ably/models/token_details.rb

Overview

TokenDetails is a class providing details of the token string and the token’s associated metadata, constructed from the response from Ably when request in a token via the REST API.

Ruby Time objects are supported in place of Ably ms since epoch time fields. However, if a numeric is provided it must always be expressed in milliseconds as the Ably API always uses milliseconds for time fields.

Constant Summary collapse

TOKEN_EXPIRY_BUFFER =

Buffer in seconds before a token is considered unusable For example, if buffer is 10s, the token can no longer be used for new requests 9s before it expires

5

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Ably::Modules::ModelCommon

#==, #[], #as_json, #to_json

Methods included from Ably::Modules::MessagePack

#to_msgpack

Constructor Details

#initialize(attributes = {}) ⇒ TokenDetails

Returns a new instance of TokenDetails.

Parameters:

  • attributes (defaults to: {})

Options Hash (attributes):

  • :token (String)

    token used to authenticate requests

  • :key_name (String)

    API key name used to create this token

  • :issued (Time, Integer)

    Time the token was issued as Time or Integer in milliseconds

  • :expires (Time, Integer)

    Time the token expires as Time or Integer in milliseconds

  • :capability (String)

    JSON stringified capabilities assigned to this token

  • :client_id (String)

    client ID assigned to this token



42
43
44
# File 'lib/submodules/ably-ruby/lib/ably/models/token_details.rb', line 42

def initialize(attributes)
  @hash_object = IdiomaticRubyWrapper(attributes.clone.freeze)
end

Instance Attribute Details

#capabilityHash (readonly)

Returns Capabilities assigned to this token.

Returns:

  • (Hash)

    Capabilities assigned to this token



78
79
80
# File 'lib/submodules/ably-ruby/lib/ably/models/token_details.rb', line 78

def capability
  JSON.parse(hash.fetch(:capability))
end

#client_idString (readonly)

Returns Optional client ID assigned to this token.

Returns:

  • (String)

    Optional client ID assigned to this token



84
85
86
# File 'lib/submodules/ably-ruby/lib/ably/models/token_details.rb', line 84

def client_id
  hash[:client_id]
end

#expiresTime (readonly)

Returns Time the token expires.

Returns:

  • (Time)

    Time the token expires



72
73
74
# File 'lib/submodules/ably-ruby/lib/ably/models/token_details.rb', line 72

def expires
  as_time_from_epoch(hash.fetch(:expires), granularity: :ms)
end

#hashHash (readonly)

Returns Access the token details Hash object ruby’fied to use symbolized keys.

Returns:

  • (Hash)

    Access the token details Hash object ruby’fied to use symbolized keys



97
98
99
# File 'lib/submodules/ably-ruby/lib/ably/models/token_details.rb', line 97

def hash
  @hash_object
end

#issuedTime (readonly)

Returns Time the token was issued.

Returns:

  • (Time)

    Time the token was issued



66
67
68
# File 'lib/submodules/ably-ruby/lib/ably/models/token_details.rb', line 66

def issued
  as_time_from_epoch(hash.fetch(:issued), granularity: :ms)
end

#key_nameString (readonly)

Returns API key name used to create this token. An API key is made up of an API key name and secret delimited by a :.

Returns:

  • (String)

    API key name used to create this token. An API key is made up of an API key name and secret delimited by a :



60
61
62
# File 'lib/submodules/ably-ruby/lib/ably/models/token_details.rb', line 60

def key_name
  hash.fetch(:key_name)
end

#tokenString (readonly)

Returns Token used to authenticate requests.

Returns:

  • (String)

    Token used to authenticate requests



54
55
56
# File 'lib/submodules/ably-ruby/lib/ably/models/token_details.rb', line 54

def token
  hash.fetch(:token)
end

Instance Method Details

#expired?Boolean

Returns true if token is expired or about to expire

Returns:

  • (Boolean)


91
92
93
# File 'lib/submodules/ably-ruby/lib/ably/models/token_details.rb', line 91

def expired?
  expires < Time.now + TOKEN_EXPIRY_BUFFER
end