rest-client 1.6.0 → 1.6.1.a

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of rest-client might be problematic. Click here for more details.

@@ -100,11 +100,11 @@ A block can be passed to the RestClient method, this block will then be called w
100
100
  Response.return! can be called to invoke the default response's behavior.
101
101
 
102
102
  # Don't raise exceptions but return the response
103
- RestClient.get('http://example.com/resource'){|response, request| response }
103
+ RestClient.get('http://example.com/resource'){|response, request, result| response }
104
104
  ➔ 404 Resource Not Found | text/html 282 bytes
105
105
 
106
106
  # Manage a specific error code
107
- RestClient.get('http://my-rest-service.com/resource'){ |response, request, &block|
107
+ RestClient.get('http://my-rest-service.com/resource'){ |response, request, result, &block|
108
108
  case response.code
109
109
  when 200
110
110
  p "It worked !"
@@ -112,7 +112,7 @@ Response.return! can be called to invoke the default response's behavior.
112
112
  when 423
113
113
  raise SomeCustomExceptionIfYouWant
114
114
  else
115
- response.return!(request, &block)
115
+ response.return!(request, result, &block)
116
116
  end
117
117
  }
118
118
 
@@ -120,11 +120,11 @@ Response.return! can be called to invoke the default response's behavior.
120
120
  # RFC : "If the 301, 302 or 307 status code is received in response to a request other than GET or HEAD,
121
121
  # the user agent MUST NOT automatically redirect the request unless it can be confirmed by the user,
122
122
  # since this might change the conditions under which the request was issued."
123
- RestClient.get('http://my-rest-service.com/resource'){ |response, request, &block|
123
+ RestClient.get('http://my-rest-service.com/resource'){ |response, request, result, &block|
124
124
  if [301, 302, 307].include? response.code
125
- response.follow_redirection(request, &block)
125
+ response.follow_redirection(request, result, &block)
126
126
  else
127
- response.return!(request, &block)
127
+ response.return!(request, result, &block)
128
128
  end
129
129
  }
130
130
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.6.0
1
+ 1.6.1
data/history.md CHANGED
@@ -1,3 +1,12 @@
1
+ # 1.6.1
2
+
3
+ - add response body in Exception#inspect
4
+ - add support for RestClient.options
5
+ - fix tests for 1.9.2 (patch provided by Niko Dittmann)
6
+ - block passing in Resource#[] (patch provided by Niko Dittmann)
7
+ - cookies set in a response should be kept in a redirect
8
+ - HEAD requests should process parameters just like GET (patch provided by Rob Eanes)
9
+
1
10
  # 1.6.0
2
11
 
3
12
  - forgot to include rest-client.rb in the gem
@@ -84,6 +84,10 @@ module RestClient
84
84
  Request.execute(:method => :head, :url => url, :headers => headers, &block)
85
85
  end
86
86
 
87
+ def self.options(url, headers={}, &block)
88
+ Request.execute(:method => :options, :url => url, :headers => headers, &block)
89
+ end
90
+
87
91
  class << self
88
92
  attr_accessor :proxy
89
93
  end
@@ -25,34 +25,29 @@ module RestClient
25
25
  # Hash of cookies extracted from response headers
26
26
  def cookies
27
27
  @cookies ||= (self.headers[:set_cookie] || {}).inject({}) do |out, cookie_content|
28
- CGI::Cookie::parse(cookie_content).each do |key, cookie|
29
- unless ['expires', 'path'].include? key
30
- out[CGI::escape(key)] = cookie.value[0] ? (CGI::escape(cookie.value[0]) || '') : ''
31
- end
32
- end
33
- out
28
+ out.merge parse_cookie(cookie_content)
34
29
  end
35
30
  end
36
31
 
37
32
  # Return the default behavior corresponding to the response code:
38
33
  # the response itself for code in 200..206, redirection for 301, 302 and 307 in get and head cases, redirection for 303 and an exception in other cases
39
- def return! request = nil, &block
34
+ def return! request = nil, result = nil, & block
40
35
  if (200..207).include? code
41
36
  self
42
37
  elsif [301, 302, 307].include? code
43
38
  unless [:get, :head].include? args[:method]
44
- raise Exceptions::EXCEPTIONS_MAP[code], self
39
+ raise Exceptions::EXCEPTIONS_MAP[code].new(self, code)
45
40
  else
46
- follow_redirection(request, &block)
41
+ follow_redirection(request, result, & block)
47
42
  end
48
43
  elsif code == 303
49
44
  args[:method] = :get
50
45
  args.delete :payload
51
- follow_redirection(request, &block)
46
+ follow_redirection(request, result, & block)
52
47
  elsif Exceptions::EXCEPTIONS_MAP[code]
53
- raise Exceptions::EXCEPTIONS_MAP[code], self
48
+ raise Exceptions::EXCEPTIONS_MAP[code].new(self, code)
54
49
  else
55
- raise RequestFailed, self
50
+ raise RequestFailed self
56
51
  end
57
52
  end
58
53
 
@@ -65,7 +60,7 @@ module RestClient
65
60
  end
66
61
 
67
62
  # Follow a redirection
68
- def follow_redirection request = nil, &block
63
+ def follow_redirection request = nil, result = nil, & block
69
64
  url = headers[:location]
70
65
  if url !~ /^http/
71
66
  url = URI.parse(args[:url]).merge(url).to_s
@@ -75,6 +70,10 @@ module RestClient
75
70
  args[:password] = request.password
76
71
  args[:user] = request.user
77
72
  args[:headers] = request.headers
73
+ # pass any cookie set in the result
74
+ if result && result['set-cookie']
75
+ args[:headers][:cookies] = (args[:headers][:cookies] || {}).merge(parse_cookie(result['set-cookie']))
76
+ end
78
77
  end
79
78
  Request.execute args, &block
80
79
  end
@@ -85,5 +84,19 @@ module RestClient
85
84
  out
86
85
  end
87
86
  end
87
+
88
+ private
89
+
90
+ # Parse a cookie value and return its content in an Hash
91
+ def parse_cookie cookie_content
92
+ out = {}
93
+ CGI::Cookie::parse(cookie_content).each do |key, cookie|
94
+ unless ['expires', 'path'].include? key
95
+ out[CGI::escape(key)] = cookie.value[0] ? (CGI::escape(cookie.value[0]) || '') : ''
96
+ end
97
+ end
98
+ out
99
+ end
88
100
  end
101
+
89
102
  end
@@ -82,8 +82,9 @@ module RestClient
82
82
  class Exception < RuntimeError
83
83
  attr_accessor :message, :response
84
84
 
85
- def initialize response = nil
85
+ def initialize response = nil, initial_response_code = nil
86
86
  @response = response
87
+ @initial_response_code = initial_response_code
87
88
 
88
89
  # compatibility: this make the exception behave like a Net::HTTPResponse
89
90
  response.extend ResponseForException if response
@@ -91,7 +92,11 @@ module RestClient
91
92
 
92
93
  def http_code
93
94
  # return integer for compatibility
94
- @response.code.to_i if @response
95
+ if @response
96
+ @response.code.to_i
97
+ else
98
+ @initial_response_code
99
+ end
95
100
  end
96
101
 
97
102
  def http_body
@@ -99,7 +104,11 @@ module RestClient
99
104
  end
100
105
 
101
106
  def inspect
102
- "#{self.class} : #{http_code} #{message}"
107
+ "#{message}: #{http_body}"
108
+ end
109
+
110
+ def to_s
111
+ inspect
103
112
  end
104
113
 
105
114
  end
@@ -131,7 +140,7 @@ module RestClient
131
140
  # Compatibility
132
141
  superclass = ([304, 401, 404].include? code) ? ExceptionWithResponse : RequestFailed
133
142
  klass = Class.new(superclass) do
134
- send(:define_method, :message) {message}
143
+ send(:define_method, :message) {"#{http_code ? "#{http_code} " : ''}#{message}"}
135
144
  end
136
145
  klass_constant = const_set message.delete(' \-\''), klass
137
146
  Exceptions::EXCEPTIONS_MAP[code] = klass_constant
@@ -158,7 +167,7 @@ module RestClient
158
167
 
159
168
  class SSLCertificateNotVerified < Exception
160
169
  def initialize(message)
161
- super(nil)
170
+ super nil, nil
162
171
  self.message = message
163
172
  end
164
173
  end
@@ -62,7 +62,7 @@ module RestClient
62
62
 
63
63
  # Extract the query parameters for get request and append them to the url
64
64
  def process_get_params url, headers
65
- if method == :get
65
+ if [:get, :head].include? method
66
66
  get_params = {}
67
67
  headers.delete_if do |key, value|
68
68
  if 'params' == key.to_s.downcase && value.is_a?(Hash)
@@ -215,9 +215,9 @@ module RestClient
215
215
  end
216
216
 
217
217
  if block_given?
218
- block.call(response, self, & block)
218
+ block.call(response, self, res, & block)
219
219
  else
220
- response.return!(self, & block)
220
+ response.return!(self, res, & block)
221
221
  end
222
222
 
223
223
  end
@@ -130,8 +130,13 @@ module RestClient
130
130
  # comments = first_post['comments']
131
131
  # comments.post 'Hello', :content_type => 'text/plain'
132
132
  #
133
- def [](suburl)
134
- self.class.new(concat_urls(url, suburl), options)
133
+ def [](suburl, &new_block)
134
+ case
135
+ when block_given? then self.class.new(concat_urls(url, suburl), options, &new_block)
136
+ when block then self.class.new(concat_urls(url, suburl), options, &block)
137
+ else
138
+ self.class.new(concat_urls(url, suburl), options)
139
+ end
135
140
  end
136
141
 
137
142
  def concat_urls(url, suburl) # :nodoc:
@@ -1,4 +1,4 @@
1
- require File.dirname(__FILE__) + '/base'
1
+ require File.join( File.dirname(File.expand_path(__FILE__)), 'base')
2
2
 
3
3
  describe RestClient::AbstractResponse do
4
4
 
@@ -1,3 +1,9 @@
1
+ def is_ruby_19?
2
+ RUBY_VERSION == '1.9.1' or RUBY_VERSION == '1.9.2'
3
+ end
4
+
5
+ Encoding.default_internal = Encoding.default_external = "ASCII-8BIT" if is_ruby_19?
6
+
1
7
  require 'rubygems'
2
8
  require 'spec'
3
9
 
@@ -1,4 +1,4 @@
1
- require File.dirname(__FILE__) + '/base'
1
+ require File.join( File.dirname(File.expand_path(__FILE__)), 'base')
2
2
 
3
3
  require 'webmock/rspec'
4
4
  include WebMock
@@ -1,4 +1,4 @@
1
- require File.dirname(__FILE__) + '/../base'
1
+ require File.join( File.dirname(File.expand_path(__FILE__)), '../base')
2
2
 
3
3
  describe RestClient::Request do
4
4
  describe "ssl verification" do
@@ -1,4 +1,4 @@
1
- require File.dirname(__FILE__) + '/base'
1
+ require File.join( File.dirname(File.expand_path(__FILE__)), 'base')
2
2
 
3
3
  require 'webmock/rspec'
4
4
  include WebMock
@@ -1,4 +1,4 @@
1
- require File.dirname(__FILE__) + "/base"
1
+ require File.join( File.dirname(File.expand_path(__FILE__)), 'base')
2
2
 
3
3
  describe RestClient::Payload do
4
4
  context "A regular Payload" do
@@ -1,4 +1,4 @@
1
- require File.dirname(__FILE__) + '/base'
1
+ require File.join( File.dirname(File.expand_path(__FILE__)), 'base')
2
2
 
3
3
  describe RestClient::RawResponse do
4
4
  before do
@@ -1,4 +1,4 @@
1
- require File.dirname(__FILE__) + '/base'
1
+ require File.join( File.dirname(File.expand_path(__FILE__)), 'base')
2
2
 
3
3
  require 'webmock/rspec'
4
4
  include WebMock
@@ -1,4 +1,4 @@
1
- require File.dirname(__FILE__) + '/base'
1
+ require File.join( File.dirname(File.expand_path(__FILE__)), 'base')
2
2
 
3
3
  require 'webmock/rspec'
4
4
  include WebMock
@@ -1,4 +1,4 @@
1
- require File.dirname(__FILE__) + '/base'
1
+ require File.join( File.dirname(File.expand_path(__FILE__)), 'base')
2
2
 
3
3
  require 'webmock/rspec'
4
4
  include WebMock
@@ -72,6 +72,31 @@ describe RestClient::Resource do
72
72
  parent['posts'].password.should == 'password'
73
73
  end
74
74
 
75
+ it "passes a given block to subresources" do
76
+ block = Proc.new{|r| r}
77
+ parent = RestClient::Resource.new('http://example.com', &block)
78
+ parent['posts'].block.should == block
79
+ end
80
+
81
+ it "the block should be overrideable" do
82
+ block1 = Proc.new{|r| r}
83
+ block2 = Proc.new{|r| r}
84
+ parent = RestClient::Resource.new('http://example.com', &block1)
85
+ # parent['posts', &block2].block.should == block2 # ruby 1.9 syntax
86
+ parent.send(:[], 'posts', &block2).block.should == block2
87
+ end
88
+
89
+ it "the block should be overrideable in ruby 1.9 syntax" do
90
+ block = Proc.new{|r| r}
91
+ parent = RestClient::Resource.new('http://example.com', &block)
92
+ r19_syntax = %q{
93
+ parent['posts', &->(r){r}].block.should_not == block
94
+ }
95
+ if is_ruby_19?
96
+ eval(r19_syntax)
97
+ end
98
+ end
99
+
75
100
  it "prints its url with to_s" do
76
101
  RestClient::Resource.new('x').to_s.should == 'x'
77
102
  end
@@ -1,4 +1,4 @@
1
- require File.dirname(__FILE__) + '/base'
1
+ require File.join( File.dirname(File.expand_path(__FILE__)), 'base')
2
2
 
3
3
  require 'webmock/rspec'
4
4
  include WebMock
@@ -90,6 +90,12 @@ describe RestClient::Response do
90
90
  RestClient::Request.execute(:url => 'http://some/resource', :method => :get, :user => 'foo', :password => 'bar', :headers => {:accept => :json}).body.should == 'Foo'
91
91
  end
92
92
 
93
+ it "follows a redirection and keep the cookies" do
94
+ stub_request(:get, 'http://some/resource').to_return(:body => '', :status => 301, :headers => {'Set-Cookie' => CGI::Cookie.new('Foo', 'Bar'), 'Location' => 'http://new/resource', })
95
+ stub_request(:get, 'http://new/resource').with(:headers => {'Cookie' => 'Foo=Bar'}).to_return(:body => 'Qux')
96
+ RestClient::Request.execute(:url => 'http://some/resource', :method => :get).body.should == 'Qux'
97
+ end
98
+
93
99
  it "doesn't follow a 301 when the request is a post" do
94
100
  net_http_res = mock('net http response', :code => 301)
95
101
  response = RestClient::Response.create('abc', net_http_res, {:method => :post})
@@ -1,4 +1,4 @@
1
- require File.dirname(__FILE__) + '/base'
1
+ require File.join( File.dirname(File.expand_path(__FILE__)), 'base')
2
2
 
3
3
  describe RestClient do
4
4
  describe "API" do
@@ -26,6 +26,11 @@ describe RestClient do
26
26
  RestClient::Request.should_receive(:execute).with(:method => :head, :url => 'http://some/resource', :headers => {})
27
27
  RestClient.head('http://some/resource')
28
28
  end
29
+
30
+ it "OPTIONS" do
31
+ RestClient::Request.should_receive(:execute).with(:method => :options, :url => 'http://some/resource', :headers => {})
32
+ RestClient.options('http://some/resource')
33
+ end
29
34
  end
30
35
 
31
36
  describe "logging" do
metadata CHANGED
@@ -1,13 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rest-client
3
3
  version: !ruby/object:Gem::Version
4
- hash: 15
5
- prerelease: false
4
+ hash: 14
5
+ prerelease: true
6
6
  segments:
7
7
  - 1
8
8
  - 6
9
- - 0
10
- version: 1.6.0
9
+ - 1
10
+ - a
11
+ version: 1.6.1.a
11
12
  platform: ruby
12
13
  authors:
13
14
  - Adam Wiggins
@@ -16,7 +17,7 @@ autorequire:
16
17
  bindir: bin
17
18
  cert_chain: []
18
19
 
19
- date: 2010-06-28 00:00:00 +02:00
20
+ date: 2010-07-24 00:00:00 +02:00
20
21
  default_executable: restclient
21
22
  dependencies:
22
23
  - !ruby/object:Gem::Dependency