Class: Ably::Models::TokenRequest

Inherits:
Object
  • Object
show all
Includes:
Ably::Modules::ModelCommon
Defined in:
lib/ably/models/token_request.rb

Overview

TokenRequest is a class that stores the attributes of a token request

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.

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 = {}) ⇒ TokenRequest

Returns a new instance of TokenRequest.

Parameters:

  • attributes (defaults to: {})

Options Hash (attributes):

  • :ttl (Integer)

    requested time to live for the token in milliseconds

  • :timestamp (Time, Integer)

    the timestamp of this request in milliseconds or as a Time

  • :key_name (String)

    API key name of the key against which this request is made

  • :capability (String)

    JSON stringified capability of the token

  • :client_id (String)

    client ID to associate with this token

  • :nonce (String)

    an opaque nonce string of at least 16 characters

  • :mac (String)

    the Message Authentication Code for this request



34
35
36
37
38
# File 'lib/ably/models/token_request.rb', line 34

def initialize(attributes = {})
  @hash_object = IdiomaticRubyWrapper(attributes.clone)
  hash[:timestamp] = (hash[:timestamp].to_f * 1000).round if hash[:timestamp].kind_of?(Time)
  hash.freeze
end

Instance Attribute Details

#capabilityHash (readonly)

Returns capability of the token. If the token request is successful, the capability of the returned token will be the intersection of this capability with the capability of the issuing key.

Returns:

  • (Hash)

    capability of the token. If the token request is successful, the capability of the returned token will be the intersection of this capability with the capability of the issuing key.



59
60
61
# File 'lib/ably/models/token_request.rb', line 59

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

#client_idString (readonly)

Returns the client ID to associate with this token. The generated token may be used to authenticate as this clientId.

Returns:

  • (String)

    the client ID to associate with this token. The generated token may be used to authenticate as this clientId.



66
67
68
# File 'lib/ably/models/token_request.rb', line 66

def client_id
  hash[:client_id]
end

#hashHash (readonly)

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

Returns:

  • (Hash)

    the token request Hash object ruby’fied to use symbolized keys



103
104
105
# File 'lib/ably/models/token_request.rb', line 103

def hash
  @hash_object
end

#key_nameString (readonly)

Returns API key name of the key against which this request is made. An API key is made up of an API key name and secret delimited by a :.

Returns:

  • (String)

    API key name of the key against which this request is made. An API key is made up of an API key name and secret delimited by a :



42
43
44
# File 'lib/ably/models/token_request.rb', line 42

def key_name
  hash.fetch(:key_name)
end

#macString (readonly)

Returns the Message Authentication Code for this request. See the Ably Authentication documentation for more details.

Returns:



90
91
92
# File 'lib/ably/models/token_request.rb', line 90

def mac
  hash.fetch(:mac)
end

#nonceString (readonly)

Returns an opaque nonce string of at least 16 characters to ensure uniqueness of this request. Any subsequent request using the same nonce will be rejected.

Returns:

  • (String)

    an opaque nonce string of at least 16 characters to ensure uniqueness of this request. Any subsequent request using the same nonce will be rejected.



83
84
85
# File 'lib/ably/models/token_request.rb', line 83

def nonce
  hash.fetch(:nonce)
end

#timestampTime (readonly)

Returns the timestamp of this request. Timestamps, in conjunction with the nonce, are used to prevent token requests from being replayed. Timestamp when sent to Ably is in milliseconds.

Returns:

  • (Time)

    the timestamp of this request. Timestamps, in conjunction with the nonce, are used to prevent token requests from being replayed. Timestamp when sent to Ably is in milliseconds.



75
76
77
# File 'lib/ably/models/token_request.rb', line 75

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

#ttlInteger (readonly)

Returns requested time to live for the token in seconds. If the token request is successful, the TTL of the returned token will be less than or equal to this value depending on application settings and the attributes of the issuing key. TTL when sent to Ably is in milliseconds.

Returns:

  • (Integer)

    requested time to live for the token in seconds. If the token request is successful, the TTL of the returned token will be less than or equal to this value depending on application settings and the attributes of the issuing key. TTL when sent to Ably is in milliseconds.



51
52
53
# File 'lib/ably/models/token_request.rb', line 51

def ttl
  hash.fetch(:ttl) / 1000
end

Instance Method Details

#persistedObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Requests that the token is always persisted



97
98
99
# File 'lib/ably/models/token_request.rb', line 97

def persisted
  hash.fetch(:persisted)
end