astro-em-http-request 0.1.3.20090419 → 0.1.5
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/README.rdoc +17 -0
- data/em-http-request.gemspec +42 -0
- data/lib/em-http.rb +1 -0
- data/lib/em-http/client.rb +10 -2
- data/lib/em-http/core_ext/hash.rb +53 -0
- data/test/hash.rb +17 -0
- data/test/stallion.rb +1 -1
- data/test/test_request.rb +17 -3
- metadata +7 -3
data/.gitignore
ADDED
data/README.rdoc
CHANGED
@@ -50,3 +50,20 @@ EventMachine based HTTP Request interface. Supports streaming response processin
|
|
50
50
|
}
|
51
51
|
}
|
52
52
|
|
53
|
+
== POST example
|
54
|
+
|
55
|
+
EventMachine.run {
|
56
|
+
http1 = EventMachine::HttpRequest.new('http://www.website.com/').post :body => {"key1" => 1, "key2" => [2,3]}
|
57
|
+
http2 = EventMachine::HttpRequest.new('http://www.website.com/').post :body => "some data"
|
58
|
+
|
59
|
+
# ...
|
60
|
+
}
|
61
|
+
|
62
|
+
== Streaming body processing
|
63
|
+
EventMachine.run {
|
64
|
+
body = ''
|
65
|
+
on_body = lambda { |chunk| body += chunk }
|
66
|
+
http = EventMachine::HttpRequest.new('http://www.website.com/').get :on_response => on_body
|
67
|
+
|
68
|
+
# ...
|
69
|
+
}
|
@@ -0,0 +1,42 @@
|
|
1
|
+
spec = Gem::Specification.new do |s|
|
2
|
+
s.name = 'em-http-request'
|
3
|
+
s.version = '0.1.5'
|
4
|
+
s.date = '2009-03-20'
|
5
|
+
s.summary = 'EventMachine based HTTP Request interface'
|
6
|
+
s.description = s.summary
|
7
|
+
s.email = 'ilya@igvita.com'
|
8
|
+
s.homepage = "http://github.com/igrigorik/em-http-request"
|
9
|
+
s.has_rdoc = true
|
10
|
+
s.authors = ["Ilya Grigorik"]
|
11
|
+
s.add_dependency('eventmachine', '>= 0.12.2')
|
12
|
+
s.extensions = ["ext/buffer/extconf.rb" , "ext/http11_client/extconf.rb"]
|
13
|
+
s.rubyforge_project = "em-http-request"
|
14
|
+
|
15
|
+
# ruby -rpp -e' pp `git ls-files`.split("\n") '
|
16
|
+
s.files = [
|
17
|
+
".autotest",
|
18
|
+
".gitignore",
|
19
|
+
"LICENSE",
|
20
|
+
"README.rdoc",
|
21
|
+
"Rakefile",
|
22
|
+
"em-http-request.gemspec",
|
23
|
+
"ext/buffer/em_buffer.c",
|
24
|
+
"ext/buffer/extconf.rb",
|
25
|
+
"ext/http11_client/ext_help.h",
|
26
|
+
"ext/http11_client/extconf.rb",
|
27
|
+
"ext/http11_client/http11_client.c",
|
28
|
+
"ext/http11_client/http11_parser.c",
|
29
|
+
"ext/http11_client/http11_parser.h",
|
30
|
+
"ext/http11_client/http11_parser.rl",
|
31
|
+
"lib/em-http.rb",
|
32
|
+
"lib/em-http/client.rb",
|
33
|
+
"lib/em-http/core_ext/hash.rb",
|
34
|
+
"lib/em-http/decoders.rb",
|
35
|
+
"lib/em-http/multi.rb",
|
36
|
+
"lib/em-http/request.rb",
|
37
|
+
"test/hash.rb",
|
38
|
+
"test/helper.rb",
|
39
|
+
"test/stallion.rb",
|
40
|
+
"test/test_multi.rb",
|
41
|
+
"test/test_request.rb"]
|
42
|
+
end
|
data/lib/em-http.rb
CHANGED
@@ -11,6 +11,7 @@ require 'eventmachine'
|
|
11
11
|
require File.dirname(__FILE__) + '/http11_client'
|
12
12
|
require File.dirname(__FILE__) + '/em_buffer'
|
13
13
|
|
14
|
+
require File.dirname(__FILE__) + '/em-http/core_ext/hash'
|
14
15
|
require File.dirname(__FILE__) + '/em-http/client'
|
15
16
|
require File.dirname(__FILE__) + '/em-http/multi'
|
16
17
|
require File.dirname(__FILE__) + '/em-http/request'
|
data/lib/em-http/client.rb
CHANGED
@@ -219,7 +219,13 @@ module EventMachine
|
|
219
219
|
end
|
220
220
|
|
221
221
|
def send_request_body
|
222
|
-
|
222
|
+
return unless @options[:body]
|
223
|
+
if @options[:body].is_a? Hash
|
224
|
+
body = @options[:body].to_params
|
225
|
+
else
|
226
|
+
body = @options[:body]
|
227
|
+
end
|
228
|
+
send_data body
|
223
229
|
end
|
224
230
|
|
225
231
|
def receive_data(data)
|
@@ -249,7 +255,7 @@ module EventMachine
|
|
249
255
|
end
|
250
256
|
|
251
257
|
def unbind
|
252
|
-
(@state == :finished) ? succeed : fail
|
258
|
+
(@state == :finished) ? succeed(self) : fail
|
253
259
|
close_connection
|
254
260
|
end
|
255
261
|
|
@@ -423,6 +429,8 @@ module EventMachine
|
|
423
429
|
|
424
430
|
false
|
425
431
|
end
|
432
|
+
|
433
|
+
|
426
434
|
end
|
427
435
|
|
428
436
|
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
class Hash
|
2
|
+
# Stolen partially from Merb : http://noobkit.com/show/ruby/gems/development/merb/hash/to_params.html
|
3
|
+
# Convert this hash to a query string:
|
4
|
+
#
|
5
|
+
# { :name => "Bob",
|
6
|
+
# :address => {
|
7
|
+
# :street => '111 Ruby Ave.',
|
8
|
+
# :city => 'Ruby Central',
|
9
|
+
# :phones => ['111-111-1111', '222-222-2222']
|
10
|
+
# }
|
11
|
+
# }.to_params
|
12
|
+
# #=> "name=Bob&address[city]=Ruby Central&address[phones]=111-111-1111222-222-2222&address[street]=111 Ruby Ave."
|
13
|
+
#
|
14
|
+
def to_params
|
15
|
+
params = ''
|
16
|
+
stack = []
|
17
|
+
|
18
|
+
each do |k, v|
|
19
|
+
if v.is_a?(Hash)
|
20
|
+
stack << [k,v]
|
21
|
+
elsif v.is_a?(Array)
|
22
|
+
stack << [k,Hash.from_array(v)]
|
23
|
+
else
|
24
|
+
params << "#{k}=#{v}&"
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
stack.each do |parent, hash|
|
29
|
+
hash.each do |k, v|
|
30
|
+
if v.is_a?(Hash)
|
31
|
+
stack << ["#{parent}[#{k}]", v]
|
32
|
+
else
|
33
|
+
params << "#{parent}[#{k}]=#{v}&"
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
params.chop! # trailing &
|
39
|
+
params
|
40
|
+
end
|
41
|
+
|
42
|
+
##
|
43
|
+
# Builds a hash from an array with keys as array indices.
|
44
|
+
def self.from_array(array = [])
|
45
|
+
h = Hash.new
|
46
|
+
array.size.times do |t|
|
47
|
+
h[t] = array[t]
|
48
|
+
end
|
49
|
+
h
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
53
|
+
|
data/test/hash.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'test/helper'
|
2
|
+
|
3
|
+
describe Hash do
|
4
|
+
describe ".to_params" do
|
5
|
+
it "should transform a basic hash into HTTP POST Params" do
|
6
|
+
{:a => "alpha", :b => "beta"}.to_params.should == "a=alpha&b=beta"
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should transform a more complex hash into HTTP POST Params" do
|
10
|
+
{:a => "a", :b => ["c", "d", "e"]}.to_params.should == "a=a&b[0]=c&b[1]=d&b[2]=e"
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should transform a very complex hash into HTTP POST Params" do
|
14
|
+
{:a => "a", :b => [{:c => "c", :d => "d"}, {:e => "e", :f => "f"}]}.to_params.should == "a=a&b[0][d]=d&b[0][c]=c&b[1][e]=e&b[1][f]=f"
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
data/test/stallion.rb
CHANGED
@@ -81,7 +81,7 @@ Stallion.saddle :spec do |stable|
|
|
81
81
|
stable.response.write stable.request.query_string
|
82
82
|
|
83
83
|
elsif stable.request.post?
|
84
|
-
stable.response.write
|
84
|
+
stable.response.write stable.request.body.read
|
85
85
|
|
86
86
|
elsif stable.request.path_info == '/timeout'
|
87
87
|
sleep(10)
|
data/test/test_request.rb
CHANGED
@@ -19,7 +19,7 @@ describe EventMachine::HttpRequest do
|
|
19
19
|
}
|
20
20
|
end
|
21
21
|
|
22
|
-
|
22
|
+
it "should fail GET on invalid host" do
|
23
23
|
EventMachine.run {
|
24
24
|
http = EventMachine::HttpRequest.new('http://google1.com/').get
|
25
25
|
http.callback { failed }
|
@@ -112,7 +112,21 @@ describe EventMachine::HttpRequest do
|
|
112
112
|
http.errback { failed }
|
113
113
|
http.callback {
|
114
114
|
http.response_header.status.should == 200
|
115
|
-
http.response.should match(/
|
115
|
+
http.response.should match(/data/)
|
116
|
+
EventMachine.stop
|
117
|
+
}
|
118
|
+
}
|
119
|
+
end
|
120
|
+
|
121
|
+
it "should perform successfull POST with Ruby Hash/Array as params" do
|
122
|
+
EventMachine.run {
|
123
|
+
http = EventMachine::HttpRequest.new('http://127.0.0.1:8080/').post :body => {"key1" => 1, "key2" => [2,3]}
|
124
|
+
|
125
|
+
http.errback { failed }
|
126
|
+
http.callback {
|
127
|
+
http.response_header.status.should == 200
|
128
|
+
|
129
|
+
http.response.should match(/key1=1&key2\[0\]=2&key2\[1\]=3/)
|
116
130
|
EventMachine.stop
|
117
131
|
}
|
118
132
|
}
|
@@ -236,7 +250,7 @@ describe EventMachine::HttpRequest do
|
|
236
250
|
body = ''
|
237
251
|
on_body = lambda { |chunk| body += chunk }
|
238
252
|
http = EventMachine::HttpRequest.new('http://127.0.0.1:8080/deflate').get :head => {"accept-encoding" => "deflate, compressed"},
|
239
|
-
|
253
|
+
:on_response => on_body
|
240
254
|
|
241
255
|
http.errback { failed }
|
242
256
|
http.callback {
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: astro-em-http-request
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ilya Grigorik
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-
|
12
|
+
date: 2009-03-20 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -33,9 +33,11 @@ extra_rdoc_files: []
|
|
33
33
|
|
34
34
|
files:
|
35
35
|
- .autotest
|
36
|
+
- .gitignore
|
36
37
|
- LICENSE
|
37
38
|
- README.rdoc
|
38
39
|
- Rakefile
|
40
|
+
- em-http-request.gemspec
|
39
41
|
- ext/buffer/em_buffer.c
|
40
42
|
- ext/buffer/extconf.rb
|
41
43
|
- ext/http11_client/ext_help.h
|
@@ -46,9 +48,11 @@ files:
|
|
46
48
|
- ext/http11_client/http11_parser.rl
|
47
49
|
- lib/em-http.rb
|
48
50
|
- lib/em-http/client.rb
|
51
|
+
- lib/em-http/core_ext/hash.rb
|
52
|
+
- lib/em-http/decoders.rb
|
49
53
|
- lib/em-http/multi.rb
|
50
54
|
- lib/em-http/request.rb
|
51
|
-
-
|
55
|
+
- test/hash.rb
|
52
56
|
- test/helper.rb
|
53
57
|
- test/stallion.rb
|
54
58
|
- test/test_multi.rb
|