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

Contains an Ably Token and its associated metadata.

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

15

Instance Attribute Summary

Attributes included from Ably::Modules::ModelCommon

#hash

Instance Method Summary collapse

Methods included from Ably::Modules::ModelCommon

#==, #[], #as_json, included, #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



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/submodules/ably-ruby/lib/ably/models/token_details.rb', line 34

def initialize(attributes = {})
  @hash_object = IdiomaticRubyWrapper(attributes.clone)

  %w(issued expires).map(&:to_sym).each do |time_attribute|
    if self.attributes[time_attribute].kind_of?(Time)
      self.attributes[time_attribute] = (self.attributes[time_attribute].to_f * 1000).round
    end
  end

  self.attributes.freeze
end

Instance Method Details

#attributesHash

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



126
127
128
# File 'lib/submodules/ably-ruby/lib/ably/models/token_details.rb', line 126

def attributes
  @hash_object
end

#capabilityHash

The capabilities associated with this Ably Token. 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)

    Capabilities assigned to this token



80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/submodules/ably-ruby/lib/ably/models/token_details.rb', line 80

def capability
  if attributes.has_key?(:capability)
    capability_val = attributes.fetch(:capability)
    case capability_val
    when Hash
      capability_val
    when Ably::Models::IdiomaticRubyWrapper
      capability_val.as_json
    else
      JSON.parse(attributes.fetch(:capability))
    end
  end
end

#client_idString

The client ID, if any, bound to this Ably Token. If a client ID is included, then the Ably Token authenticates its bearer as that client ID, and the Ably Token may only be used to perform operations on behalf of that client ID. The client is then considered to be an identified client.

Returns:

  • (String)

    Optional client ID assigned to this token



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

def client_id
  attributes[:client_id]
end

#expired?(attributes = {}) ⇒ Boolean

Returns true if token is expired or about to expire For tokens that have not got an explicit expires attribute expired? will always return true

Parameters:

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

Options Hash (attributes):

  • :from (Time)

    Sets a current time from which token expires

Returns:

  • (Boolean)


110
111
112
113
114
115
# File 'lib/submodules/ably-ruby/lib/ably/models/token_details.rb', line 110

def expired?(attributes = {})
  return false if !expires

  from = attributes[:from] || Time.now
  expires < from + TOKEN_EXPIRY_BUFFER
end

#expiresTime

The timestamp at which this token expires as milliseconds since the Unix epoch.

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(attributes[:expires], granularity: :ms, allow_nil: :true)
end

#from_token_string?Boolean

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.

True if the TokenDetails was created from an opaque string i.e. no metadata exists for this token

Returns:

  • (Boolean)


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

def from_token_string?
  attributes.keys == [:token]
end

#issuedTime

The timestamp at which this token was issued as milliseconds since the Unix epoch.

Returns:

  • (Time)

    Time the token was issued



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

def issued
  as_time_from_epoch(attributes[:issued], granularity: :ms, allow_nil: :true)
end

#key_nameString

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 :



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

def key_name
  attributes[:key_name]
end

#to_sObject



130
131
132
# File 'lib/submodules/ably-ruby/lib/ably/models/token_details.rb', line 130

def to_s
  "<TokenDetails token=#{token} client_id=#{client_id} key_name=#{key_name} issued=#{issued} expires=#{expires} capability=#{capability} expired?=#{expired?}>"
end

#tokenString

The Ably Token itself. A typical Ably Token string appears with the form xVLyHw.A-pwh7wicf3afTfgiw4k2Ku33kcnSA7z6y8FjuYpe3QaNRTEo4.

Returns:

  • (String)

    Token used to authenticate requests



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

def token
  attributes[:token]
end