Module: Net::HTTP::Local

Defined in:
lib/net/http/local.rb,
lib/net/http/local/version.rb

Overview

Net::HTTP::Local binds a Net::HTTP request to a specified local address and port.

Constant Summary collapse

VERSION =
'0.1.2'

Instance Method Summary collapse

Instance Method Details

#bind(local_address, local_port = nil) ⇒ Object

Binds to a local IP address.

local_address - A local IP address or hostname. local_port - A local port (default: nil).

If given a block, unbinds after the block executes.

Returns nothing.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/net/http/local.rb', line 14

def bind(local_address, local_port = nil)
  (class << TCPSocket; self; end).instance_eval do
    alias_method :open_with_local, :open
    define_method(:open) do |conn_address, conn_port|
      open_with_local conn_address, conn_port, local_address, local_port
    end
  end

  if block_given?
    begin
      yield
    ensure
      unbind
    end
  end
end

#unbindObject

Unbinds from the local address and port.

Returns nothing.



34
35
36
37
38
39
# File 'lib/net/http/local.rb', line 34

def unbind
  (class << TCPSocket; self; end).instance_eval do
    alias_method :open, :open_with_local
    remove_method :open_with_local
  end
end