Permalink
Browse files

Transport now contains class methods, only. should be considered a pr…

…ivate module and not a feature.
  • Loading branch information...
1 parent 0f624a8 commit cb05b35848e4b17b5a8be614e68512bdcbf91c17 @apotonick apotonick committed Dec 19, 2011
Showing with 36 additions and 37 deletions.
  1. +31 −29 lib/roar/representer/feature/transport.rb
  2. +5 −8 test/transport_test.rb
@@ -6,36 +6,38 @@ module Representer
module Feature
# Implements the HTTP verbs with Net::HTTP.
module Transport
- # TODO: generically handle return codes/let Restfulie do it.
- def get_uri(uri, as)
- do_request(Net::HTTP::Get, uri, as)
- end
-
- def post_uri(uri, body, as)
- do_request(Net::HTTP::Post, uri, as)
- end
-
- def put_uri(uri, body, as)
- do_request(Net::HTTP::Put, uri, as)
- end
-
- def patch_uri(uri, body, as)
- do_request(Net::HTTP::Patch, uri, as)
- end
+ class << self
+ # TODO: generically handle return codes/let Restfulie do it.
+ def get_uri(uri, as)
+ do_request(Net::HTTP::Get, uri, as)
+ end
+
+ def post_uri(uri, body, as)
+ do_request(Net::HTTP::Post, uri, as)
+ end
+
+ def put_uri(uri, body, as)
+ do_request(Net::HTTP::Put, uri, as)
+ end
+
+ def patch_uri(uri, body, as)
+ do_request(Net::HTTP::Patch, uri, as)
+ end
+
+ def delete_uri(uri, as)
+ do_request(Net::HTTP::Delete, uri, as)
+ end
- def delete_uri(uri, as)
- do_request(Net::HTTP::Delete, uri, as)
- end
-
- private
- def do_request(what, uri, as, body="")
- # DISCUSS: can this be made easier?
- uri = URI.parse(uri)
- http = Net::HTTP.new(uri.host, uri.port)
- req = what.new(uri.request_uri)
- req.content_type = as
- req.body = body if body
- http.request(req)
+ private
+ def do_request(what, uri, as, body="")
+ # DISCUSS: can this be made easier?
+ uri = URI.parse(uri)
+ http = Net::HTTP.new(uri.host, uri.port)
+ req = what.new(uri.request_uri)
+ req.content_type = as
+ req.body = body if body
+ http.request(req)
+ end
end
end
end
View
@@ -4,26 +4,23 @@
class TransportTest < MiniTest::Spec
describe "Transport" do
before do
- @klass = Class.new(Object) do
- include Roar::Representer::Feature::Transport
- end
- @o = @klass.new
+ @transport = Roar::Representer::Feature::Transport
end
it "#get_uri returns response" do
- assert_equal "<method>get</method>", @o.get_uri("http://localhost:9999/method", "application/xml").body
+ assert_equal "<method>get</method>", @transport.get_uri("http://localhost:9999/method", "application/xml").body
end
it "#post_uri returns response" do
- assert_equal "<method>post</method>", @o.post_uri("http://localhost:9999/method", "booty", "application/xml").body
+ assert_equal "<method>post</method>", @transport.post_uri("http://localhost:9999/method", "booty", "application/xml").body
end
it "#put_uri returns response" do
- assert_equal "<method>put</method>", @o.put_uri("http://localhost:9999/method", "booty", "application/xml").body
+ assert_equal "<method>put</method>", @transport.put_uri("http://localhost:9999/method", "booty", "application/xml").body
end
it "#delete_uri returns response" do
- assert_equal "<method>delete</method>", @o.delete_uri("http://localhost:9999/method", "application/xml").body
+ assert_equal "<method>delete</method>", @transport.delete_uri("http://localhost:9999/method", "application/xml").body
end
# TODO: how to get PATCH into Sinatra?

0 comments on commit cb05b35

Please sign in to comment.