Class: HaveAPI::Client::Authentication::Token

Inherits:
Base
  • Object
show all
Defined in:
lib/haveapi/client/authentication/token.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#initialize, register, #request_payload, #resource

Constructor Details

This class inherits a constructor from HaveAPI::Client::Authentication::Base

Instance Attribute Details

#tokenObject (readonly)

Returns the value of attribute token.



6
7
8
# File 'lib/haveapi/client/authentication/token.rb', line 6

def token
  @token
end

#valid_toObject (readonly)

Returns the value of attribute valid_to.



6
7
8
# File 'lib/haveapi/client/authentication/token.rb', line 6

def valid_to
  @valid_to
end

Instance Method Details

#load(hash) ⇒ Object



34
35
36
37
# File 'lib/haveapi/client/authentication/token.rb', line 34

def load(hash)
  @token = hash[:token]
  @valid_to = hash[:valid_to]
end

#renewObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/haveapi/client/authentication/token.rb', line 39

def renew
  a = HaveAPI::Client::Action.new(
      nil,
      @communicator,
      :renew,
      @desc[:resources][:token][:actions][:renew],
      []
  )
  ret = HaveAPI::Client::Response.new(a, a.execute({}))
  raise HaveAPI::Client::ActionFailed.new(ret) unless ret.ok?

  @valid_to = ret[:valid_to]
  @valid_to = @valid_to && DateTime.iso8601(@valid_to).to_time
end

#request_headersObject



24
25
26
27
28
# File 'lib/haveapi/client/authentication/token.rb', line 24

def request_headers
  return {} unless @configured
  check_validity
  @via == :header ? {@desc[:http_header] => @token} : {}
end

#request_query_paramsObject



18
19
20
21
22
# File 'lib/haveapi/client/authentication/token.rb', line 18

def request_query_params
  return {} unless @configured
  check_validity
  @via == :query_param ? {@desc[:query_parameter] => @token} : {}
end

#revokeObject



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/haveapi/client/authentication/token.rb', line 54

def revoke
  a = HaveAPI::Client::Action.new(
      nil,
      @communicator,
      :revoke,
      @desc[:resources][:token][:actions][:revoke],
      []
  )
  ret = HaveAPI::Client::Response.new(a, a.execute({}))
  raise HaveAPI::Client::ActionFailed.new(ret) unless ret.ok?
end

#saveObject



30
31
32
# File 'lib/haveapi/client/authentication/token.rb', line 30

def save
  {token: @token, valid_to: @valid_to}
end

#setupObject



8
9
10
11
12
13
14
15
16
# File 'lib/haveapi/client/authentication/token.rb', line 8

def setup
  @via = @opts[:via] || :header
  @token = @opts[:token]
  @valid_to = @opts[:valid_to]

  request_token unless @token

  @configured = true
end