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.



85
86
87
88
# File 'lib/http_configuration.rb', line 85

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.



136
137
138
139
140
# File 'lib/http_configuration.rb', line 136

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

.globalObject



131
132
133
# File 'lib/http_configuration.rb', line 131

def self.global
  @global
end

.no_proxy?(host, options) ⇒ Boolean

Returns:

  • (Boolean)


109
110
111
112
113
114
115
116
117
118
119
# File 'lib/http_configuration.rb', line 109

def self.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



123
124
125
126
127
128
129
# File 'lib/http_configuration.rb', line 123

def self.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.



91
92
93
# File 'lib/http_configuration.rb', line 91

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.



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

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