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

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

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.

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
39
40
# File 'lib/submodules/ably-ruby/lib/ably/models/token_request.rb', line 34

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)



115
116
117
# File 'lib/submodules/ably-ruby/lib/ably/models/token_request.rb', line 115

def attributes
  @hash_object
end

#capabilityHash (readonly)



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/submodules/ably-ruby/lib/ably/models/token_request.rb', line 61

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)



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

def client_id
  attributes[:client_id]
end

#key_nameString (readonly)



44
45
46
# File 'lib/submodules/ably-ruby/lib/ably/models/token_request.rb', line 44

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

#macString (readonly)



102
103
104
# File 'lib/submodules/ably-ruby/lib/ably/models/token_request.rb', line 102

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

#nonceString (readonly)



95
96
97
# File 'lib/submodules/ably-ruby/lib/ably/models/token_request.rb', line 95

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

#timestampTime (readonly)



86
87
88
89
# File 'lib/submodules/ably-ruby/lib/ably/models/token_request.rb', line 86

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)



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

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

API:

  • private



109
110
111
# File 'lib/submodules/ably-ruby/lib/ably/models/token_request.rb', line 109

def persisted
  attributes[:persisted]
end