Method: CrAPI::Client#initialize

Defined in:
lib/crapi/client.rb

#initialize(base_uri, opts = {}) ⇒ Client

Returns a new instance of Client.

Options Hash (opts):

  • :insecure (true, false)

    Whether to allow insecure SSH connections (i.e. don't attempt to verify the validity of the given certificate).

  • :proxy_host (String)

    The DNS name or IP address of a proxy server to use when connecting to the target system. Maps to Net::HTTP#new's p_addr.

  • :proxy_port (Number)

    The proxy server port to use when connecting to the target system. Maps to Net::HTTP#new's p_port.

  • :proxy_username (String)

    The username to give if authorization is required to access the specified proxy host. Maps to Net::HTTP#new's p_user.

  • :proxy_password (Number)

    The password to give if authorization is required to access the specified proxy host. Maps to Net::HTTP#new's p_pass.

Raises:



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/crapi/client.rb', line 57

def initialize(base_uri, opts = {})
  @base_uri = case base_uri
              when URI then base_uri
              when String then URI(base_uri)
              else raise CrAPI::ArgumentError, %(Unexpected "base_uri" type: #{base_uri.class})
              end

  @proxy_host = opts[:proxy_host]
  @proxy_port = opts[:proxy_port]
  @proxy_username = opts[:proxy_username]
  @proxy_password = opts[:proxy_password]

  @http = Net::HTTP.new(@base_uri.host, @base_uri.port,
                        @proxy_host, @proxy_port, @proxy_username, @proxy_password)
  @http.use_ssl = (@base_uri.scheme == 'https')
  @http.verify_mode = ::OpenSSL::SSL::VERIFY_NONE if opts[:insecure].present?

  @default_headers = { 'Content-Type': JSON_CONTENT_TYPE }.with_indifferent_access
end