Skip to content
Browse files

Completed refactor of RPC to live under Whack-A-Node

  • Loading branch information...
1 parent 05413b8 commit 23f24416d4e2916cbd03160e5b8e9905f2828418 @nielsm committed
View
3 lib/whack_a_node.rb
@@ -1,3 +1,4 @@
require File.expand_path(File.dirname(__FILE__) + '/whack_a_node/proxy')
require File.expand_path(File.dirname(__FILE__) + '/whack_a_node/redirect')
-require File.expand_path(File.dirname(__FILE__) + '/whack_a_node/whacky')
+require File.expand_path(File.dirname(__FILE__) + '/whack_a_node/whacky')
+require File.expand_path(File.dirname(__FILE__) + '/whack_a_node/rpc')
View
82 lib/whack-a-dnode.rb → lib/whack_a_node/rpc.rb
@@ -1,39 +1,40 @@
require 'dnode'
require 'eventmachine'
require 'events'
-class WhackADnode
- def initialize(path="/",host="localhost", port="8820", redirect=false)
- @path = path
- @host = host
- @port = port
- @redirect = redirect
-
- end
-
- def proxy_request
- uri = self.uri
- session = Net::HTTP.new(uri.host, uri.port)
- session.start {|http|
- req = Net::HTTP::Get.new(uri.request_uri)
- body = ''
- res = http.request(req) do |res|
- res.read_body do |segment|
- body << segment
+module WhackANode
+ class Rpc
+ def initialize(path="/",host="localhost", port="8820", redirect=false)
+ @path = path
+ @host = host
+ @port = port
+ @redirect = redirect
+
+ end
+
+ def proxy_request
+ uri = self.uri
+ session = Net::HTTP.new(uri.host, uri.port)
+ session.start {|http|
+ req = Net::HTTP::Get.new(uri.request_uri)
+ body = ''
+ res = http.request(req) do |res|
+ res.read_body do |segment|
+ body << segment
+ end
end
- end
- [res.code, create_response_headers(res), [body]]
+ [res.code, create_response_headers(res), [body]]
}
- end
+ end
+
+ def forward_request
+ [ 302, {'Location'=> uri.to_s }, [] ]
+ end
+
+ def call(env)
+ return @redirect ? forward_request : proxy_request
+ end
- def forward_request
- [ 302, {'Location'=> uri.to_s }, [] ]
- end
-
- def call(env)
- return @redirect ? forward_request : proxy_request
- end
-
private
@@ -83,20 +84,21 @@ def reverse_proxy matcher, url, opts={}
@paths.merge!(matcher => url)
@opts.merge!(opts)
end
-
- def rewrite_env(env)
- env["PORT"] = "8000"
- env
- end
+ def rewrite_env(env)
+ env["PORT"] = "8000"
- def rewrite_response(triplet)
- status, headers, body = triplet
+ env
+ end
- headers["X-Foo"] = "Bar"
+ def rewrite_response(triplet)
+ status, headers, body = triplet
- triplet
- end
+ headers["X-Foo"] = "Bar"
+
+ triplet
+ end
-end
+ end
+end
View
6 spec/whack-a-dnode_spec.rb → spec/rpc_spec.rb
@@ -1,17 +1,17 @@
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
require 'rack/test'
-describe "WhackADnode" do
+describe "Rpc" do
include Rack::Test::Methods
include WebMock::API
- class DwhackyTest < WhackADnode
+ class DwhackyTest < WhackANode::Rpc
def rewrite_env(env)
env['PORT'] = 90220
end
end
def app
- WhackADnode.new
+ WhackANode::Rpc.new
end
it "should have a port of 90210" do
View
1 spec/spec_helper.rb
@@ -3,7 +3,6 @@
require 'rspec'
require 'rack/test'
require 'whack-a-node'
-require 'whack-a-dnode'
require 'webmock/rspec'
# Requires supporting files with custom matchers and macros, etc,
# in ./support/ and its subdirectories.

0 comments on commit 23f2441

Please sign in to comment.
Something went wrong with that request. Please try again.