Class: CoreLibrary::ProxySettings

Inherits:
Object
  • Object
show all
Defined in:
lib/apimatic-core/http/configurations/proxy_settings.rb

Overview

ProxySettings encapsulates HTTP proxy configuration for Faraday, including optional basic authentication.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(address:, port: nil, username: nil, password: nil) ⇒ ProxySettings

Returns a new instance of ProxySettings.

Parameters:

  • address (String)

    The proxy server address (e.g., ‘localhost’).

  • port (Integer, nil) (defaults to: nil)

    Optional proxy server port (e.g., 8080).

  • username (String, nil) (defaults to: nil)

    Optional proxy auth username.

  • password (String, nil) (defaults to: nil)

    Optional proxy auth password.

Raises:

  • (ArgumentError)

    If address is invalid.



17
18
19
20
21
22
23
24
# File 'lib/apimatic-core/http/configurations/proxy_settings.rb', line 17

def initialize(address:, port: nil, username: nil, password: nil)
  raise ArgumentError, 'Proxy address must be a non-empty string' unless address.is_a?(String) && !address.empty?

  @address = address
  @port = port
  @username = username
  @password = password
end

Instance Attribute Details

#addressObject

Returns the value of attribute address.



7
8
9
# File 'lib/apimatic-core/http/configurations/proxy_settings.rb', line 7

def address
  @address
end

#passwordObject

Returns the value of attribute password.



7
8
9
# File 'lib/apimatic-core/http/configurations/proxy_settings.rb', line 7

def password
  @password
end

#portObject

Returns the value of attribute port.



7
8
9
# File 'lib/apimatic-core/http/configurations/proxy_settings.rb', line 7

def port
  @port
end

#usernameObject

Returns the value of attribute username.



7
8
9
# File 'lib/apimatic-core/http/configurations/proxy_settings.rb', line 7

def username
  @username
end

Instance Method Details

#to_hHash

Converts the proxy settings into a Faraday-compatible hash.

Returns:

  • (Hash)

    A hash with keys :uri, :user, and :password (as applicable).



31
32
33
34
35
36
37
38
39
40
# File 'lib/apimatic-core/http/configurations/proxy_settings.rb', line 31

def to_h
  uri_str = port ? "#{address}:#{port}" : address

  {
    uri: uri_str
  }.tap do |hash|
    hash[:user] = username if username
    hash[:password] = password if password
  end
end