Skip to content

Commit

Permalink
Added PATCH
Browse files Browse the repository at this point in the history
  • Loading branch information
Isaac Sanders committed Apr 16, 2012
1 parent 95f7cb3 commit 7ab6641
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 2 deletions.
11 changes: 10 additions & 1 deletion lib/httparty.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def self.included(base)
end

# == Common Request Options
# Request methods (get, post, put, delete, head, options) all take a common set of options. These are:
# Request methods (get, post, patch, put, delete, head, options) all take a common set of options. These are:
#
# [:+body+:] Body of the request. If passed a Hash, will try to normalize it first, by default passing it to ActiveSupport::to_params. Any other kind of object will get used as-is.
# [:+http_proxyaddr+:] Address of proxy server to use.
Expand Down Expand Up @@ -363,6 +363,11 @@ def post(path, options={}, &block)
perform_request Net::HTTP::Post, path, options, &block
end

# Perform a PATCH request to a path
def patch(path, options={}, &block)
perform_request Net::HTTP::Patch, path, options, &block
end

# Perform a PUT request to a path
def put(path, options={}, &block)
perform_request Net::HTTP::Put, path, options, &block
Expand Down Expand Up @@ -431,6 +436,10 @@ def self.post(*args, &block)
Basement.post(*args, &block)
end

def self.patch(*args, &block)
Basement.patch(*args, &block)
end

def self.put(*args, &block)
Basement.put(*args, &block)
end
Expand Down
3 changes: 2 additions & 1 deletion lib/httparty/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ class Request #:nodoc:
SupportedHTTPMethods = [
Net::HTTP::Get,
Net::HTTP::Post,
Net::HTTP::Patch,
Net::HTTP::Put,
Net::HTTP::Delete,
Net::HTTP::Head,
Expand Down Expand Up @@ -260,7 +261,7 @@ def format_from_mimetype(mimetype)

def validate
raise HTTParty::RedirectionTooDeep.new(last_response), 'HTTP redirects too deep' if options[:limit].to_i <= 0
raise ArgumentError, 'only get, post, put, delete, head, and options methods are supported' unless SupportedHTTPMethods.include?(http_method)
raise ArgumentError, 'only get, post, patch, put, delete, head, and options methods are supported' unless SupportedHTTPMethods.include?(http_method)
raise ArgumentError, ':headers must be a hash' if options[:headers] && !options[:headers].is_a?(Hash)
raise ArgumentError, 'only one authentication method, :basic_auth or :digest_auth may be used at a time' if options[:basic_auth] && options[:digest_auth]
raise ArgumentError, ':basic_auth must be a hash' if options[:basic_auth] && !options[:basic_auth].is_a?(Hash)
Expand Down
5 changes: 5 additions & 0 deletions spec/httparty/request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,11 @@
@request.perform.should == {"hash" => {"foo" => "bar"}}
end

it "should be handled by PATCH transparently" do
@request.http_method = Net::HTTP::Patch
@request.perform.should == {"hash" => {"foo" => "bar"}}
end

it "should be handled by PUT transparently" do
@request.http_method = Net::HTTP::Put
@request.perform.should == {"hash" => {"foo" => "bar"}}
Expand Down
6 changes: 6 additions & 0 deletions spec/httparty_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,12 @@ class MyParser < HTTParty::Parser
end.should raise_error(HTTParty::RedirectionTooDeep) {|e| e.response.body.should == 'first redirect'}
end

it "should fail with redirected PATCH" do
lambda do
@klass.patch('/foo', :no_follow => true)
end.should raise_error(HTTParty::RedirectionTooDeep) {|e| e.response.body.should == 'first redirect'}
end

it "should fail with redirected DELETE" do
lambda do
@klass.delete('/foo', :no_follow => true)
Expand Down

0 comments on commit 7ab6641

Please sign in to comment.