Class: Net::HTTP::Configuration

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

Overview

The Configuration class encapsulates a set of HTTP defaults. The configuration can made either global or all requests, or it can be applied only within a block. Configuration blocks can also set an additional set of options which take precedence over the initialization options.

Available options are :proxy_host, :proxy_port, :proxy_user, :proxy_password, :no_proxy, :read_timeout, :open_timeout, and :proxy. This last value can either contain a proxy string or the symbol :none for no proxy or :environment to use the values in the HTTP_PROXY/http_proxy and NO_PROXY/no_proxy environment variables.

If you specify a proxy, but don’t want it to be used for certain hosts, specify the domain names in the :no_proxy option. This can either be an array or a comma delimited string. A request to a host name which ends with any of these values will not be proxied.

The normal functionality for Net::HTTP is still available, so you can set proxies and timeouts manually if needed. Because of the way in which https calls are made, you cannot configure a special proxy just for https calls.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Configuration

Returns a new instance of Configuration.



59
60
61
62
# File 'lib/http_configuration.rb', line 59

def initialize (options = {})
  @default_options = options.dup
  expand_proxy_config!(@default_options)
end

Class Method Details

.currentObject

Get the current configuration that is in scope.



111
112
113
114
115
# File 'lib/http_configuration.rb', line 111

def current
  stack = Thread.current[:net_http_configuration]
  config = stack.last if stack
  config || global
end

.globalObject



106
107
108
# File 'lib/http_configuration.rb', line 106

def global
  @global
end

.no_proxy?(host, options) ⇒ Boolean

Returns:

  • (Boolean)


84
85
86
87
88
89
90
91
92
93
94
# File 'lib/http_configuration.rb', line 84

def no_proxy? (host, options)
  return false unless options[:no_proxy].kind_of?(Array)

  host = host.downcase
  options[:no_proxy].each do |pattern|
    pattern = pattern.downcase
    return true if host[-pattern.length, pattern.length] == pattern
  end

  return false
end

.set_global(options) ⇒ Object

Set the options for a global configuration used for all HTTP requests. The global configuration can be cleared by setting nil



98
99
100
101
102
103
104
# File 'lib/http_configuration.rb', line 98

def set_global (options)
  if options
    @global = Configuration.new(options)
  else
    @global = nil
  end
end

Instance Method Details

#[](name) ⇒ Object

Get the specified option for the configuration.



65
66
67
# File 'lib/http_configuration.rb', line 65

def [] (name)
  @default_options[name]
end

#apply(options = {}) ⇒ Object

Apply the configuration to the block. If any options are provided, they will override the default options for the configuration.



71
72
73
74
75
76
77
78
79
80
81
# File 'lib/http_configuration.rb', line 71

def apply (options = {})
  options = @default_options.merge(options)
  expand_proxy_config!(options)
  Thread.current[:net_http_configuration] ||= []
  Thread.current[:net_http_configuration].push(options)
  begin
    return yield
  ensure
    Thread.current[:net_http_configuration].pop
  end
end