rest-client 0.5.1 → 0.6

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.

data/Rakefile CHANGED
@@ -31,7 +31,7 @@ require 'rake/gempackagetask'
31
31
  require 'rake/rdoctask'
32
32
  require 'fileutils'
33
33
 
34
- version = "0.5.1"
34
+ version = "0.6"
35
35
  name = "rest-client"
36
36
 
37
37
  spec = Gem::Specification.new do |s|
@@ -48,6 +48,7 @@ spec = Gem::Specification.new do |s|
48
48
  s.has_rdoc = true
49
49
 
50
50
  s.files = %w(Rakefile) + Dir.glob("{lib,spec}/**/*")
51
+ s.executables = ['restclient']
51
52
 
52
53
  s.require_path = "lib"
53
54
  end
data/bin/restclient ADDED
@@ -0,0 +1,65 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $:.unshift File.dirname(__FILE__) + "/../lib"
4
+ require 'rest_client'
5
+
6
+ require "yaml"
7
+
8
+ def usage(why = nil)
9
+ puts "failed for reason: #{why}" if why
10
+ puts "usage: restclient url|name [username] [password]"
11
+ exit(1)
12
+ end
13
+
14
+ @url = ARGV.shift || 'http://localhost:4567'
15
+
16
+ config = YAML.load(File.read(ENV['HOME'] + "/.restclient")) rescue {}
17
+
18
+ @url, @username, @password = if c = config[@url]
19
+ [c[:url], c[:username], c[:password]]
20
+ else
21
+ [@url, *ARGV]
22
+ end
23
+
24
+ usage("invalid url '#{@url}") unless @url =~ /^https?/
25
+ usage("to few args") unless ARGV.size < 3
26
+
27
+ def r
28
+ @r ||= RestClient::Resource.new(@url, @username, @password)
29
+ end
30
+
31
+ r # force rc to load
32
+
33
+ %w(get post put delete).each do |m|
34
+ eval <<-end_eval
35
+ def #{m}(path, *args, &b)
36
+ r[path].#{m}(*args, &b)
37
+ end
38
+ end_eval
39
+ end
40
+
41
+ def method_missing(s, *args, &b)
42
+ super unless r.respond_to?(s)
43
+ begin
44
+ r.send(s, *args, &b)
45
+ rescue RestClient::RequestFailed => e
46
+ puts e.response.body
47
+ raise e
48
+ end
49
+ end
50
+
51
+ require 'irb'
52
+ require 'irb/completion'
53
+
54
+ if File.exists? ".irbrc"
55
+ ENV['IRBRC'] = ".irbrc"
56
+ end
57
+
58
+ if File.exists?(rcfile = "~/.restclientrc")
59
+ load(rcfile)
60
+ end
61
+
62
+ ARGV.clear
63
+
64
+ IRB.start
65
+ exit!
@@ -1,5 +1,3 @@
1
- require 'rexml/document'
2
-
3
1
  module RestClient
4
2
  # This is the base RestClient exception class. Rescue it if you want to
5
3
  # catch any exception that your request might raise
@@ -59,14 +57,8 @@ module RestClient
59
57
  @response.code.to_i if @response
60
58
  end
61
59
 
62
- def message(default="Unknown error, HTTP status code #{http_code}")
63
- return default unless @response
64
- parse_error_xml rescue default
65
- end
66
-
67
- def parse_error_xml
68
- xml_errors = REXML::Document.new(@response.body).elements.to_a("//errors/error")
69
- xml_errors.empty? ? raise : xml_errors.map { |a| a.text }.join(" / ")
60
+ def message
61
+ "HTTP status code #{http_code}"
70
62
  end
71
63
 
72
64
  def to_s
data/lib/rest_client.rb CHANGED
@@ -71,7 +71,7 @@ module RestClient
71
71
  end
72
72
 
73
73
  def net_http_class(method)
74
- Object.module_eval "Net::HTTP::#{method.to_s.capitalize}"
74
+ Net::HTTP.const_get(method.to_s.capitalize)
75
75
  end
76
76
 
77
77
  def parse_url(url)
@@ -121,7 +121,15 @@ module RestClient
121
121
  if %w(200 201 202).include? res.code
122
122
  res.body
123
123
  elsif %w(301 302 303).include? res.code
124
- raise Redirect, res.header['Location']
124
+ url = res.header['Location']
125
+
126
+ if url !~ /^http/
127
+ uri = URI.parse(@url)
128
+ uri.path = "/#{url}".squeeze('/')
129
+ url = uri.to_s
130
+ end
131
+
132
+ raise Redirect, url
125
133
  elsif res.code == "401"
126
134
  raise Unauthorized
127
135
  elsif res.code == "404"
@@ -12,28 +12,20 @@ describe RestClient::Exception do
12
12
  end
13
13
 
14
14
  describe RestClient::RequestFailed do
15
- before do
16
- @error = RestClient::RequestFailed.new
15
+ it "stores the http response on the exception" do
16
+ begin
17
+ raise RestClient::RequestFailed, :response
18
+ rescue RestClient::RequestFailed => e
19
+ e.response.should == :response
20
+ end
17
21
  end
18
22
 
19
- it "extracts the error message from xml" do
20
- @error.response = mock('response', :code => '422', :body => '<errors><error>Error 1</error><error>Error 2</error></errors>')
21
- @error.message.should == 'Error 1 / Error 2'
23
+ it "http_code convenience method for fetching the code as an integer" do
24
+ RestClient::RequestFailed.new(mock('res', :code => '502')).http_code.should == 502
22
25
  end
23
26
 
24
- it "ignores responses without xml since they might contain sensitive data" do
25
- @error.response = mock('response', :code => '500', :body => 'Syntax error in SQL query: SELECT * FROM ...')
26
- @error.message.should == 'Unknown error, HTTP status code 500'
27
- end
28
-
29
- it "accepts a default error message" do
30
- @error.response = mock('response', :code => '500', :body => 'Internal Server Error')
31
- @error.message('Custom default message').should == 'Custom default message'
32
- end
33
-
34
- it "doesn't show the default error message when there's something in the xml" do
35
- @error.response = mock('response', :code => '422', :body => '<errors><error>Specific error message</error></errors>')
36
- @error.message('Custom default message').should == 'Specific error message'
27
+ it "shows the status code in the message" do
28
+ RestClient::RequestFailed.new(mock('res', :code => '502')).to_s.should match(/502/)
37
29
  end
38
30
  end
39
31
 
@@ -184,6 +184,17 @@ describe RestClient do
184
184
  lambda { @request.process_result(res) }.should raise_error(RestClient::Redirect) { |e| e.url.should == 'http://new/resource'}
185
185
  end
186
186
 
187
+ it "handles redirects with relative paths" do
188
+ res = mock('response', :code => '301', :header => { 'Location' => 'index' })
189
+ lambda { @request.process_result(res) }.should raise_error(RestClient::Redirect) { |e| e.url.should == 'http://some/index' }
190
+ end
191
+
192
+ it "handles redirects with absolute paths" do
193
+ @request.instance_variable_set('@url', 'http://some/place/else')
194
+ res = mock('response', :code => '301', :header => { 'Location' => '/index' })
195
+ lambda { @request.process_result(res) }.should raise_error(RestClient::Redirect) { |e| e.url.should == 'http://some/index' }
196
+ end
197
+
187
198
  it "raises Unauthorized when the response is 401" do
188
199
  res = mock('response', :code => '401')
189
200
  lambda { @request.process_result(res) }.should raise_error(RestClient::Unauthorized)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rest-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.1
4
+ version: "0.6"
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Wiggins
@@ -9,14 +9,14 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-07-01 00:00:00 -07:00
12
+ date: 2008-07-22 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
16
16
  description: "A simple REST client for Ruby, inspired by the Sinatra microframework style of specifying actions: get, put, post, delete."
17
17
  email: adam@heroku.com
18
- executables: []
19
-
18
+ executables:
19
+ - restclient
20
20
  extensions: []
21
21
 
22
22
  extra_rdoc_files: []