Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose Webmachine::Application to adapters #154

Merged
merged 1 commit into from
Mar 17, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,5 @@ Gemfile.lock
**/bin
*.rbc
.rvmrc
.SyncID
.SyncIgnore
13 changes: 10 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
rvm:
- 1.9.3
- 2.0.0
- 2.1.0
- ruby-head
- jruby
- rbx
- jruby-head
- rbx-2

matrix:
allow_failures:
- rvm: ruby-head
- rvm: jruby-head

bundler_args: --without guard docs
before_install:
- gem install bundler -v '= 1.5.1'
17 changes: 9 additions & 8 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
require 'rbconfig'

source 'https://rubygems.org'

gemspec

gem 'bundler'
group :development do
gem "yard"
gem "rake"
end

group :test do
gem "rspec"
gem "rack"
end

group :webservers do
gem 'mongrel', '~> 1.2.beta', :platform => [:mri, :rbx]
Expand Down Expand Up @@ -34,8 +40,3 @@ end
platforms :jruby do
gem 'jruby-openssl'
end

platform :rbx do
gem 'rubysl'
gem 'racc'
end
20 changes: 7 additions & 13 deletions lib/webmachine/adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,17 @@ module Webmachine
# @abstract Subclass and override {#run} to implement a custom adapter.
class Adapter

# @return [Webmachine::Configuration] the application's configuration.
attr_reader :configuration
# @return [Webmachine::Application] returns the application
attr_reader :application

# @return [Webmachine::Dispatcher] the application's dispatcher.
attr_reader :dispatcher

# @param [Webmachine::Configuration] configuration the application's
# configuration.
# @param [Webmachine::Dispatcher] dispatcher the application's dispatcher.
def initialize(configuration, dispatcher)
@configuration = configuration
@dispatcher = dispatcher
# @param [Webmachine::Application] application the application
def initialize(application)
@application = application
end

# Create a new adapter and run it.
def self.run(configuration, dispatcher)
new(configuration, dispatcher).run
def self.run(application)
new(application).run
end

# Start the adapter.
Expand Down
6 changes: 3 additions & 3 deletions lib/webmachine/adapters/hatetepe.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ module Adapters
class Hatetepe < Adapter
def options
{
:host => configuration.ip,
:port => configuration.port,
:host => application.configuration.ip,
:port => application.configuration.port,
:app => [
::Hatetepe::Server::Pipeline,
::Hatetepe::Server::KeepAlive,
Expand All @@ -40,7 +40,7 @@ def shutdown

def call(request, &respond)
response = Webmachine::Response.new
dispatcher.dispatch(convert_request(request), response)
application.dispatcher.dispatch(convert_request(request), response)

respond.call(convert_response(response))
end
Expand Down
16 changes: 8 additions & 8 deletions lib/webmachine/adapters/mongrel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ class Mongrel < Adapter
# Starts the Mongrel adapter
def run
defaults = {
:port => configuration.port,
:host => configuration.ip,
:dispatcher => dispatcher
}.merge(configuration.adapter_options)
:port => application.configuration.port,
:host => application.configuration.ip,
:application => application
}.merge(application.configuration.adapter_options)
@config = ::Mongrel::Configurator.new(defaults) do
listener do
uri '/', :handler => Webmachine::Adapters::Mongrel::Handler.new(defaults[:dispatcher])
uri '/', :handler => Webmachine::Adapters::Mongrel::Handler.new(defaults[:application])
end
trap("INT") { stop }
run
Expand All @@ -36,8 +36,8 @@ def shutdown

# A Mongrel handler for Webmachine
class Handler < ::Mongrel::HttpHandler
def initialize(dispatcher)
@dispatcher = dispatcher
def initialize(application)
@application = application
super()
end

Expand All @@ -51,7 +51,7 @@ def process(wreq, wres)
RequestBody.new(wreq))

response = Webmachine::Response.new
@dispatcher.dispatch(request, response)
@application.dispatcher.dispatch(request, response)

begin
wres.status = response.code.to_i
Expand Down
8 changes: 4 additions & 4 deletions lib/webmachine/adapters/rack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ class Rack < Adapter
def run
options = DEFAULT_OPTIONS.merge({
:app => self,
:Port => configuration.port,
:Host => configuration.ip
}).merge(configuration.adapter_options)
:Port => application.configuration.port,
:Host => application.configuration.ip
}).merge(application.configuration.adapter_options)

@server = ::Rack::Server.new(options)
@server.start
Expand All @@ -62,7 +62,7 @@ def call(env)
RequestBody.new(rack_req))

response = Webmachine::Response.new
@dispatcher.dispatch(request, response)
application.dispatcher.dispatch(request, response)

response.headers['Server'] = [Webmachine::SERVER_STRING, "Rack/#{::Rack.version}"].join(" ")

Expand Down
13 changes: 7 additions & 6 deletions lib/webmachine/adapters/reel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ module Adapters
class Reel < Adapter
# Used to override default Reel server options (useful in testing)
DEFAULT_OPTIONS = {}

def run
@options = DEFAULT_OPTIONS.merge({
:port => configuration.port,
:host => configuration.ip
}).merge(configuration.adapter_options)
:port => application.configuration.port,
:host => application.configuration.ip
}).merge(application.configuration.adapter_options)

if extra_verbs = configuration.adapter_options[:extra_verbs]
if extra_verbs = application.configuration.adapter_options[:extra_verbs]
@extra_verbs = Set.new(extra_verbs.map(&:to_s).map(&:upcase))
else
@extra_verbs = Set.new
Expand Down Expand Up @@ -64,8 +64,9 @@ def process(connection)

wm_headers = Webmachine::Headers[request.headers.dup]
wm_request = Webmachine::Request.new(method, uri, wm_headers, request.body)

wm_response = Webmachine::Response.new
@dispatcher.dispatch(wm_request, wm_response)
application.dispatcher.dispatch(wm_request, wm_response)

fixup_headers(wm_response)
fixup_callable_encoder(wm_response)
Expand Down
16 changes: 9 additions & 7 deletions lib/webmachine/adapters/webrick.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ class WEBrick < Adapter
# Starts the WEBrick adapter
def run
options = DEFAULT_OPTIONS.merge({
:Port => configuration.port,
:BindAddress => configuration.ip
}).merge(configuration.adapter_options)
@server = Server.new(dispatcher, options)
:Port => application.configuration.port,
:BindAddress => application.configuration.ip,
:application => application
}).merge(application.configuration.adapter_options)
@server = Server.new(options)
trap("INT") { shutdown }
@server.start
end
Expand All @@ -29,8 +30,8 @@ def shutdown

# WEBRick::HTTPServer that is run by the WEBrick adapter.
class Server < ::WEBrick::HTTPServer
def initialize(dispatcher, options)
@dispatcher = dispatcher
def initialize(options)
@application = options[:application]
super(options)
end

Expand All @@ -42,8 +43,9 @@ def service(wreq, wres)
wreq.request_uri,
header,
LazyRequestBody.new(wreq))

response = Webmachine::Response.new
@dispatcher.dispatch(request, response)
@application.dispatcher.dispatch(request, response)
wres.status = response.code.to_i

headers = response.headers.flattened.reject { |k,v| k == 'Set-Cookie' }
Expand Down
2 changes: 1 addition & 1 deletion lib/webmachine/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def run
# @return an instance of the configured web-server adapter
# @see Adapters
def adapter
@adapter ||= adapter_class.new(configuration, dispatcher)
@adapter ||= adapter_class.new(self)
end

# @return an instance of the configured web-server adapter
Expand Down
14 changes: 8 additions & 6 deletions lib/webmachine/spec/adapter_lint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
attr_accessor :client

before(:all) do
configuration = Webmachine::Configuration.default
dispatcher = Webmachine::Dispatcher.new
dispatcher.add_route ["test"], Test::Resource

@adapter = described_class.new(configuration, dispatcher)
@client = Net::HTTP.new(configuration.ip, configuration.port)
application = Webmachine::Application.new
server = TCPServer.new('0.0.0.0', 0)
application.configuration.port = server.addr[1]
server.close
application.dispatcher.add_route ["test"], Test::Resource

@adapter = described_class.new(application)
@client = Net::HTTP.new(application.configuration.ip, application.configuration.port)

Thread.abort_on_exception = true
@server_thread = Thread.new { @adapter.run }
Expand Down
2 changes: 1 addition & 1 deletion lib/webmachine/translation.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require 'i18n'

I18n.enforce_available_locales = true if I18n.respond_to?(:enforce_available_locales)
I18n.config.load_path << File.expand_path("../locale/en.yml", __FILE__)

module Webmachine
Expand Down
9 changes: 2 additions & 7 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
$LOAD_PATH << File.expand_path("..", __FILE__)
$LOAD_PATH << File.expand_path("../../lib", __FILE__)

require 'rubygems'
require 'webmachine'
require 'rspec'
require "bundler/setup"
Bundler.require :default, :test, :webservers
require 'logger'

RSpec.configure do |config|
config.mock_with :rspec
config.filter_run :focus => true
Expand Down
21 changes: 10 additions & 11 deletions spec/webmachine/adapter_spec.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
require "spec_helper"

describe Webmachine::Adapter do
let(:configuration) { Webmachine::Configuration.default }
let(:dispatcher) { Webmachine::Dispatcher.new }
let(:application) { Webmachine::Application.new }
let(:adapter) do
described_class.new(configuration, dispatcher)
server = TCPServer.new('0.0.0.0', 0)
application.configuration.port = server.addr[1]
server.close

described_class.new(application)
end

describe "#initialize" do
it "stores the provided configuration" do
adapter.configuration.should eql configuration
end

it "stores the provided dispatcher" do
adapter.dispatcher.should eql dispatcher
it "stores the provided application" do
adapter.application.should eql application
end
end

Expand All @@ -22,12 +21,12 @@
adapter = mock(described_class)

described_class.should_receive(:new).
with(configuration, dispatcher).
with(application).
and_return(adapter)

adapter.should_receive(:run)

described_class.run(configuration, dispatcher)
described_class.run(application)
end
end

Expand Down
15 changes: 10 additions & 5 deletions spec/webmachine/adapters/hatetepe_spec.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
require "spec_helper"

examples = proc do
let(:configuration) { Webmachine::Configuration.default }
let(:dispatcher) { Webmachine::Dispatcher.new }
let(:adapter) { described_class.new(configuration, dispatcher) }
let(:application) { Webmachine::Application.new }
let(:adapter) do
server = TCPServer.new('0.0.0.0', 0)
application.configuration.port = server.addr[1]
server.close

described_class.new(application)
end

it "inherits from Webmachine::Adapter" do
adapter.should be_a(Webmachine::Adapter)
Expand All @@ -22,7 +27,7 @@
end

it "builds a string-like and enumerable request body" do
dispatcher.should_receive(:dispatch) do |req, res|
application.dispatcher.should_receive(:dispatch) do |req, res|
req.body.to_s.should eq("hello, world!")
enum_to_s(req.body).should eq("hello, world!")
end
Expand All @@ -31,7 +36,7 @@

shared_examples "enumerable response body" do
before do
dispatcher.stub(:dispatch) {|_, response| response.body = body }
application.dispatcher.stub(:dispatch) {|_, response| response.body = body }
end

it "builds an enumerable response body" do
Expand Down
Loading