mirror of
https://github.com/rest-client/rest-client.git
synced 2022-11-09 13:49:40 -05:00
add support for options. Closes #37
This commit is contained in:
parent
add35e298a
commit
e61a2bd842
5 changed files with 23 additions and 8 deletions
|
@ -1,6 +1,7 @@
|
|||
# 1.6.1
|
||||
|
||||
- add response body in Exception#inspect
|
||||
- add support for RestClient.options
|
||||
|
||||
# 1.6.0
|
||||
|
||||
|
|
|
@ -84,6 +84,10 @@ module RestClient
|
|||
Request.execute(:method => :head, :url => url, :headers => headers, &block)
|
||||
end
|
||||
|
||||
def self.options(url, headers={}, &block)
|
||||
Request.execute(:method => :options, :url => url, :headers => headers, &block)
|
||||
end
|
||||
|
||||
class << self
|
||||
attr_accessor :proxy
|
||||
end
|
||||
|
|
|
@ -41,7 +41,7 @@ module RestClient
|
|||
self
|
||||
elsif [301, 302, 307].include? code
|
||||
unless [:get, :head].include? args[:method]
|
||||
raise Exceptions::EXCEPTIONS_MAP[code], self
|
||||
raise Exceptions::EXCEPTIONS_MAP[code].new(self, code)
|
||||
else
|
||||
follow_redirection(request, &block)
|
||||
end
|
||||
|
@ -50,9 +50,9 @@ module RestClient
|
|||
args.delete :payload
|
||||
follow_redirection(request, &block)
|
||||
elsif Exceptions::EXCEPTIONS_MAP[code]
|
||||
raise Exceptions::EXCEPTIONS_MAP[code], self
|
||||
raise Exceptions::EXCEPTIONS_MAP[code].new(self, code)
|
||||
else
|
||||
raise RequestFailed, self
|
||||
raise RequestFailed self
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -82,8 +82,9 @@ module RestClient
|
|||
class Exception < RuntimeError
|
||||
attr_accessor :message, :response
|
||||
|
||||
def initialize response = nil
|
||||
def initialize response = nil, initial_response_code = nil
|
||||
@response = response
|
||||
@initial_response_code = initial_response_code
|
||||
|
||||
# compatibility: this make the exception behave like a Net::HTTPResponse
|
||||
response.extend ResponseForException if response
|
||||
|
@ -91,7 +92,11 @@ module RestClient
|
|||
|
||||
def http_code
|
||||
# return integer for compatibility
|
||||
@response.code.to_i if @response
|
||||
if @response
|
||||
@response.code.to_i
|
||||
else
|
||||
@initial_response_code
|
||||
end
|
||||
end
|
||||
|
||||
def http_body
|
||||
|
@ -99,7 +104,7 @@ module RestClient
|
|||
end
|
||||
|
||||
def inspect
|
||||
"#{message} #{http_code}: #{http_body}"
|
||||
"#{message}: #{http_body}"
|
||||
end
|
||||
|
||||
def to_s
|
||||
|
@ -135,7 +140,7 @@ module RestClient
|
|||
# Compatibility
|
||||
superclass = ([304, 401, 404].include? code) ? ExceptionWithResponse : RequestFailed
|
||||
klass = Class.new(superclass) do
|
||||
send(:define_method, :message) {message}
|
||||
send(:define_method, :message) {"#{http_code ? "#{http_code} " : ''}#{message}"}
|
||||
end
|
||||
klass_constant = const_set message.delete(' \-\''), klass
|
||||
Exceptions::EXCEPTIONS_MAP[code] = klass_constant
|
||||
|
@ -162,7 +167,7 @@ module RestClient
|
|||
|
||||
class SSLCertificateNotVerified < Exception
|
||||
def initialize(message)
|
||||
super(nil)
|
||||
super nil, nil
|
||||
self.message = message
|
||||
end
|
||||
end
|
||||
|
|
|
@ -26,6 +26,11 @@ describe RestClient do
|
|||
RestClient::Request.should_receive(:execute).with(:method => :head, :url => 'http://some/resource', :headers => {})
|
||||
RestClient.head('http://some/resource')
|
||||
end
|
||||
|
||||
it "OPTIONS" do
|
||||
RestClient::Request.should_receive(:execute).with(:method => :options, :url => 'http://some/resource', :headers => {})
|
||||
RestClient.options('http://some/resource')
|
||||
end
|
||||
end
|
||||
|
||||
describe "logging" do
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue