rest-client 0.6 → 0.6.1
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 +1 -1
- data/lib/rest_client.rb +36 -4
- data/spec/rest_client_spec.rb +7 -0
- metadata +5 -5
data/Rakefile
CHANGED
data/lib/rest_client.rb
CHANGED
@@ -5,6 +5,33 @@ require File.dirname(__FILE__) + '/resource'
|
|
5
5
|
require File.dirname(__FILE__) + '/request_errors'
|
6
6
|
|
7
7
|
# This module's static methods are the entry point for using the REST client.
|
8
|
+
#
|
9
|
+
# # GET
|
10
|
+
# xml = RestClient.get 'http://example.com/resource'
|
11
|
+
# jpg = RestClient.get 'http://example.com/resource', :accept => 'image/jpg'
|
12
|
+
#
|
13
|
+
# # authentication and SSL
|
14
|
+
# RestClient.get 'https://user:password@example.com/private/resource'
|
15
|
+
#
|
16
|
+
# # POST or PUT with a hash sends parameters as a urlencoded form body
|
17
|
+
# RestClient.post 'http://example.com/resource', :param1 => 'one'
|
18
|
+
#
|
19
|
+
# # nest hash parameters
|
20
|
+
# RestClient.post 'http://example.com/resource', :nested => { :param1 => 'one' }
|
21
|
+
#
|
22
|
+
# # POST and PUT with raw payloads
|
23
|
+
# RestClient.post 'http://example.com/resource', 'the post body', :content_type => 'text/plain'
|
24
|
+
# RestClient.post 'http://example.com/resource.xml', xml_doc
|
25
|
+
# RestClient.put 'http://example.com/resource.pdf', File.read('my.pdf'), :content_type => 'application/pdf'
|
26
|
+
#
|
27
|
+
# # DELETE
|
28
|
+
# RestClient.delete 'http://example.com/resource'
|
29
|
+
#
|
30
|
+
# For live tests of RestClient, try using http://rest-test.heroku.com, which echoes back information about the rest call:
|
31
|
+
#
|
32
|
+
# >> RestClient.put 'http://rest-test.heroku.com/resource', :foo => 'baz'
|
33
|
+
# => "PUT http://rest-test.heroku.com/resource with a 7 byte payload, content type application/x-www-form-urlencoded {\"foo\"=>\"baz\"}"
|
34
|
+
#
|
8
35
|
module RestClient
|
9
36
|
def self.get(url, headers={})
|
10
37
|
Request.execute(:method => :get,
|
@@ -86,14 +113,19 @@ module RestClient
|
|
86
113
|
uri
|
87
114
|
end
|
88
115
|
|
89
|
-
def process_payload(p=nil)
|
116
|
+
def process_payload(p=nil, parent_key=nil)
|
90
117
|
unless p.is_a?(Hash)
|
91
118
|
p
|
92
119
|
else
|
93
|
-
@headers[:content_type]
|
120
|
+
@headers[:content_type] ||= 'application/x-www-form-urlencoded'
|
94
121
|
p.keys.map do |k|
|
95
|
-
|
96
|
-
|
122
|
+
key = parent_key ? "#{parent_key}[#{k}]" : k
|
123
|
+
if p[k].is_a? Hash
|
124
|
+
process_payload(p[k], key)
|
125
|
+
else
|
126
|
+
value = URI.escape(p[k].to_s, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))
|
127
|
+
"#{key}=#{value}"
|
128
|
+
end
|
97
129
|
end.join("&")
|
98
130
|
end
|
99
131
|
end
|
data/spec/rest_client_spec.rb
CHANGED
@@ -131,6 +131,13 @@ describe RestClient do
|
|
131
131
|
@request.process_payload(:a => 'b c+d').should == "a=b%20c%2Bd"
|
132
132
|
end
|
133
133
|
|
134
|
+
it "accepts nested hashes in payload" do
|
135
|
+
payload = @request.process_payload(:user => { :name => 'joe', :location => { :country => 'USA', :state => 'CA' }})
|
136
|
+
payload.should include('user[name]=joe')
|
137
|
+
payload.should include('user[location][country]=USA')
|
138
|
+
payload.should include('user[location][state]=CA')
|
139
|
+
end
|
140
|
+
|
134
141
|
it "set urlencoded content_type header on hash payloads" do
|
135
142
|
@request.process_payload(:a => 1)
|
136
143
|
@request.headers[:content_type].should == 'application/x-www-form-urlencoded'
|
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:
|
4
|
+
version: 0.6.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Adam Wiggins
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2008-
|
12
|
+
date: 2008-08-01 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
@@ -23,13 +23,13 @@ extra_rdoc_files: []
|
|
23
23
|
|
24
24
|
files:
|
25
25
|
- Rakefile
|
26
|
+
- lib/request_errors.rb
|
26
27
|
- lib/resource.rb
|
27
28
|
- lib/rest_client.rb
|
28
|
-
-
|
29
|
-
- spec/resource_spec.rb
|
29
|
+
- spec/base.rb
|
30
30
|
- spec/request_errors_spec.rb
|
31
|
+
- spec/resource_spec.rb
|
31
32
|
- spec/rest_client_spec.rb
|
32
|
-
- spec/base.rb
|
33
33
|
has_rdoc: true
|
34
34
|
homepage: http://rest-client.heroku.com/
|
35
35
|
post_install_message:
|