Skip to content

Commit

Permalink
cleaned up #get, #post and #put. removed their class method pendants …
Browse files Browse the repository at this point in the history
…since we don't want to be responsible for creating objects.
  • Loading branch information
apotonick committed Dec 19, 2011
1 parent aa734c8 commit 107daca
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 51 deletions.
44 changes: 17 additions & 27 deletions lib/roar/representer/feature/http_verbs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,44 +18,34 @@ def get(url, format) # TODO: test me!
representation = get_uri(url, format).body
deserialize(representation)
end

def post(url, body, format)
representation = post_uri(url, body, format).body
deserialize(representation)
end


def put(url, body, format)
representation = put_uri(url, body, format).body
deserialize(representation)
end
end

# Serializes the object, POSTs it to +url+ with +format+, deserializes the returned document
# and updates properties accordingly.
def post(url, format)
self.class.post(url, serialize, format)
end
def post!(*args)
rep = post(*args) # TODO: make this better.

self.class.representable_attrs.each do |definition|

send(definition.setter, rep.public_send(definition.getter))
end # TODO: this sucks. do this with #properties and #replace_properties.
# DISCUSS: what if a redirect happens here?
document = http.post_uri(url, serialize, format).body
deserialize(document)
end

def get!(url, format) # FIXME: abstract to #replace_properties
rep = self.class.get(url, format) # TODO: where's the format? why do we need class here?

self.class.representable_attrs.each do |definition|
send(definition.setter, rep.public_send(definition.getter))
end # TODO: this sucks. do this with #properties and #replace_properties.
# GETs +url+ with +format+, deserializes the returned document and updates properties accordingly.
def get(url, format)
document = http.get_uri(url, format).body
deserialize(document)
end

# Serializes the object, PUTs it to +url+ with +format+, deserializes the returned document
# and updates properties accordingly.
def put(url, format)
self.class.put(url, serialize, format)
document = http.put_uri(url, serialize, format).body
deserialize(document)
end

# TODO: implement delete, patch.
private
def http
self.class # DISCUSS: might be refering to separate http object soon.
end
end
end
end
Expand Down
7 changes: 5 additions & 2 deletions test/fake_server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class FakeServer < Sinatra::Base
# "<method>patch</method>"
#end

post "/band" do
post "/bands" do
if request.content_type =~ /xml/
%{<band><label>n/a</label><name>Strung Out</name>
<link href="http://search" rel="search" />
Expand All @@ -35,10 +35,13 @@ class FakeServer < Sinatra::Base
end
end

put "/band/strungout" do
put "/bands/strungout" do
%{<band><label>Fat Wreck</label><name>Strung Out</name></band>}
end

get "/bands/slayer" do
%{<band><label>Canadian Maple</label><name>Slayer</name></band>}
end


require Dir.pwd + '/order_representers'
Expand Down
47 changes: 25 additions & 22 deletions test/http_verbs_feature_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,34 +13,37 @@ class Band

describe "HttpVerbs" do
before do
@r = Band.new
@band = Band.new
end

# TODO: assert that Restfulie#post receives the correct document.

it "#post deserializes the incoming representation and returns it" do
@r.name = "Strung Out"
rep = @r.post("http://localhost:9999/band", "application/xml")
assert_equal "Strung Out", rep.name
assert_equal "n/a", rep.label
describe "#get" do
it "updates instance with incoming representation" do
@band.name = "Strung Out"
@band.get("http://localhost:9999/bands/slayer", "application/xml")
assert_equal "Slayer", @band.name
assert_equal "Canadian Maple", @band.label
end
end

it "#post! deserializes the incoming representation and replaces attributes" do
@r.name = "Strung Out"
assert_equal nil, @r.label
@r.post!("http://localhost:9999/band", "application/xml")
assert_equal "Strung Out", @r.name
assert_equal "n/a", @r.label
describe "#post" do
it "updates instance with incoming representation" do
@band.name = "Strung Out"
assert_equal nil, @band.label

@band.post("http://localhost:9999/bands", "application/xml")
assert_equal "Strung Out", @band.name
assert_equal "n/a", @band.label
end
end



it "#put deserializes the incoming representation and returns it" do
@r.name = "Strung Out"
@r.label = "Fat Wreck"
rep = @r.put("http://localhost:9999/band/strungout", "application/xml")
assert_equal "Strung Out", rep.name
assert_equal "Fat Wreck", rep.label
describe "#put" do
it "updates instance with incoming representation" do
@band.name = "Strung Out"
@band.label = "Fat Wreck"
@band.put("http://localhost:9999/bands/strungout", "application/xml")
assert_equal "Strung Out", @band.name
assert_equal "Fat Wreck", @band.label
end
end
end
end

0 comments on commit 107daca

Please sign in to comment.