excon 0.6.5 → 0.6.6
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of excon might be problematic. Click here for more details.
- data/README.rdoc +12 -7
- data/Rakefile +1 -1
- data/excon.gemspec +5 -2
- data/lib/excon.rb +2 -9
- data/lib/excon/connection.rb +9 -0
- data/lib/excon/constants.rb +13 -0
- data/lib/excon/response.rb +11 -13
- data/tests/rackups/request_methods.ru +17 -0
- data/tests/request_method_tests.rb +33 -0
- metadata +7 -4
data/README.rdoc
CHANGED
@@ -17,32 +17,37 @@ The simplest way to use excon is with one-off requests:
|
|
17
17
|
|
18
18
|
response = Excon.get('http://geemus.com')
|
19
19
|
|
20
|
-
Supported one
|
20
|
+
Supported one-off request methods are #connect, #delete, #get, #head, #options, #post, #put, and #trace.
|
21
21
|
|
22
22
|
The returned response object has #body, #headers and #status attributes.
|
23
23
|
|
24
24
|
Alternately you can create a connection object which is reusable across multiple requests (more performant!).
|
25
25
|
|
26
26
|
connection = Excon.new('http://geemus.com')
|
27
|
-
response_one = connection.
|
28
|
-
response_two = connection.
|
29
|
-
response_three = connection.
|
27
|
+
response_one = connection.get
|
28
|
+
response_two = connection.post(:path => '/foo')
|
29
|
+
response_three = connection.delete(:path => '/bar')
|
30
|
+
|
31
|
+
Sometimes it is more convenient to specify the request type as an argument:
|
32
|
+
|
33
|
+
response_four = connection.request(:method => :get, :path => '/more')
|
30
34
|
|
31
35
|
Both one-off and persistent connections support many other options. Here are a few common examples:
|
32
36
|
|
33
37
|
# Custom headers
|
34
38
|
Excon.get('http://geemus.com', :headers => {'Authorization' => 'Basic 0123456789ABCDEF'})
|
35
|
-
connection.
|
39
|
+
connection.get(:headers => {'Authorization' => 'Basic 0123456789ABCDEF'})
|
36
40
|
|
37
41
|
# Changing query strings
|
38
42
|
connection = Excon.new('http://geemus.com/')
|
39
|
-
connection.
|
43
|
+
connection.get(:query => {:foo => 'bar'})
|
40
44
|
|
41
45
|
# POST body
|
42
46
|
Excon.post('http://geemus.com', :body => 'language=ruby&class=fog')
|
43
47
|
|
44
|
-
#
|
48
|
+
# request accepts either symbols or strings
|
45
49
|
connection.request(:method => :get)
|
50
|
+
connection.request(:method => 'GET')
|
46
51
|
|
47
52
|
These options can be combined to make pretty much any request you might need.
|
48
53
|
|
data/Rakefile
CHANGED
data/excon.gemspec
CHANGED
@@ -13,8 +13,8 @@ Gem::Specification.new do |s|
|
|
13
13
|
## If your rubyforge_project name is different, then edit it and comment out
|
14
14
|
## the sub! line in the Rakefile
|
15
15
|
s.name = 'excon'
|
16
|
-
s.version = '0.6.
|
17
|
-
s.date = '2011-
|
16
|
+
s.version = '0.6.6'
|
17
|
+
s.date = '2011-09-06'
|
18
18
|
s.rubyforge_project = 'excon'
|
19
19
|
|
20
20
|
## Make sure your summary is short. The description may be as long
|
@@ -87,6 +87,7 @@ Gem::Specification.new do |s|
|
|
87
87
|
excon.gemspec
|
88
88
|
lib/excon.rb
|
89
89
|
lib/excon/connection.rb
|
90
|
+
lib/excon/constants.rb
|
90
91
|
lib/excon/errors.rb
|
91
92
|
lib/excon/response.rb
|
92
93
|
tests/basic_tests.rb
|
@@ -97,8 +98,10 @@ Gem::Specification.new do |s|
|
|
97
98
|
tests/rackups/basic.ru
|
98
99
|
tests/rackups/proxy.ru
|
99
100
|
tests/rackups/query_string.ru
|
101
|
+
tests/rackups/request_methods.ru
|
100
102
|
tests/rackups/response_header.ru
|
101
103
|
tests/rackups/thread_safety.ru
|
104
|
+
tests/request_method_tests.rb
|
102
105
|
tests/stub_tests.rb
|
103
106
|
tests/test_helper.rb
|
104
107
|
tests/thread_safety_tests.rb
|
data/lib/excon.rb
CHANGED
@@ -7,19 +7,12 @@ require 'rbconfig'
|
|
7
7
|
require 'socket'
|
8
8
|
require 'uri'
|
9
9
|
|
10
|
+
require 'excon/constants'
|
10
11
|
require 'excon/connection'
|
11
12
|
require 'excon/errors'
|
12
13
|
require 'excon/response'
|
13
14
|
|
14
15
|
module Excon
|
15
|
-
unless const_defined?(:VERSION)
|
16
|
-
VERSION = '0.6.5'
|
17
|
-
end
|
18
|
-
|
19
|
-
unless const_defined?(:CHUNK_SIZE)
|
20
|
-
CHUNK_SIZE = 1048576 # 1 megabyte
|
21
|
-
end
|
22
|
-
|
23
16
|
class << self
|
24
17
|
# @return [String] The filesystem path to the SSL Certificate Authority
|
25
18
|
attr_accessor :ssl_ca_path
|
@@ -85,7 +78,7 @@ module Excon
|
|
85
78
|
end
|
86
79
|
|
87
80
|
# Generic non-persistent HTTP methods
|
88
|
-
|
81
|
+
HTTP_VERBS.each do |method|
|
89
82
|
eval <<-DEF
|
90
83
|
def #{method}(url, params = {}, &block)
|
91
84
|
new(url).request(params.merge!(:method => :#{method}), &block)
|
data/lib/excon/connection.rb
CHANGED
@@ -212,6 +212,15 @@ module Excon
|
|
212
212
|
def reset
|
213
213
|
(old_socket = sockets.delete(@socket_key)) && old_socket.close
|
214
214
|
end
|
215
|
+
|
216
|
+
# Generate HTTP request verb methods
|
217
|
+
Excon::HTTP_VERBS.each do |method|
|
218
|
+
eval <<-DEF
|
219
|
+
def #{method}(params={}, &block)
|
220
|
+
request(params.merge!(:method => :#{method}), &block)
|
221
|
+
end
|
222
|
+
DEF
|
223
|
+
end
|
215
224
|
|
216
225
|
private
|
217
226
|
def connect
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module Excon
|
2
|
+
unless const_defined?(:VERSION)
|
3
|
+
VERSION = '0.6.6'
|
4
|
+
end
|
5
|
+
|
6
|
+
unless const_defined?(:CHUNK_SIZE)
|
7
|
+
CHUNK_SIZE = 1048576 # 1 megabyte
|
8
|
+
end
|
9
|
+
|
10
|
+
unless const_defined?(:HTTP_VERBS)
|
11
|
+
HTTP_VERBS = %w{connect delete get head options post put trace}
|
12
|
+
end
|
13
|
+
end
|
data/lib/excon/response.rb
CHANGED
@@ -29,8 +29,6 @@ module Excon
|
|
29
29
|
content_length = value.to_i
|
30
30
|
elsif (key.casecmp('Transfer-Encoding') == 0) && (value.casecmp('chunked') == 0)
|
31
31
|
transfer_encoding_chunked = true
|
32
|
-
elsif (key.casecmp('Connection') == 0) && (value.casecmp('close') == 0)
|
33
|
-
connection_close = true
|
34
32
|
end
|
35
33
|
end
|
36
34
|
|
@@ -48,15 +46,16 @@ module Excon
|
|
48
46
|
yield(socket.read(chunk_size + 2).chop!, nil, content_length)
|
49
47
|
end
|
50
48
|
socket.read(2)
|
51
|
-
elsif
|
52
|
-
remaining = socket.read
|
53
|
-
yield(remaining, remaining.length, content_length)
|
54
|
-
else
|
49
|
+
elsif remaining = content_length
|
55
50
|
remaining = content_length
|
56
51
|
while remaining > 0
|
57
52
|
yield(socket.read([CHUNK_SIZE, remaining].min), [remaining - CHUNK_SIZE, 0].max, content_length)
|
58
53
|
remaining -= CHUNK_SIZE
|
59
54
|
end
|
55
|
+
else
|
56
|
+
while remaining = socket.read(CHUNK_SIZE)
|
57
|
+
yield(remaining, remaining.length, content_length)
|
58
|
+
end
|
60
59
|
end
|
61
60
|
else
|
62
61
|
if transfer_encoding_chunked
|
@@ -64,27 +63,26 @@ module Excon
|
|
64
63
|
response.body << socket.read(chunk_size + 2).chop! # 2 == "/r/n".length
|
65
64
|
end
|
66
65
|
socket.read(2) # 2 == "/r/n".length
|
67
|
-
elsif
|
68
|
-
response.body << socket.read
|
69
|
-
else
|
70
|
-
remaining = content_length
|
66
|
+
elsif remaining = content_length
|
71
67
|
while remaining > 0
|
72
68
|
response.body << socket.read([CHUNK_SIZE, remaining].min)
|
73
69
|
remaining -= CHUNK_SIZE
|
74
70
|
end
|
71
|
+
else
|
72
|
+
response.body << socket.read
|
75
73
|
end
|
76
74
|
end
|
77
75
|
end
|
78
76
|
|
79
77
|
response
|
80
78
|
end
|
81
|
-
|
79
|
+
|
82
80
|
# Retrieve a specific header value. Header names are treated case-insensitively.
|
83
81
|
# @param [String] name Header name
|
84
82
|
def get_header(name)
|
85
83
|
headers.each do |key,value|
|
86
|
-
if key.casecmp(name) == 0
|
87
|
-
return value
|
84
|
+
if key.casecmp(name) == 0
|
85
|
+
return value
|
88
86
|
end
|
89
87
|
end
|
90
88
|
nil
|
@@ -0,0 +1,33 @@
|
|
1
|
+
Shindo.tests('Excon request methods') do
|
2
|
+
|
3
|
+
with_rackup('request_methods.ru') do
|
4
|
+
|
5
|
+
tests 'one-offs' do
|
6
|
+
|
7
|
+
tests('Excon.get').returns('GET') do
|
8
|
+
Excon.get('http://127.0.0.1:9292').body
|
9
|
+
end
|
10
|
+
|
11
|
+
tests('Excon.post').returns('POST') do
|
12
|
+
Excon.post('http://127.0.0.1:9292').body
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
16
|
+
|
17
|
+
tests 'with a connection object' do
|
18
|
+
|
19
|
+
connection = Excon.new('http://127.0.0.1:9292')
|
20
|
+
|
21
|
+
tests('connection.get').returns('GET') do
|
22
|
+
connection.get.body
|
23
|
+
end
|
24
|
+
|
25
|
+
tests('connection.post').returns('POST') do
|
26
|
+
connection.post.body
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: excon
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 11
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 6
|
9
|
-
-
|
10
|
-
version: 0.6.
|
9
|
+
- 6
|
10
|
+
version: 0.6.6
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- geemus (Wesley Beary)
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-
|
18
|
+
date: 2011-09-06 00:00:00 Z
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
21
|
name: open4
|
@@ -108,6 +108,7 @@ files:
|
|
108
108
|
- excon.gemspec
|
109
109
|
- lib/excon.rb
|
110
110
|
- lib/excon/connection.rb
|
111
|
+
- lib/excon/constants.rb
|
111
112
|
- lib/excon/errors.rb
|
112
113
|
- lib/excon/response.rb
|
113
114
|
- tests/basic_tests.rb
|
@@ -118,8 +119,10 @@ files:
|
|
118
119
|
- tests/rackups/basic.ru
|
119
120
|
- tests/rackups/proxy.ru
|
120
121
|
- tests/rackups/query_string.ru
|
122
|
+
- tests/rackups/request_methods.ru
|
121
123
|
- tests/rackups/response_header.ru
|
122
124
|
- tests/rackups/thread_safety.ru
|
125
|
+
- tests/request_method_tests.rb
|
123
126
|
- tests/stub_tests.rb
|
124
127
|
- tests/test_helper.rb
|
125
128
|
- tests/thread_safety_tests.rb
|