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

make format argument of HttpVerbs methods optional #37

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
21 changes: 16 additions & 5 deletions lib/roar/representer/feature/http_verbs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,37 +32,48 @@ def transport_engine

# Serializes the object, POSTs it to +url+ with +format+, deserializes the returned document
# and updates properties accordingly.
def post(url, format)
def post(url, format = nil)
format ||= format_from_url(url)
response = http.post_uri(url, serialize, format)
handle_response(response)
end

# GETs +url+ with +format+, deserializes the returned document and updates properties accordingly.
def get(url, format)
def get(url, format = nil)
format ||= format_from_url(url)
response = http.get_uri(url, format)
handle_response(response)
end

# Serializes the object, PUTs it to +url+ with +format+, deserializes the returned document
# and updates properties accordingly.
def put(url, format)
def put(url, format = nil)
format ||= format_from_url(url)
response = http.put_uri(url, serialize, format)
handle_response(response)
self
end

def patch(url, format)
def patch(url, format = nil)
format ||= format_from_url(url)
response = http.patch_uri(url, serialize, format)
handle_response(response)
self
end

def delete(url, format)
def delete(url, format = nil)
format ||= format_from_url(url)
http.delete_uri(url, format)
self
end

private
def format_from_url url
extension = File.extname(url)[1..-1]
raise ArgumentError.new("Format can not read from Url '#{url}', the Url need's an extension or the format must set manually!") if extension.nil? || extension.empty?
"application/#{extension}"
end

def handle_response(response)
document = response.body
deserialize(document)
Expand Down
4 changes: 4 additions & 0 deletions test/fake_server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ def consume_band
{:name => "Slayer", :label => "Canadian Maple"}.to_json
end

get "/bands/fake.json" do
{:name => "Fake", :label => request.env["CONTENT_TYPE"]}.to_json
end

delete '/banks/metallica' do
status 204
end
Expand Down
14 changes: 14 additions & 0 deletions test/http_verbs_feature_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,20 @@ class Band
end
end

describe "#format_from_url" do
it "returns the format of the extention" do
@band.get "http://roar.example.com/bands/fake.json"
assert_equal 'Fake', @band.name
assert_equal 'application/json', @band.label
end

it "raise an error" do
assert_raises(ArgumentError) do
@band.get "http://roar.example.com/bands/fake"
end
end
end

# HEAD, OPTIONs?

end
Expand Down