Class: RubyPhpipam::Authentication

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_phpipam/authentication.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAuthentication

Returns a new instance of Authentication.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/ruby_phpipam/authentication.rb', line 5

def initialize
  response = HTTParty.post(RubyPhpipam.gen_url("/user/"),
      { basic_auth: { username: RubyPhpipam.configuration.username,
                     password: RubyPhpipam.configuration.password
                    }
      }
    )

  body = JSON.parse(response.body, symbolize_names: true)
  unless body[:message].nil?
    raise AuthenticationFailed, body[:message]
  end

  unless body[:success]
    raise RequestFailed.new(body[:code], body[:message])
  end

  @token = body[:data][:token]
  @expires = Time.strptime(body[:data][:expires], '%Y-%m-%d %H:%M:%S')
end

Instance Attribute Details

#expiresObject (readonly)

Returns the value of attribute expires.



3
4
5
# File 'lib/ruby_phpipam/authentication.rb', line 3

def expires
  @expires
end

#tokenObject (readonly)

Returns the value of attribute token.



3
4
5
# File 'lib/ruby_phpipam/authentication.rb', line 3

def token
  @token
end

Instance Method Details

#validate_token!Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/ruby_phpipam/authentication.rb', line 26

def validate_token!
  if @expires <= Time.now
    header = {
      "token" => @token
    }

    response = HTTParty.get(RubyPhpipam.gen_url("/user/"), headers: header)

    body = JSON.parse(response.body, symbolize_names: true)

    unless body[:success] && body[:code] >= 200 && body[:code] < 400
      raise RequestFailed.new(body[:code], body[:message])
    end


    if body[:data][:expires] <= Time.now
      # Pending re authentication
    else
      @expires = body[:ñdata][:expires]
    end
  end
end