alovak-network 1.1.4 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/network/connection.rb +33 -8
- data/lib/network.rb +7 -2
- data/test/network/test_connection.rb +12 -3
- data/test/network/test_network.rb +5 -0
- metadata +30 -9
data/lib/network/connection.rb
CHANGED
@@ -26,18 +26,30 @@ module Network
|
|
26
26
|
VERIFY_NONE = OpenSSL::SSL::VERIFY_NONE
|
27
27
|
VERIFY_PEER = OpenSSL::SSL::VERIFY_PEER
|
28
28
|
|
29
|
-
|
29
|
+
# options are:
|
30
|
+
# :read_timeout
|
31
|
+
# :open_timeout
|
32
|
+
# :verify_peer
|
33
|
+
# :proxy_addr
|
34
|
+
# :proxy_port
|
35
|
+
# :proxy_user
|
36
|
+
# :proxy_pass
|
37
|
+
def initialize(uri, options = {})
|
30
38
|
@uri = URI.parse(uri)
|
31
|
-
@read_timeout = READ_TIMEOUT
|
32
|
-
@open_timeout = OPEN_TIMEOUT
|
33
|
-
@verify_peer = false
|
39
|
+
@read_timeout = options[:read_timeout] || READ_TIMEOUT
|
40
|
+
@open_timeout = options[:open_timeout] || OPEN_TIMEOUT
|
41
|
+
@verify_peer = options[:verify_peer] || false
|
34
42
|
@debugger_stream = nil
|
35
43
|
@headers = {}
|
44
|
+
@proxy_addr = options[:proxy_addr]
|
45
|
+
@proxy_port = options[:proxy_port]
|
46
|
+
@proxy_user = options[:proxy_user]
|
47
|
+
@proxy_pass = options[:proxy_pass]
|
36
48
|
end
|
37
49
|
|
38
50
|
def post(data)
|
39
51
|
try_request do
|
40
|
-
log_request(data)
|
52
|
+
log_request(data, "POST")
|
41
53
|
response = nil
|
42
54
|
ms = Benchmark.realtime do
|
43
55
|
response = http.post(uri.path, data, post_headers(data))
|
@@ -47,6 +59,19 @@ module Network
|
|
47
59
|
end
|
48
60
|
end
|
49
61
|
|
62
|
+
def get(data)
|
63
|
+
try_request do
|
64
|
+
log_request(data, "GET")
|
65
|
+
response = nil
|
66
|
+
query_string = uri.path + data
|
67
|
+
ms = Benchmark.realtime do
|
68
|
+
response = http.get(query_string)
|
69
|
+
end
|
70
|
+
log_response(response, ms)
|
71
|
+
response
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
50
75
|
def use_ssl?
|
51
76
|
@uri.scheme == "https"
|
52
77
|
end
|
@@ -58,7 +83,7 @@ module Network
|
|
58
83
|
private
|
59
84
|
|
60
85
|
def http
|
61
|
-
http = Net::HTTP.new(uri.host, uri.port)
|
86
|
+
http = Net::HTTP.new(uri.host, uri.port, @proxy_addr, @proxy_port, @proxy_user, @proxy_pass)
|
62
87
|
configure_timeouts(http)
|
63
88
|
configure_debugging(http)
|
64
89
|
|
@@ -123,9 +148,9 @@ module Network
|
|
123
148
|
logger.info(message) if logger
|
124
149
|
end
|
125
150
|
|
126
|
-
def log_request(data)
|
151
|
+
def log_request(data, method)
|
127
152
|
log "[#{sender}]" if sender
|
128
|
-
log "
|
153
|
+
log "#{method} #{uri}"
|
129
154
|
log "--->"
|
130
155
|
log (request_filter ? request_filter.call(data) : data)
|
131
156
|
end
|
data/lib/network.rb
CHANGED
@@ -3,7 +3,12 @@ require 'net/https'
|
|
3
3
|
require 'network/connection'
|
4
4
|
|
5
5
|
module Network
|
6
|
-
def self.post(url, data)
|
7
|
-
Connection.new(url).post(data)
|
6
|
+
def self.post(url, data, options = {})
|
7
|
+
Connection.new(url, options).post(data)
|
8
8
|
end
|
9
|
+
|
10
|
+
def self.get(url, data, options = {})
|
11
|
+
Connection.new(url, options).get(data)
|
12
|
+
end
|
13
|
+
|
9
14
|
end
|
@@ -19,7 +19,7 @@ class TestConnectionLogging < Test::Unit::TestCase
|
|
19
19
|
end
|
20
20
|
|
21
21
|
def test_log_request
|
22
|
-
@connection.send(:log_request, "request data")
|
22
|
+
@connection.send(:log_request, "request data", "POST")
|
23
23
|
log = @connection.logger.log
|
24
24
|
|
25
25
|
assert_match /POST http:\/\/example.com\/path/, log
|
@@ -29,7 +29,7 @@ class TestConnectionLogging < Test::Unit::TestCase
|
|
29
29
|
|
30
30
|
def test_log_request_with_sender
|
31
31
|
@connection.sender = "ModuleName"
|
32
|
-
@connection.send(:log_request, "request data")
|
32
|
+
@connection.send(:log_request, "request data", "POST")
|
33
33
|
assert_match /ModuleName/, @connection.logger.log
|
34
34
|
end
|
35
35
|
|
@@ -45,7 +45,7 @@ class TestConnectionLogging < Test::Unit::TestCase
|
|
45
45
|
|
46
46
|
def test_request_filtering
|
47
47
|
@connection.request_filter = Proc.new {|req| "#{req} is filtered"}
|
48
|
-
@connection.send(:log_request, "request")
|
48
|
+
@connection.send(:log_request, "request", "POST")
|
49
49
|
assert_match /request is filtered/, @connection.logger.log
|
50
50
|
end
|
51
51
|
|
@@ -89,6 +89,15 @@ class TestConnection < Test::Unit::TestCase
|
|
89
89
|
@connection.post("hello")
|
90
90
|
end
|
91
91
|
|
92
|
+
def test_get_methods
|
93
|
+
sec = sequence('order')
|
94
|
+
@connection.expects(:log_request).in_sequence(sec)
|
95
|
+
@connection.expects(:http).in_sequence(sec).returns(stub('http', :get => true))
|
96
|
+
@connection.expects(:log_response).in_sequence(sec)
|
97
|
+
|
98
|
+
@connection.get("query=hello")
|
99
|
+
end
|
100
|
+
|
92
101
|
def test_default_timeouts
|
93
102
|
assert_equal Network::Connection::READ_TIMEOUT, @connection.read_timeout
|
94
103
|
assert_equal Network::Connection::OPEN_TIMEOUT, @connection.open_timeout
|
@@ -5,4 +5,9 @@ class TestNetwork < Test::Unit::TestCase
|
|
5
5
|
Network::Connection.any_instance.expects(:post)
|
6
6
|
Network.post('http://site.com/', "some data")
|
7
7
|
end
|
8
|
+
|
9
|
+
def test_make_get_throught_connection
|
10
|
+
Network::Connection.any_instance.expects(:get)
|
11
|
+
Network.get('http://site.com/', 'query=helo')
|
12
|
+
end
|
8
13
|
end
|
metadata
CHANGED
@@ -1,7 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: alovak-network
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
4
|
+
hash: 31
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 1
|
8
|
+
- 2
|
9
|
+
- 0
|
10
|
+
version: 1.2.0
|
5
11
|
platform: ruby
|
6
12
|
authors:
|
7
13
|
- Pavel Gabriel
|
@@ -9,19 +15,25 @@ autorequire:
|
|
9
15
|
bindir: bin
|
10
16
|
cert_chain: []
|
11
17
|
|
12
|
-
date: 2010-
|
18
|
+
date: 2010-09-06 00:00:00 +03:00
|
13
19
|
default_executable:
|
14
20
|
dependencies:
|
15
21
|
- !ruby/object:Gem::Dependency
|
16
22
|
name: mocha
|
17
|
-
|
18
|
-
|
19
|
-
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
20
26
|
requirements:
|
21
27
|
- - ">="
|
22
28
|
- !ruby/object:Gem::Version
|
29
|
+
hash: 53
|
30
|
+
segments:
|
31
|
+
- 0
|
32
|
+
- 9
|
33
|
+
- 7
|
23
34
|
version: 0.9.7
|
24
|
-
|
35
|
+
type: :runtime
|
36
|
+
version_requirements: *id001
|
25
37
|
description: HTTP/HTTPS communication module with logging, filtering and easy SSL configuration
|
26
38
|
email: alovak@gmail.com
|
27
39
|
executables: []
|
@@ -33,6 +45,9 @@ extra_rdoc_files: []
|
|
33
45
|
files:
|
34
46
|
- lib/network/connection.rb
|
35
47
|
- lib/network.rb
|
48
|
+
- test/network/test_network.rb
|
49
|
+
- test/network/test_connection.rb
|
50
|
+
- test/test_helper.rb
|
36
51
|
has_rdoc: true
|
37
52
|
homepage: http://github.com/alovak/network/
|
38
53
|
licenses: []
|
@@ -43,21 +58,27 @@ rdoc_options: []
|
|
43
58
|
require_paths:
|
44
59
|
- lib
|
45
60
|
required_ruby_version: !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
46
62
|
requirements:
|
47
63
|
- - ">="
|
48
64
|
- !ruby/object:Gem::Version
|
65
|
+
hash: 3
|
66
|
+
segments:
|
67
|
+
- 0
|
49
68
|
version: "0"
|
50
|
-
version:
|
51
69
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
70
|
+
none: false
|
52
71
|
requirements:
|
53
72
|
- - ">="
|
54
73
|
- !ruby/object:Gem::Version
|
74
|
+
hash: 3
|
75
|
+
segments:
|
76
|
+
- 0
|
55
77
|
version: "0"
|
56
|
-
version:
|
57
78
|
requirements: []
|
58
79
|
|
59
80
|
rubyforge_project:
|
60
|
-
rubygems_version: 1.3.
|
81
|
+
rubygems_version: 1.3.7
|
61
82
|
signing_key:
|
62
83
|
specification_version: 3
|
63
84
|
summary: HTTP/HTTPS communication module based on ruby net/http, net/https modules
|