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

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

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
46
47
# 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|
    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 Attribute Details

#attributesHash (readonly)


113
114
115
# File 'lib/ably/models/token_details.rb', line 113

def attributes
  @hash_object
end

#capabilityHash (readonly)


75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/ably/models/token_details.rb', line 75

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 (readonly)


91
92
93
# File 'lib/ably/models/token_details.rb', line 91

def client_id
  attributes[:client_id]
end

#expiresTime (readonly)


69
70
71
# File 'lib/ably/models/token_details.rb', line 69

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

#issuedTime (readonly)


63
64
65
# File 'lib/ably/models/token_details.rb', line 63

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

#key_nameString (readonly)


57
58
59
# File 'lib/ably/models/token_details.rb', line 57

def key_name
  attributes[:key_name]
end

#tokenString (readonly)


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

def token
  attributes[: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


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

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


107
108
109
# File 'lib/ably/models/token_details.rb', line 107

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

#to_sObject


117
118
119
# File 'lib/ably/models/token_details.rb', line 117

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