Class: Nexmo::JWT

Inherits:
Object
  • Object
show all
Defined in:
lib/nexmo-jwt/jwt.rb,
lib/nexmo-jwt/version.rb

Constant Summary collapse

VERSION =
'0.1.2'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ JWT

Returns a new instance of JWT.



7
8
9
10
11
# File 'lib/nexmo-jwt/jwt.rb', line 7

def initialize(params = {})
  @generator = params.fetch(:generator)
  @typ = params.fetch(:typ, 'JWT')
  @iat = params.fetch(:iat, Time.now.to_i)
end

Instance Attribute Details

#generatorObject

Returns the value of attribute generator.



5
6
7
# File 'lib/nexmo-jwt/jwt.rb', line 5

def generator
  @generator
end

#iatObject

Returns the value of attribute iat.



5
6
7
# File 'lib/nexmo-jwt/jwt.rb', line 5

def iat
  @iat
end

#typObject

Returns the value of attribute typ.



5
6
7
# File 'lib/nexmo-jwt/jwt.rb', line 5

def typ
  @typ
end

Instance Method Details

#generateObject



13
14
15
# File 'lib/nexmo-jwt/jwt.rb', line 13

def generate
  ::JWT.encode(to_payload, generator.private_key, generator.alg)
end

#to_payloadObject



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/nexmo-jwt/jwt.rb', line 17

def to_payload
  hash = {
    iat: iat,
    jti: generator.jti,
    exp: generator.exp || iat + generator.ttl,
    sub: generator.subject,
    application_id: generator.application_id,
    typ: typ
  }
  hash.merge!(generator.paths) if generator.paths
  hash.merge!(nbf: generator.nbf) if generator.nbf
  hash
end