net-http-local 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source 'http://rubygems.org'
2
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ (The MIT License)
2
+
3
+ Copyright (c) 2012 Hakan Ensari
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,36 @@
1
+ Net::HTTP::Local
2
+ ================
3
+
4
+ Net::HTTP::Local allows TCPSocket to bind a Net::HTTP request to a specified
5
+ local address and port.
6
+
7
+ If you are craving cURL's `interface` option, this is what you need.
8
+
9
+ Installation
10
+ ------------
11
+
12
+ ```sh
13
+ gem install net-http-local'
14
+ ```
15
+
16
+ Usage
17
+ -----
18
+
19
+ ```ruby
20
+ require 'net/http'
21
+ require 'net/http/local'
22
+
23
+ # Bind to 10.1.1.3 in a block.
24
+ Net::HTTP.bind '10.1.1.3' do
25
+ res = Net::HTTP.get_response uri
26
+ p JSON.parse(res.body)['ip'] # => 10.1.1.3
27
+ end
28
+
29
+ # Bind and unbind without a block.
30
+ Net::HTTP.bind '10.1.1.3'
31
+
32
+ res = Net::HTTP.get_response(uri)
33
+ p JSON.parse(res.body)['ip'] # => 10.1.1.3
34
+
35
+ Net::HTTP.unbind
36
+ ```
@@ -0,0 +1,11 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ task :default => :test
5
+
6
+ desc 'Run the test suite'
7
+ task :test do
8
+ Dir['test/**/*_test.rb'].each do |f|
9
+ ruby(f)
10
+ end
11
+ end
@@ -0,0 +1,43 @@
1
+ # Net::HTTP::Local allows TCPSocket to bind a Net::HTTP request to a specified
2
+ # local address and port.
3
+ #
4
+ # If you are craving cURL's `interface` option, this is what you need.
5
+ module Net::HTTP::Local
6
+ # Binds to a local IP address.
7
+ #
8
+ # local_address - A local IP address or hostname.
9
+ # local_port - A local port (default: nil).
10
+ #
11
+ # If given a block, unbinds after the block executes.
12
+ #
13
+ # Returns nothing.
14
+ def bind(local_address, local_port = nil)
15
+ (class << TCPSocket; self; end).instance_eval do
16
+ alias_method :open_with_local, :open
17
+ define_method(:open) do |conn_address, conn_port|
18
+ open_with_local conn_address, conn_port, local_address, local_port
19
+ end
20
+ end
21
+
22
+ if block_given?
23
+ begin
24
+ yield
25
+ ensure
26
+ unbind
27
+ end
28
+ end
29
+ end
30
+
31
+ # Unbinds from the local address and port.
32
+ #
33
+ # Returns nothing.
34
+ def unbind
35
+ (class << TCPSocket; self; end).instance_eval do
36
+ alias_method :open, :open_with_local
37
+ remove_method :open_with_local
38
+ end
39
+ end
40
+ end
41
+
42
+ Net::HTTP.extend Net::HTTP::Local if defined? Net::HTTP
43
+ Net::HTTPS.extend Net::HTTP::Local if defined? Net::HTTPS
@@ -0,0 +1,3 @@
1
+ module Net::HTTP::Local
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path('../lib', __FILE__)
3
+ require 'net/http'
4
+ require 'net/http/local/version'
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = 'net-http-local'
8
+ s.version = Net::HTTP::Local::VERSION
9
+ s.platform = Gem::Platform::RUBY
10
+ s.authors = ['Hakan Ensari']
11
+ s.email = ['hakan.ensari@papercavalier.com']
12
+ s.homepage = 'http://github.com/hakanensari/net-http-local'
13
+ s.summary = %q{ Binds Net::HTTP requests to a local address and port }
14
+ s.description = %q{ Net::HTTP::Local allows TCPSocket to bind Net::HTTP
15
+ requests to a local address and port. }
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.require_paths = ['lib']
20
+ end
@@ -0,0 +1,62 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+
3
+ require 'test/unit'
4
+ require 'json'
5
+ require 'net/http'
6
+ require 'net/http/local'
7
+
8
+ class TestNetHTTPLocal < Test::Unit::TestCase
9
+ def find_ip
10
+ uri = URI.parse 'http://jsonip.com'
11
+ res = Net::HTTP.start(uri.host, uri.port) do |http|
12
+ http.get '/'
13
+ end
14
+
15
+ JSON.parse(res.body)['ip']
16
+ end
17
+
18
+ def setup
19
+ unless defined? @@old_ip
20
+ @@old_ip = find_ip
21
+ print "Change #{@@old_ip} to: "
22
+ @@new_ip = STDIN.gets.chomp
23
+ end
24
+ end
25
+
26
+ def teardown
27
+ Net::HTTP.unbind if TCPSocket.respond_to? :open_with_local
28
+ end
29
+
30
+ def test_bind_without_block
31
+ Net::HTTP.bind @@new_ip
32
+ assert_equal @@new_ip, find_ip
33
+
34
+ Net::HTTP.unbind
35
+ assert_equal @@old_ip, find_ip
36
+ end
37
+
38
+ def test_bind_with_block
39
+ Net::HTTP.bind @@new_ip do
40
+ assert_equal @@new_ip, find_ip
41
+ end
42
+
43
+ assert_equal @@old_ip, find_ip
44
+ end
45
+
46
+ def test_do_not_unbind_when_bind_not_given_block
47
+ Net::HTTP.bind @@new_ip
48
+ assert_respond_to TCPSocket, :open_with_local
49
+ end
50
+
51
+ def test_unbind_when_bind_given_block
52
+ Net::HTTP.bind(@@new_ip) {}
53
+ assert_raise NoMethodError do
54
+ TCPSocket.open_with_local
55
+ end
56
+ end
57
+
58
+ def test_bind_nil
59
+ Net::HTTP.bind nil
60
+ assert_equal @@old_ip, find_ip
61
+ end
62
+ end
metadata ADDED
@@ -0,0 +1,56 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: net-http-local
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Hakan Ensari
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-04-13 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: ! " Net::HTTP::Local allows TCPSocket to bind Net::HTTP\n requests
15
+ to a local address and port. "
16
+ email:
17
+ - hakan.ensari@papercavalier.com
18
+ executables: []
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - .gitignore
23
+ - Gemfile
24
+ - LICENSE
25
+ - README.md
26
+ - Rakefile
27
+ - lib/net/http/local.rb
28
+ - lib/net/http/local/version.rb
29
+ - net-http-local.gemspec
30
+ - test/net_http_local_test.rb
31
+ homepage: http://github.com/hakanensari/net-http-local
32
+ licenses: []
33
+ post_install_message:
34
+ rdoc_options: []
35
+ require_paths:
36
+ - lib
37
+ required_ruby_version: !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ! '>='
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ required_rubygems_version: !ruby/object:Gem::Requirement
44
+ none: false
45
+ requirements:
46
+ - - ! '>='
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ requirements: []
50
+ rubyforge_project:
51
+ rubygems_version: 1.8.11
52
+ signing_key:
53
+ specification_version: 3
54
+ summary: Binds Net::HTTP requests to a local address and port
55
+ test_files:
56
+ - test/net_http_local_test.rb