aeden-jruby-http-reactor 0.2.0 → 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/README.rdoc CHANGED
@@ -1,10 +1,13 @@
1
1
  A client that uses the Apache HttpCore NIO library to do HTTP requests.
2
2
 
3
3
  <pre>
4
+ require 'http_reactor'
5
+
4
6
  uris = ['http://www.yahoo.com/','http://www.google.com/']
5
7
  requests = uris.map { |uri| HttpReactor::Request.new(uri) }
6
8
 
7
9
  HttpReactor::Client.new(requests) do |response, context|
8
- puts "Response: #{response.status_line.status_code}"
10
+ puts "Response code: #{response.code}"
11
+ puts "Response body: #{response.body}"
9
12
  end
10
13
  </pre>
data/README.textile CHANGED
@@ -7,6 +7,7 @@ uris = ['http://www.yahoo.com/','http://www.google.com/']
7
7
  requests = uris.map { |uri| HttpReactor::Request.new(uri) }
8
8
 
9
9
  HttpReactor::Client.new(requests) do |response, context|
10
- puts "Response: #{response.status_line.status_code}"
10
+ puts "Response code: #{response.code}"
11
+ puts "Response body: #{response.body}"
11
12
  end
12
13
  </pre>
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.0
1
+ 0.3.0
data/examples/opml.rb CHANGED
@@ -1,4 +1,4 @@
1
- #!/usr/bin/env ruby
1
+ #!/usr/bin/env jruby
2
2
  #
3
3
  # Usage: jruby examples/opml.rb opml.xml
4
4
  #
@@ -25,6 +25,7 @@ end
25
25
  #uris.threadify(:each_slice, 1) do |slice|
26
26
  HttpReactor::Client.new(requests) do |response, context|
27
27
  puts "Response: #{response.status_line.status_code}"
28
+ puts "Content length: #{response.body.length}"
28
29
  end
29
30
  #end
30
31
  puts "Processed #{requests.length} feeds"
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{jruby-http-reactor}
5
- s.version = "0.2.0"
5
+ s.version = "0.3.0"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Anthony Eden"]
@@ -24,6 +24,7 @@ Gem::Specification.new do |s|
24
24
  "lib/http_reactor.rb",
25
25
  "lib/http_reactor/client.rb",
26
26
  "lib/http_reactor/request.rb",
27
+ "lib/http_reactor/response.rb",
27
28
  "test/client_test.rb",
28
29
  "test/test_helper.rb",
29
30
  "vendor/httpcore-4.0.1.jar",
data/lib/http_reactor.rb CHANGED
@@ -10,4 +10,5 @@ end
10
10
  $:.unshift(File.dirname(__FILE__))
11
11
 
12
12
  require 'http_reactor/request'
13
+ require 'http_reactor/response'
13
14
  require 'http_reactor/client'
@@ -48,12 +48,16 @@ module HttpReactor #:nodoc:
48
48
  end
49
49
 
50
50
  def handle_response(response, context)
51
- @handler_proc.call(response, context)
51
+ begin
52
+ @handler_proc.call(HttpReactor::Response.new(response), context)
52
53
 
53
- context.setAttribute(RESPONSE_RECEIVED, true)
54
+ context.setAttribute(RESPONSE_RECEIVED, true)
54
55
 
55
- # Signal completion of the request execution
56
- @request_count.count_down()
56
+ # Signal completion of the request execution
57
+ @request_count.count_down()
58
+ rescue => e
59
+ puts "Error handling response: #{e.message}"
60
+ end
57
61
  end
58
62
  end
59
63
 
@@ -0,0 +1,46 @@
1
+ module HttpReactor #:nodoc:
2
+ # A class that represents an HTTP response which wraps the
3
+ # Java HTTP NIO response object and provides methods for accessing
4
+ # the data using ruby idioms.
5
+ class Response
6
+ def initialize(response_impl)
7
+ @response_impl = response_impl
8
+ end
9
+
10
+ # Delegates to the HTTP NIO response
11
+ def status_line
12
+ @response_impl.status_line
13
+ end
14
+
15
+ # Delegates to the HTTP NIO response
16
+ def entity
17
+ @response_impl.entity
18
+ end
19
+
20
+ def code
21
+ status_line.status_code
22
+ end
23
+
24
+ # Get the response content type
25
+ def content_type
26
+ @response_impl.entity.content_type.value
27
+ end
28
+
29
+ # Get the response content length
30
+ def content_length
31
+ @response_impl.entity.content_length
32
+ end
33
+
34
+ # Get the body text
35
+ def body
36
+ begin
37
+ io = Java.java_to_ruby(
38
+ org.jruby.RubyIO.new(JRuby.runtime, entity.content).java_object
39
+ )
40
+ io.read
41
+ rescue Exception => e
42
+ puts "Error in Response#body: #{e.message}"
43
+ end
44
+ end
45
+ end
46
+ end
data/test/client_test.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require File.dirname(__FILE__) + '/test_helper'
2
2
  require 'uri'
3
+ require 'mime/types'
3
4
 
4
5
  class ClientTest < Test::Unit::TestCase
5
6
  def requests
@@ -18,15 +19,32 @@ class ClientTest < Test::Unit::TestCase
18
19
  end
19
20
 
20
21
  def test_proc
21
- handler = Proc.new { |response, context|
22
- puts "Proc Response: #{response.status_line.status_code}"
23
- }
22
+ handler = Proc.new do |response, context|
23
+ assert_equal 200, response.code
24
+ assert response.body.length > 0
25
+ mime_type = MIME::Types[response.content_type].first
26
+ assert_equal "text/html", mime_type.content_type
27
+ end
24
28
  HttpReactor::Client.new(requests, handler)
25
29
  end
26
30
 
27
31
  def test_block
28
32
  HttpReactor::Client.new(requests) do |response, context|
29
- puts "Block Response: #{response.status_line.status_code}"
33
+ assert_equal 200, response.status_line.status_code
34
+ assert_equal 200, response.code
35
+ mime_type = MIME::Types[response.content_type].first
36
+ assert_equal "text/html", mime_type.content_type
37
+ puts "request ur: #{context.getAttribute('http_target_request').uri}"
38
+ puts "content-length: #{response.content_length}"
39
+ assert response.body.length > 0
40
+ end
41
+ end
42
+
43
+ def test_body
44
+ HttpReactor::Client.new(requests) do |response, context|
45
+ if response.code == 200
46
+ puts response.body
47
+ end
30
48
  end
31
49
  end
32
50
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: aeden-jruby-http-reactor
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Anthony Eden
@@ -33,6 +33,7 @@ files:
33
33
  - lib/http_reactor.rb
34
34
  - lib/http_reactor/client.rb
35
35
  - lib/http_reactor/request.rb
36
+ - lib/http_reactor/response.rb
36
37
  - test/client_test.rb
37
38
  - test/test_helper.rb
38
39
  - vendor/httpcore-4.0.1.jar