Class: Ably::Models::TokenRequest

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

Overview

Contains the properties of a request for a token to Ably. Tokens are generated using Auth#requestToken.

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

Attributes included from Ably::Modules::ModelCommon

#hash

Instance Method Summary collapse

Methods included from Ably::Modules::ModelCommon

#==, #[], #as_json, included, #to_json, #to_s

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



36
37
38
39
40
41
42
# File 'lib/submodules/ably-ruby/lib/ably/models/token_request.rb', line 36

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

Instance Attribute Details

#attributesHash (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



162
163
164
# File 'lib/submodules/ably-ruby/lib/ably/models/token_request.rb', line 162

def attributes
  @hash_object
end

#capabilityHash (readonly)

Capability of the requested Ably Token. If the Ably TokenRequest is successful, the capability of the returned Ably Token will be the intersection of this capability with the capability of the issuing key. The capabilities value is a JSON-encoded representation of the resource paths and associated operations. Read more about capabilities in the capabilities docs.

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.



84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/submodules/ably-ruby/lib/ably/models/token_request.rb', line 84

def capability
  capability_val = attributes.fetch(:capability) { raise Ably::Exceptions::InvalidTokenRequest, 'Capability is missing' }

  case capability_val
  when Hash
    capability_val
  when Ably::Models::IdiomaticRubyWrapper
    capability_val.as_json
  else
    JSON.parse(attributes.fetch(:capability))
  end
end

#client_idString (readonly)

The client ID to associate with the requested Ably Token. When provided, the Ably Token may only be used to perform operations on behalf of that client ID.

Returns:

  • (String)

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



106
107
108
# File 'lib/submodules/ably-ruby/lib/ably/models/token_request.rb', line 106

def client_id
  attributes[:client_id]
end

#key_nameString (readonly)

The name of the key against which this request is made. The key name is public, whereas the key secret is private.

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 :



52
53
54
# File 'lib/submodules/ably-ruby/lib/ably/models/token_request.rb', line 52

def key_name
  attributes.fetch(:key_name) { raise Ably::Exceptions::InvalidTokenRequest, 'Key name is missing' }
end

#macString (readonly)

The Message Authentication Code for this request.

Returns:

  • (String)

    the Message Authentication Code for this request. See the



146
147
148
# File 'lib/submodules/ably-ruby/lib/ably/models/token_request.rb', line 146

def mac
  attributes.fetch(:mac) { raise Ably::Exceptions::InvalidTokenRequest, 'MAC is missing' }
end

#nonceString (readonly)

A cryptographically secure random string of at least 16 characters, used to ensure the TokenRequest cannot be reused.

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.



134
135
136
# File 'lib/submodules/ably-ruby/lib/ably/models/token_request.rb', line 134

def nonce
  attributes.fetch(:nonce) { raise Ably::Exceptions::InvalidTokenRequest, 'Nonce is missing' }
end

#timestampTime (readonly)

The timestamp of this request as milliseconds since the Unix epoch.

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.



120
121
122
123
# File 'lib/submodules/ably-ruby/lib/ably/models/token_request.rb', line 120

def timestamp
  timestamp_val = attributes.fetch(:timestamp) { raise Ably::Exceptions::InvalidTokenRequest, 'Timestamp is missing' }
  as_time_from_epoch(timestamp_val, granularity: :ms)
end

#ttlInteger (readonly)

Requested time to live for the Ably Token in milliseconds. If the Ably TokenRequest is successful, the TTL of the returned Ably Token is less than or equal to this value, depending on application settings and the attributes of the issuing key. The default is 60 minutes.

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.



68
69
70
# File 'lib/submodules/ably-ruby/lib/ably/models/token_request.rb', line 68

def ttl
  attributes.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



154
155
156
# File 'lib/submodules/ably-ruby/lib/ably/models/token_request.rb', line 154

def persisted
  attributes[:persisted]
end