Permalink
Browse files

Merge pull request #51 from seancribbs/reel

Reel
  • Loading branch information...
2 parents aedcfa2 + 82bd60e commit aaefa6d137f8f9452c06e7e3afaa2eb209d101fa @seancribbs seancribbs committed Aug 20, 2012
View
@@ -2,6 +2,8 @@ rvm:
- 1.8.7
- 1.9.2
- 1.9.3
- - rbx
- - jruby
+ - jruby-18mode
+ - jruby-19mode
+ - rbx-18mode
+ - rbx-19mode
bundler_args: --without=guard,docs
View
@@ -6,6 +6,12 @@ gemspec
gem 'bundler'
+group :webservers do
+ gem 'mongrel', '~> 1.2.beta', :platform => [:mri, :rbx]
+ gem 'reel', '>= 0.1.0', :platform => [:ruby_19, :jruby]
+ gem 'hatetepe', '~> 0.5'
+end
+
group :guard do
gem 'guard-rspec'
case RbConfig::CONFIG['host_os']
@@ -1,10 +1,12 @@
+require 'webmachine/adapters/lazy_request_body'
require 'webmachine/adapters/webrick'
module Webmachine
# Contains classes and modules that connect Webmachine to Ruby
# application servers.
module Adapters
autoload :Mongrel, 'webmachine/adapters/mongrel'
+ autoload :Reel, 'webmachine/adapters/reel'
autoload :Hatetepe, 'webmachine/adapters/hatetepe'
end
end
@@ -0,0 +1,32 @@
+
+module Webmachine
+ module Adapters
+ # Wraps a request body so that it can be passed to
+ # {Request} while still lazily evaluating the body.
+ class LazyRequestBody
+ def initialize(request)
+ @request = request
+ end
+
+ # Converts the body to a String so you can work with the entire
+ # thing.
+ def to_s
+ @value ? @value.join : @request.body
+ end
+
+ # Iterates over the body in chunks. If the body has previously
+ # been read, this method can be called again and get the same
+ # sequence of chunks.
+ # @yield [chunk]
+ # @yieldparam [String] chunk a chunk of the request body
+ def each
+ if @value
+ @value.each {|chunk| yield chunk }
+ else
+ @value = []
+ @request.body {|chunk| @value << chunk; yield chunk }
+ end
+ end
+ end # class RequestBody
+ end # module Adapters
+end # module Webmachine
@@ -0,0 +1,41 @@
+require 'reel'
+require 'webmachine/version'
+require 'webmachine/headers'
+require 'webmachine/request'
+require 'webmachine/response'
+require 'webmachine/dispatcher'
+require 'webmachine/chunked_body'
+
+module Webmachine
+ module Adapters
+ class Reel < Adapter
+ def run
+ options = {
+ :port => configuration.port,
+ :host => configuration.ip
+ }.merge(configuration.adapter_options)
+ server = ::Reel::Server.supervise(options[:host], options[:port], &method(:process))
+ trap("INT"){ server.terminate; exit 0 }
+ sleep
+ end
+
+ def process(connection)
+ while wreq = connection.request
+ header = Webmachine::Headers[wreq.headers.dup]
+ requri = URI::HTTP.build(:host => header.fetch('Host').split(':').first,
+ :port => header.fetch('Host').split(':').last.to_i,
+ :path => wreq.url.split('?').first,
+ :query => wreq.url.split('?').last)
+ request = Webmachine::Request.new(wreq.method.to_s.upcase,
+ requri,
+ header,
+ LazyRequestBody.new(wreq))
+ response = Webmachine::Response.new
+ @dispatcher.dispatch(request,response)
+
+ connection.respond ::Reel::Response.new(response.code, response.headers, response.body)
+ end
+ end
+ end
+ end
+end
@@ -35,7 +35,7 @@ def service(wreq, wres)
request = Webmachine::Request.new(wreq.request_method,
wreq.request_uri,
header,
- RequestBody.new(wreq))
+ LazyRequestBody.new(wreq))
response = Webmachine::Response.new
@dispatcher.dispatch(request, response)
wres.status = response.code.to_i
@@ -61,35 +61,6 @@ def service(wreq, wres)
end
end
end # class Server
-
- # Wraps the WEBrick request body so that it can be passed to
- # {Request} while still lazily evaluating the body.
- class RequestBody
- def initialize(request)
- @request = request
- end
-
- # Converts the body to a String so you can work with the entire
- # thing.
- def to_s
- @value ? @value.join : @request.body
- end
-
- # Iterates over the body in chunks. If the body has previously
- # been read, this method can be called again and get the same
- # sequence of chunks.
- # @yield [chunk]
- # @yieldparam [String] chunk a chunk of the request body
- def each
- if @value
- @value.each {|chunk| yield chunk }
- else
- @value = []
- @request.body {|chunk| @value << chunk; yield chunk }
- end
- end
- end # class RequestBody
-
end # module WEBrick
end # module Adapters
end # module Webmachine
@@ -0,0 +1,23 @@
+require 'spec_helper'
+
+if RUBY_VERSION >= "1.9"
+ describe Webmachine::Adapters::Reel do
+ let(:configuration) { Webmachine::Configuration.default }
+ let(:dispatcher) { Webmachine::Dispatcher.new }
+ let(:adapter) do
+ described_class.new(configuration, dispatcher)
+ end
+
+ it 'inherits from Webmachine::Adapter' do
+ adapter.should be_a_kind_of(Webmachine::Adapter)
+ end
+
+ it 'implements #run' do
+ adapter.should respond_to(:run)
+ end
+
+ it 'implements #process' do
+ adapter.should respond_to(:process)
+ end
+ end
+end
@@ -7,7 +7,7 @@
res = Webmachine::Response.new
Webmachine.render_error(404, req, res)
- res.code.should be(404)
+ res.code.should == 404
end
end
end
View
@@ -19,8 +19,6 @@ Gem::Specification.new do |gem|
gem.add_development_dependency(%q<rspec>, ["~> 2.8.0"])
gem.add_development_dependency(%q<yard>, ["~> 0.7.3"])
gem.add_development_dependency(%q<rake>)
- gem.add_development_dependency(%q<mongrel>, ['~>1.2.beta'])
- gem.add_development_dependency(%q<hatetepe>, ['~> 0.5'])
gem.add_development_dependency(%q<rack>)
gem.add_development_dependency(%q<rack-test>)

0 comments on commit aaefa6d

Please sign in to comment.