Skip to content

Commit 7ab6641

Browse files
author
Isaac Sanders
committed
Added PATCH
1 parent 95f7cb3 commit 7ab6641

4 files changed

Lines changed: 23 additions & 2 deletions

File tree

lib/httparty.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def self.included(base)
3636
end
3737

3838
# == Common Request Options
39-
# Request methods (get, post, put, delete, head, options) all take a common set of options. These are:
39+
# Request methods (get, post, patch, put, delete, head, options) all take a common set of options. These are:
4040
#
4141
# [:+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.
4242
# [:+http_proxyaddr+:] Address of proxy server to use.
@@ -363,6 +363,11 @@ def post(path, options={}, &block)
363363
perform_request Net::HTTP::Post, path, options, &block
364364
end
365365

366+
# Perform a PATCH request to a path
367+
def patch(path, options={}, &block)
368+
perform_request Net::HTTP::Patch, path, options, &block
369+
end
370+
366371
# Perform a PUT request to a path
367372
def put(path, options={}, &block)
368373
perform_request Net::HTTP::Put, path, options, &block
@@ -431,6 +436,10 @@ def self.post(*args, &block)
431436
Basement.post(*args, &block)
432437
end
433438

439+
def self.patch(*args, &block)
440+
Basement.patch(*args, &block)
441+
end
442+
434443
def self.put(*args, &block)
435444
Basement.put(*args, &block)
436445
end

lib/httparty/request.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ class Request #:nodoc:
33
SupportedHTTPMethods = [
44
Net::HTTP::Get,
55
Net::HTTP::Post,
6+
Net::HTTP::Patch,
67
Net::HTTP::Put,
78
Net::HTTP::Delete,
89
Net::HTTP::Head,
@@ -260,7 +261,7 @@ def format_from_mimetype(mimetype)
260261

261262
def validate
262263
raise HTTParty::RedirectionTooDeep.new(last_response), 'HTTP redirects too deep' if options[:limit].to_i <= 0
263-
raise ArgumentError, 'only get, post, put, delete, head, and options methods are supported' unless SupportedHTTPMethods.include?(http_method)
264+
raise ArgumentError, 'only get, post, patch, put, delete, head, and options methods are supported' unless SupportedHTTPMethods.include?(http_method)
264265
raise ArgumentError, ':headers must be a hash' if options[:headers] && !options[:headers].is_a?(Hash)
265266
raise ArgumentError, 'only one authentication method, :basic_auth or :digest_auth may be used at a time' if options[:basic_auth] && options[:digest_auth]
266267
raise ArgumentError, ':basic_auth must be a hash' if options[:basic_auth] && !options[:basic_auth].is_a?(Hash)

spec/httparty/request_spec.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,11 @@
456456
@request.perform.should == {"hash" => {"foo" => "bar"}}
457457
end
458458

459+
it "should be handled by PATCH transparently" do
460+
@request.http_method = Net::HTTP::Patch
461+
@request.perform.should == {"hash" => {"foo" => "bar"}}
462+
end
463+
459464
it "should be handled by PUT transparently" do
460465
@request.http_method = Net::HTTP::Put
461466
@request.perform.should == {"hash" => {"foo" => "bar"}}

spec/httparty_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,12 @@ class MyParser < HTTParty::Parser
432432
end.should raise_error(HTTParty::RedirectionTooDeep) {|e| e.response.body.should == 'first redirect'}
433433
end
434434

435+
it "should fail with redirected PATCH" do
436+
lambda do
437+
@klass.patch('/foo', :no_follow => true)
438+
end.should raise_error(HTTParty::RedirectionTooDeep) {|e| e.response.body.should == 'first redirect'}
439+
end
440+
435441
it "should fail with redirected DELETE" do
436442
lambda do
437443
@klass.delete('/foo', :no_follow => true)

0 commit comments

Comments
 (0)