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

Class Method 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

Class Method Details

.from_hash(hash) ⇒ ProxySettings?

Creates a ProxySettings instance from a hash.

Parameters:

  • hash (Hash)

    A hash containing :address, :port, :username, and/or :password.

Returns:

  • (ProxySettings, nil)

    Returns a new instance or nil if hash is nil/empty.



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/apimatic-core/http/configurations/proxy_settings.rb', line 48

def self.from_hash(hash)
  return nil if hash.nil? || hash.empty?

  # Support both symbol and string keys
  address = hash[:address] || hash['address']
  port = hash[:port] || hash['port']
  username = hash[:username] || hash['username']
  password = hash[:password] || hash['password']

  return nil if address.nil? || address.strip.empty?

  new(address: address, port: port, username: username, password: password)
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