em-http-request 0.2.9 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +2 -1
- data/.rspec +0 -0
- data/Changelog.md +54 -0
- data/Gemfile +3 -0
- data/README.md +175 -0
- data/Rakefile +11 -37
- data/em-http-request.gemspec +25 -90
- data/examples/fetch.rb +30 -30
- data/examples/fibered-http.rb +38 -38
- data/examples/oauth-tweet.rb +49 -49
- data/examples/socks5.rb +26 -0
- data/examples/websocket-handler.rb +28 -28
- data/examples/websocket-server.rb +8 -8
- data/lib/em-http/client.rb +242 -207
- data/lib/em-http/http_encoding.rb +135 -0
- data/lib/em-http/http_header.rb +71 -0
- data/lib/em-http/http_options.rb +7 -4
- data/lib/em-http/mock.rb +90 -50
- data/lib/em-http/multi.rb +55 -51
- data/lib/em-http/request.rb +2 -4
- data/lib/em-http/version.rb +5 -0
- data/lib/em-http.rb +19 -19
- data/spec/encoding_spec.rb +40 -0
- data/spec/fixtures/google.ca +20 -21
- data/spec/helper.rb +5 -4
- data/spec/mock_spec.rb +85 -36
- data/spec/multi_spec.rb +68 -51
- data/spec/request_spec.rb +422 -108
- data/spec/stallion.rb +65 -3
- metadata +111 -28
- data/LICENSE +0 -58
- data/README.rdoc +0 -138
- data/VERSION +0 -1
- data/lib/em-http/core_ext/hash.rb +0 -53
- data/spec/hash_spec.rb +0 -24
data/examples/oauth-tweet.rb
CHANGED
|
@@ -1,50 +1,50 @@
|
|
|
1
|
-
# Courtesy of Darcy Laycock:
|
|
2
|
-
# http://gist.github.com/265261
|
|
3
|
-
#
|
|
4
|
-
|
|
5
|
-
require 'rubygems'
|
|
6
|
-
|
|
7
|
-
require 'em-http'
|
|
8
|
-
require 'oauth'
|
|
9
|
-
|
|
10
|
-
# At a minimum, require 'oauth/request_proxy/em_http_request'
|
|
11
|
-
# for this example, we'll use Net::HTTP like support.
|
|
12
|
-
require 'oauth/client/em_http'
|
|
13
|
-
|
|
14
|
-
# You need two things: an oauth consumer and an access token.
|
|
15
|
-
# You need to generate an access token, I suggest looking elsewhere how to do that or wait for a full tutorial.
|
|
16
|
-
# For a consumer key / consumer secret, signup for an app at:
|
|
17
|
-
# http://twitter.com/apps/new
|
|
18
|
-
|
|
19
|
-
# Edit in your details.
|
|
20
|
-
CONSUMER_KEY = ""
|
|
21
|
-
CONSUMER_SECRET = ""
|
|
22
|
-
ACCESS_TOKEN = ""
|
|
23
|
-
ACCESS_TOKEN_SECRET = ""
|
|
24
|
-
|
|
25
|
-
def twitter_oauth_consumer
|
|
26
|
-
@twitter_oauth_consumer ||= OAuth::Consumer.new(CONSUMER_KEY, CONSUMER_SECRET, :site => "http://twitter.com")
|
|
27
|
-
end
|
|
28
|
-
|
|
29
|
-
def twitter_oauth_access_token
|
|
30
|
-
@twitter_oauth_access_token ||= OAuth::AccessToken.new(twitter_oauth_consumer, ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
|
|
31
|
-
end
|
|
32
|
-
|
|
33
|
-
EM.run do
|
|
34
|
-
|
|
35
|
-
request = EventMachine::HttpRequest.new('http://twitter.com/statuses/update.json')
|
|
36
|
-
http = request.post(:body => {'status' => 'Hello Twitter from em-http-request with OAuth'}, :head => {"Content-Type" => "application/x-www-form-urlencoded"}) do |client|
|
|
37
|
-
twitter_oauth_consumer.sign!(client, twitter_oauth_access_token)
|
|
38
|
-
end
|
|
39
|
-
|
|
40
|
-
http.callback do
|
|
41
|
-
puts "Response: #{http.response} (Code: #{http.response_header.status})"
|
|
42
|
-
EM.stop_event_loop
|
|
43
|
-
end
|
|
44
|
-
|
|
45
|
-
http.errback do
|
|
46
|
-
puts "Failed to post"
|
|
47
|
-
EM.stop_event_loop
|
|
48
|
-
end
|
|
49
|
-
|
|
1
|
+
# Courtesy of Darcy Laycock:
|
|
2
|
+
# http://gist.github.com/265261
|
|
3
|
+
#
|
|
4
|
+
|
|
5
|
+
require 'rubygems'
|
|
6
|
+
|
|
7
|
+
require 'em-http'
|
|
8
|
+
require 'oauth'
|
|
9
|
+
|
|
10
|
+
# At a minimum, require 'oauth/request_proxy/em_http_request'
|
|
11
|
+
# for this example, we'll use Net::HTTP like support.
|
|
12
|
+
require 'oauth/client/em_http'
|
|
13
|
+
|
|
14
|
+
# You need two things: an oauth consumer and an access token.
|
|
15
|
+
# You need to generate an access token, I suggest looking elsewhere how to do that or wait for a full tutorial.
|
|
16
|
+
# For a consumer key / consumer secret, signup for an app at:
|
|
17
|
+
# http://twitter.com/apps/new
|
|
18
|
+
|
|
19
|
+
# Edit in your details.
|
|
20
|
+
CONSUMER_KEY = ""
|
|
21
|
+
CONSUMER_SECRET = ""
|
|
22
|
+
ACCESS_TOKEN = ""
|
|
23
|
+
ACCESS_TOKEN_SECRET = ""
|
|
24
|
+
|
|
25
|
+
def twitter_oauth_consumer
|
|
26
|
+
@twitter_oauth_consumer ||= OAuth::Consumer.new(CONSUMER_KEY, CONSUMER_SECRET, :site => "http://twitter.com")
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def twitter_oauth_access_token
|
|
30
|
+
@twitter_oauth_access_token ||= OAuth::AccessToken.new(twitter_oauth_consumer, ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
EM.run do
|
|
34
|
+
|
|
35
|
+
request = EventMachine::HttpRequest.new('http://twitter.com/statuses/update.json')
|
|
36
|
+
http = request.post(:body => {'status' => 'Hello Twitter from em-http-request with OAuth'}, :head => {"Content-Type" => "application/x-www-form-urlencoded"}) do |client|
|
|
37
|
+
twitter_oauth_consumer.sign!(client, twitter_oauth_access_token)
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
http.callback do
|
|
41
|
+
puts "Response: #{http.response} (Code: #{http.response_header.status})"
|
|
42
|
+
EM.stop_event_loop
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
http.errback do
|
|
46
|
+
puts "Failed to post"
|
|
47
|
+
EM.stop_event_loop
|
|
48
|
+
end
|
|
49
|
+
|
|
50
50
|
end
|
data/examples/socks5.rb
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
require 'rubygems'
|
|
2
|
+
require 'eventmachine'
|
|
3
|
+
require '../lib/em-http'
|
|
4
|
+
|
|
5
|
+
EM.run do
|
|
6
|
+
# Establish a SOCKS5 tunnel via SSH
|
|
7
|
+
# ssh -D 8000 some_remote_machine
|
|
8
|
+
|
|
9
|
+
# http = EM::HttpRequest.new('http://whatismyip.org/').get({
|
|
10
|
+
http = EM::HttpRequest.new('http://igvita.com/').get({
|
|
11
|
+
:proxy => {:host => '127.0.0.1', :port => 8000, :type => :socks},
|
|
12
|
+
:redirects => 2
|
|
13
|
+
})
|
|
14
|
+
|
|
15
|
+
http.callback {
|
|
16
|
+
puts "#{http.response_header.status} - #{http.response.length} bytes\n"
|
|
17
|
+
puts http.response
|
|
18
|
+
EM.stop
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
http.errback {
|
|
22
|
+
puts "Error: " + http.error
|
|
23
|
+
puts http.inspect
|
|
24
|
+
EM.stop
|
|
25
|
+
}
|
|
26
|
+
end
|
|
@@ -1,28 +1,28 @@
|
|
|
1
|
-
require 'rubygems'
|
|
2
|
-
require 'lib/em-http'
|
|
3
|
-
|
|
4
|
-
module KBHandler
|
|
5
|
-
include EM::Protocols::LineText2
|
|
6
|
-
|
|
7
|
-
def receive_line(data)
|
|
8
|
-
p "Want to send: #{data}"
|
|
9
|
-
p "Error status: #{$http.error?}"
|
|
10
|
-
$http.send(data)
|
|
11
|
-
p "After send"
|
|
12
|
-
end
|
|
13
|
-
end
|
|
14
|
-
|
|
15
|
-
EventMachine.run {
|
|
16
|
-
$http = EventMachine::HttpRequest.new("ws://localhost:8080/").get :timeout => 0
|
|
17
|
-
|
|
18
|
-
$http.disconnect { puts 'oops' }
|
|
19
|
-
$http.callback {
|
|
20
|
-
puts "WebSocket connected!"
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
$http.stream { |msg|
|
|
24
|
-
puts "Recieved: #{msg}"
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
EM.open_keyboard(KBHandler)
|
|
28
|
-
}
|
|
1
|
+
require 'rubygems'
|
|
2
|
+
require 'lib/em-http'
|
|
3
|
+
|
|
4
|
+
module KBHandler
|
|
5
|
+
include EM::Protocols::LineText2
|
|
6
|
+
|
|
7
|
+
def receive_line(data)
|
|
8
|
+
p "Want to send: #{data}"
|
|
9
|
+
p "Error status: #{$http.error?}"
|
|
10
|
+
$http.send(data)
|
|
11
|
+
p "After send"
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
EventMachine.run {
|
|
16
|
+
$http = EventMachine::HttpRequest.new("ws://localhost:8080/").get :timeout => 0
|
|
17
|
+
|
|
18
|
+
$http.disconnect { puts 'oops' }
|
|
19
|
+
$http.callback {
|
|
20
|
+
puts "WebSocket connected!"
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
$http.stream { |msg|
|
|
24
|
+
puts "Recieved: #{msg}"
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
EM.open_keyboard(KBHandler)
|
|
28
|
+
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
require 'rubygems'
|
|
2
|
-
require 'em-websocket'
|
|
3
|
-
|
|
4
|
-
EventMachine::WebSocket.start(:host => "0.0.0.0", :port => 8080) do |ws|
|
|
5
|
-
ws.onopen { ws.send "Hello Client!"}
|
|
6
|
-
ws.onmessage { |msg| p "got: #{msg}"; ws.send "Pong: #{msg}" }
|
|
7
|
-
ws.onclose { puts "WebSocket closed" }
|
|
8
|
-
end
|
|
1
|
+
require 'rubygems'
|
|
2
|
+
require 'em-websocket'
|
|
3
|
+
|
|
4
|
+
EventMachine::WebSocket.start(:host => "0.0.0.0", :port => 8080) do |ws|
|
|
5
|
+
ws.onopen { ws.send "Hello Client!"}
|
|
6
|
+
ws.onmessage { |msg| p "got: #{msg}"; ws.send "Pong: #{msg}" }
|
|
7
|
+
ws.onclose { puts "WebSocket closed" }
|
|
8
|
+
end
|