Class: Ably::Models::TokenDetails

Inherits:
Object
  • Object
show all
Includes:
Ably::Modules::ModelCommon
Defined in:
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

15

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



37
38
39
40
41
42
43
44
45
# File 'lib/ably/models/token_details.rb', line 37

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

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

  hash.freeze
end

Instance Attribute Details

#capabilityHash (readonly)

Returns Capabilities assigned to this token.

Returns:

  • (Hash)

    Capabilities assigned to this token



73
74
75
# File 'lib/ably/models/token_details.rb', line 73

def capability
  JSON.parse(hash.fetch(:capability)) if hash.has_key?(:capability)
end

#client_idString (readonly)

Returns Optional client ID assigned to this token.

Returns:

  • (String)

    Optional client ID assigned to this token



79
80
81
# File 'lib/ably/models/token_details.rb', line 79

def client_id
  hash[:client_id]
end

#expiresTime (readonly)

Returns Time the token expires.

Returns:

  • (Time)

    Time the token expires



67
68
69
# File 'lib/ably/models/token_details.rb', line 67

def expires
  as_time_from_epoch(hash[:expires], granularity: :ms, allow_nil: :true)
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



101
102
103
# File 'lib/ably/models/token_details.rb', line 101

def hash
  @hash_object
end

#issuedTime (readonly)

Returns Time the token was issued.

Returns:

  • (Time)

    Time the token was issued



61
62
63
# File 'lib/ably/models/token_details.rb', line 61

def issued
  as_time_from_epoch(hash[:issued], granularity: :ms, allow_nil: :true)
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 :



55
56
57
# File 'lib/ably/models/token_details.rb', line 55

def key_name
  hash[:key_name]
end

#tokenString (readonly)

Returns Token used to authenticate requests.

Returns:

  • (String)

    Token used to authenticate requests



49
50
51
# File 'lib/ably/models/token_details.rb', line 49

def token
  hash[:token]
end

Instance Method Details

#expired?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

Returns:

  • (Boolean)


87
88
89
90
# File 'lib/ably/models/token_details.rb', line 87

def expired?
  return false if !expires
  expires < Time.now + TOKEN_EXPIRY_BUFFER
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)


95
96
97
# File 'lib/ably/models/token_details.rb', line 95

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

#to_sObject



105
106
107
# File 'lib/ably/models/token_details.rb', line 105

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