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, #inspect, 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

#renewObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/haveapi/client/authentication/token.rb', line 43

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, ret unless ret.ok?

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

#request_headersObject



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

def request_headers
  return {} unless @configured

  check_validity
  @via == :header ? { @desc[:http_header] => @token } : {}
end

#request_query_paramsObject



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

def request_query_params
  return {} unless @configured

  check_validity
  @via == :query_param ? { @desc[:query_parameter] => @token } : {}
end

#revokeObject



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/haveapi/client/authentication/token.rb', line 58

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, ret unless ret.ok?
end

#saveObject



39
40
41
# File 'lib/haveapi/client/authentication/token.rb', line 39

def save
  { token: @token, valid_to: @valid_to && @valid_to.to_i }
end

#setupObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/haveapi/client/authentication/token.rb', line 8

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

  @valid_to =
    case @opts[:valid_to]
    when Time # usually given at runtime
      @opts[:valid_to]
    when Integer # loaded from config
      Time.at(@opts[:valid_to])
    end

  request_token unless @token

  @configured = true
end