Class: XClarityClient::XClarityCredentialsValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/xclarity_client/xclarity_credentials_validator.rb

Constant Summary collapse

BASE_URI =
'/sessions'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(conf) ⇒ XClarityCredentialsValidator

Returns a new instance of XClarityCredentialsValidator.



7
8
9
10
# File 'lib/xclarity_client/xclarity_credentials_validator.rb', line 7

def initialize(conf)
  @connection = XClarityClient::Connection.new(conf)
  @configuration = conf
end

Instance Method Details

#build_session(conf) ⇒ Object

Raises:

  • (Faraday::Error::ConnectionFailed)


35
36
37
38
39
40
41
42
# File 'lib/xclarity_client/xclarity_credentials_validator.rb', line 35

def build_session(conf)
  @response = @connection.do_post(BASE_URI, {
    :UserId => conf.username,
    :password => conf.password
  }.to_json)

  raise Faraday::Error::ConnectionFailed unless @response.success?
end

#close_session(id_session) ⇒ Object



44
45
46
# File 'lib/xclarity_client/xclarity_credentials_validator.rb', line 44

def close_session(id_session)
  @connection.do_delete("#{BASE_URI}/#{id_session}")
end

#validateObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/xclarity_client/xclarity_credentials_validator.rb', line 12

def validate
  $lxca_log.info 'XClarityClient::XClarityValidate validate', 'Creating session ...'
  build_session(@configuration)

  id_session = JSON.parse(@response.body)['messages'].first['id']
  $lxca_log.info 'XClarityClient::XClarityValidate validate', 'Session created ...'

  close_session(id_session)
rescue => err
  cause = err.cause.to_s.downcase
  if cause.include?('connection refused')
    raise XClarityClient::Error::ConnectionRefused
  elsif cause.include?('name or service not known')
    raise XClarityClient::Error::HostnameUnknown
  elsif @response.nil?
    raise XClarityClient::Error::ConnectionFailed
  elsif @response.status == 403
    raise XClarityClient::Error::AuthenticationError
  else
    raise XClarityClient::Error::ConnectionFailedUnknown.new("status #{@response.status}")
  end
end