wrest 1.4.2 → 1.4.3
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +4 -0
- data/README.rdoc +2 -2
- data/lib/wrest.rb +1 -0
- data/lib/wrest/async_request/event_machine_backend.rb +2 -2
- data/lib/wrest/async_request/thread_backend.rb +2 -2
- data/lib/wrest/http_shared/standard_headers.rb +1 -0
- data/lib/wrest/uri.rb +60 -57
- data/lib/wrest/uri/builders.rb +42 -0
- data/lib/wrest/version.rb +1 -1
- data/lib/wrest/xml_mini.rb +0 -2
- metadata +3 -2
data/CHANGELOG
CHANGED
@@ -2,6 +2,10 @@ Features under the section marked 'Current' are completed but pending release as
|
|
2
2
|
|
3
3
|
Features under a numbered section are complete and available in the Wrest gem.
|
4
4
|
|
5
|
+
== 1.4.3
|
6
|
+
* GH#104 Allow default headers to be added to the uri that are then used for all requests
|
7
|
+
* GH#31 Add explicit API for cookies
|
8
|
+
|
5
9
|
== 1.4.2
|
6
10
|
* GH#102 Code and gemspec mismatch for json-jruby
|
7
11
|
|
data/README.rdoc
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
= Wrest 1.4.
|
1
|
+
= Wrest 1.4.3
|
2
2
|
|
3
3
|
(c) Copyright 2009-2011 {Sidu Ponnappa}[http://blog.sidu.in]. All Rights Reserved.
|
4
4
|
|
@@ -212,7 +212,7 @@ Example:
|
|
212
212
|
=== Logging
|
213
213
|
|
214
214
|
The Wrest logger can be set and accessed through Wrest.logger and is configured by default to log to STDOUT. If you're using Wrest in a Rails application, you can configure logging by adding a config/initializers/wrest.rb file with the following contents :
|
215
|
-
Wrest.logger =
|
215
|
+
Wrest.logger = Rails.logger
|
216
216
|
|
217
217
|
=== Build
|
218
218
|
|
data/lib/wrest.rb
CHANGED
@@ -79,6 +79,7 @@ Wrest::AsyncRequest.default_to_threads!
|
|
79
79
|
require "#{Wrest::Root}/wrest/caching"
|
80
80
|
|
81
81
|
# Load Wrest Wrappers
|
82
|
+
require "#{Wrest::Root}/wrest/uri/builders"
|
82
83
|
require "#{Wrest::Root}/wrest/uri"
|
83
84
|
require "#{Wrest::Root}/wrest/uri_template"
|
84
85
|
require "#{Wrest::Root}/wrest/exceptions"
|
data/lib/wrest/uri.rb
CHANGED
@@ -22,22 +22,38 @@ module Wrest #:nodoc:
|
|
22
22
|
#
|
23
23
|
# You can find examples that use real APIs (like delicious) under the wrest/examples directory.
|
24
24
|
class Uri
|
25
|
-
attr_reader :uri, :username, :password, :uri_string, :uri_path, :query
|
26
|
-
|
27
|
-
#
|
25
|
+
attr_reader :uri, :username, :password, :uri_string, :uri_path, :query, :default_headers
|
26
|
+
|
27
|
+
# Valid tuples for the options are:
|
28
|
+
# :asynchronous_backend => Can currently be set to either Wrest::AsyncRequest::EventMachineBackend.new
|
29
|
+
# or Wrest::AsyncRequest::ThreadBackend.new. Easier to do using Uri#using_em and
|
30
|
+
# Uri#using_threads.
|
31
|
+
# :callback => Accepts a hash where the keys are response codes or ranges of response codes and
|
32
|
+
# the values are the corresponding blocks that will be invoked should the response
|
33
|
+
# code match the key.
|
34
|
+
# :default_headers => Accepts a hash containing a set of default request headers with which the headers
|
35
|
+
# passed to Uri#get, Uri#post etc. are merged. Incoming headers will override the
|
36
|
+
# defaults if there are any clashes. Use this to set cookies or use OAuth2 Authorize
|
37
|
+
# headers. When extending or cloning a Uri, passing in a new set of default_headers
|
38
|
+
# will result in the old set being overridden.
|
39
|
+
# See Wrest::Native::Request for other available options and their default values.
|
28
40
|
def initialize(uri_string, options = {})
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
41
|
+
@options = options.clone
|
42
|
+
@uri_string = uri_string.to_s
|
43
|
+
@uri = URI.parse(@uri_string)
|
44
|
+
uri_scheme = URI.split(@uri_string)
|
45
|
+
@uri_path = uri_scheme[-4].split('?').first || ''
|
46
|
+
@uri_path = (@uri_path.empty? ? '/' : @uri_path)
|
47
|
+
@query = uri_scheme[-2] || ''
|
48
|
+
@username = (@options[:username] ||= @uri.user)
|
49
|
+
@password = (@options[:password] ||= @uri.password)
|
50
|
+
@asynchronous_backend = @options[:asynchronous_backend] || Wrest::AsyncRequest.default_backend
|
51
|
+
@options[:callback] = Callback.new(@options[:callback]) if @options[:callback]
|
52
|
+
@default_headers = @options[:default_headers] || {}
|
39
53
|
end
|
40
54
|
|
55
|
+
# Builds a Wrest::UriTemplate by extending the current URI
|
56
|
+
# with the pattern passed to it.
|
41
57
|
def to_template(pattern)
|
42
58
|
template_pattern = URI.join(uri_string,pattern).to_s
|
43
59
|
UriTemplate.new(template_pattern, @options)
|
@@ -88,49 +104,39 @@ module Wrest #:nodoc:
|
|
88
104
|
uri_string
|
89
105
|
end
|
90
106
|
|
91
|
-
|
92
|
-
def
|
93
|
-
options
|
94
|
-
options[:asynchronous_backend] = Wrest::AsyncRequest::ThreadBackend.new
|
95
|
-
Uri.new(uri_string, options)
|
107
|
+
#:nodoc:
|
108
|
+
def build_get(parameters = {}, headers = {}, &block)
|
109
|
+
Http::Get.new(self, parameters, default_headers.merge(headers), block ? @options.merge(:callback_block => block) : @options)
|
96
110
|
end
|
97
|
-
|
98
|
-
|
99
|
-
def
|
100
|
-
options
|
101
|
-
Wrest::AsyncRequest.enable_em
|
102
|
-
options[:asynchronous_backend] = Wrest::AsyncRequest::EventMachineBackend.new
|
103
|
-
Uri.new(uri_string, options)
|
111
|
+
|
112
|
+
#:nodoc:
|
113
|
+
def build_put(body = '', headers = {}, parameters = {}, &block)
|
114
|
+
Http::Put.new(self, body.to_s, default_headers.merge(headers), parameters, block ? @options.merge(:callback_block => block) : @options)
|
104
115
|
end
|
105
|
-
|
106
|
-
|
107
|
-
def
|
108
|
-
options
|
109
|
-
options[:cache_store] = Hash.new
|
110
|
-
Uri.new(uri_string, options)
|
116
|
+
|
117
|
+
#:nodoc:
|
118
|
+
def build_post(body = '', headers = {}, parameters = {}, &block)
|
119
|
+
Http::Post.new(self, body.to_s, default_headers.merge(headers), parameters, block ? @options.merge(:callback_block => block) : @options)
|
111
120
|
end
|
112
121
|
|
113
|
-
|
114
|
-
def
|
115
|
-
|
116
|
-
|
117
|
-
options
|
118
|
-
Uri.new(uri_string, options)
|
122
|
+
#:nodoc:
|
123
|
+
def build_post_form(parameters ={}, headers = {}, &block)
|
124
|
+
headers = default_headers.merge(headers).merge(Wrest::H::ContentType => Wrest::T::FormEncoded)
|
125
|
+
body = parameters.to_query
|
126
|
+
Http::Post.new(self, body, headers, {}, block ? @options.merge(:callback_block => block) : @options)
|
119
127
|
end
|
120
128
|
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
options[:disable_cache] = true
|
125
|
-
Uri.new(uri_string, options)
|
129
|
+
#:nodoc:
|
130
|
+
def build_delete(parameters = {}, headers = {}, &block)
|
131
|
+
Http::Delete.new(self, parameters, default_headers.merge(headers), block ? @options.merge(:callback_block => block) : @options)
|
126
132
|
end
|
127
|
-
|
133
|
+
|
128
134
|
# Make a GET request to this URI. This is a convenience API
|
129
135
|
# that creates a Wrest::Native::Get, executes it and returns a Wrest::Native::Response.
|
130
136
|
#
|
131
137
|
# Remember to escape all parameter strings if necessary, using URI.escape
|
132
138
|
def get(parameters = {}, headers = {}, &block)
|
133
|
-
|
139
|
+
build_get(parameters, headers, &block).invoke
|
134
140
|
end
|
135
141
|
|
136
142
|
# Make a GET request to this URI. This is a convenience API
|
@@ -141,7 +147,7 @@ module Wrest #:nodoc:
|
|
141
147
|
# Note: get_async does not return a response and the response should be accessed through callbacks.
|
142
148
|
# This implementation of asynchronous get is currently in alpha. Hence, it should not be used in production.
|
143
149
|
def get_async(parameters = {}, headers = {}, &block)
|
144
|
-
|
150
|
+
@asynchronous_backend.execute(build_get(parameters, headers, &block))
|
145
151
|
nil
|
146
152
|
end
|
147
153
|
|
@@ -150,7 +156,7 @@ module Wrest #:nodoc:
|
|
150
156
|
#
|
151
157
|
# Remember to escape all parameter strings if necessary, using URI.escape
|
152
158
|
def put(body = '', headers = {}, parameters = {}, &block)
|
153
|
-
|
159
|
+
build_put(body, headers, parameters, &block).invoke
|
154
160
|
end
|
155
161
|
|
156
162
|
# Make a PUT request to this URI. This is a convenience API
|
@@ -161,7 +167,7 @@ module Wrest #:nodoc:
|
|
161
167
|
# Note: put_async does not return a response and the response should be accessed through callbacks.
|
162
168
|
# This implementation of asynchronous put is currently in alpha. Hence, it should not be used in production.
|
163
169
|
def put_async(body = '', headers = {}, parameters = {}, &block)
|
164
|
-
|
170
|
+
@asynchronous_backend.execute(build_put(body, headers, parameters, &block))
|
165
171
|
nil
|
166
172
|
end
|
167
173
|
|
@@ -171,7 +177,7 @@ module Wrest #:nodoc:
|
|
171
177
|
#
|
172
178
|
# Remember to escape all parameter strings if necessary, using URI.escape
|
173
179
|
def post(body = '', headers = {}, parameters = {}, &block)
|
174
|
-
|
180
|
+
build_post(body, headers, parameters, &block).invoke
|
175
181
|
end
|
176
182
|
|
177
183
|
# Makes a POST request to this URI. This is a convenience API
|
@@ -183,7 +189,7 @@ module Wrest #:nodoc:
|
|
183
189
|
# Note: post_async does not return a response and the response should be accessed through callbacks.
|
184
190
|
# This implementation of asynchronous post is currently in alpha. Hence, it should not be used in production.
|
185
191
|
def post_async(body = '', headers = {}, parameters = {}, &block)
|
186
|
-
|
192
|
+
@asynchronous_backend.execute(build_post(body, headers, parameters, &block))
|
187
193
|
nil
|
188
194
|
end
|
189
195
|
|
@@ -195,9 +201,7 @@ module Wrest #:nodoc:
|
|
195
201
|
# in the body, as well as setting the Content-Type header to
|
196
202
|
# application/x-www-form-urlencoded
|
197
203
|
def post_form(parameters = {}, headers = {}, &block)
|
198
|
-
|
199
|
-
body = parameters.to_query
|
200
|
-
Http::Post.new(self, body, headers, {}, block ? @options.merge(:callback_block => block) : @options).invoke
|
204
|
+
build_post_form(parameters, headers, &block).invoke
|
201
205
|
end
|
202
206
|
|
203
207
|
# Makes a POST request to this URI. This is a convenience API
|
@@ -211,9 +215,7 @@ module Wrest #:nodoc:
|
|
211
215
|
# Note: post_form_async does not return a response and the response should be accessed through callbacks.
|
212
216
|
# This implementation of asynchronous post_form is currently in alpha. Hence, it should not be used in production.
|
213
217
|
def post_form_async(parameters = {}, headers = {}, &block)
|
214
|
-
|
215
|
-
body = parameters.to_query
|
216
|
-
(@options[:asynchronous_backend] || Wrest::AsyncRequest.default_backend).execute(Http::Post.new(self, body, headers, {}, block ? @options.merge(:callback_block => block) : @options))
|
218
|
+
@asynchronous_backend.execute(build_post_form(parameters, headers, &block))
|
217
219
|
nil
|
218
220
|
end
|
219
221
|
|
@@ -222,7 +224,7 @@ module Wrest #:nodoc:
|
|
222
224
|
#
|
223
225
|
# Remember to escape all parameter strings if necessary, using URI.escape
|
224
226
|
def delete(parameters = {}, headers = {}, &block)
|
225
|
-
|
227
|
+
build_delete(parameters, headers, &block).invoke
|
226
228
|
end
|
227
229
|
|
228
230
|
# Makes a DELETE request to this URI. This is a convenience API
|
@@ -233,7 +235,7 @@ module Wrest #:nodoc:
|
|
233
235
|
# Note: delete_async does not return a response and the response should be accessed through callbacks.
|
234
236
|
# This implementation of asynchronous delete is currently in alpha. Hence, it should not be used in production.
|
235
237
|
def delete_async(parameters = {}, headers = {}, &block)
|
236
|
-
|
238
|
+
@asynchronous_backend.execute(build_delete(parameters, headers, &block))
|
237
239
|
nil
|
238
240
|
end
|
239
241
|
|
@@ -269,5 +271,6 @@ module Wrest #:nodoc:
|
|
269
271
|
end
|
270
272
|
|
271
273
|
include Http::ConnectionFactory
|
274
|
+
include Uri::Builders
|
272
275
|
end
|
273
276
|
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
module Wrest
|
2
|
+
class Uri
|
3
|
+
# Contains methods that depend on Uri#clone to build
|
4
|
+
# new Uris configured in particular ways.
|
5
|
+
module Builders
|
6
|
+
# Returns a Uri object that uses threads to perform asynchronous requests.
|
7
|
+
def using_threads
|
8
|
+
clone(:asynchronous_backend => Wrest::AsyncRequest::ThreadBackend.new)
|
9
|
+
end
|
10
|
+
|
11
|
+
# Returns a Uri object that uses eventmachine to perform asynchronous requests.
|
12
|
+
# Remember to do Wrest::AsyncRequest.enable_em first so that
|
13
|
+
# EventMachine is available for use.
|
14
|
+
def using_em
|
15
|
+
clone(:asynchronous_backend => Wrest::AsyncRequest::EventMachineBackend.new)
|
16
|
+
end
|
17
|
+
|
18
|
+
# Returns a Uri object that uses hash for caching responses.
|
19
|
+
def using_hash
|
20
|
+
clone(:cache_store => {})
|
21
|
+
end
|
22
|
+
|
23
|
+
# Returns a Uri object that uses memcached for caching responses.
|
24
|
+
# Remember to do Wrest::AsyncRequest.enable_memcached first so that
|
25
|
+
# memcached is available for use.
|
26
|
+
def using_memcached
|
27
|
+
clone(:cache_store => Wrest::Caching::Memcached.new)
|
28
|
+
end
|
29
|
+
|
30
|
+
# Disables using the globally configured cache for GET requests
|
31
|
+
# made using the Uri returned by this method.
|
32
|
+
def disable_cache
|
33
|
+
clone(:disable_cache => true)
|
34
|
+
end
|
35
|
+
|
36
|
+
# Sets the specified string as the cookie for the Uri
|
37
|
+
def using_cookie(cookie_string)
|
38
|
+
clone(:default_headers => {Wrest::H::Cookie => cookie_string})
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
data/lib/wrest/version.rb
CHANGED
data/lib/wrest/xml_mini.rb
CHANGED
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: wrest
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 1.4.
|
5
|
+
version: 1.4.3
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Sidu Ponnappa
|
@@ -11,7 +11,7 @@ autorequire:
|
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
13
|
|
14
|
-
date: 2011-
|
14
|
+
date: 2011-05-02 00:00:00 +05:30
|
15
15
|
default_executable:
|
16
16
|
dependencies:
|
17
17
|
- !ruby/object:Gem::Dependency
|
@@ -174,6 +174,7 @@ files:
|
|
174
174
|
- lib/wrest/native.rb
|
175
175
|
- lib/wrest/test/request_patches.rb
|
176
176
|
- lib/wrest/test.rb
|
177
|
+
- lib/wrest/uri/builders.rb
|
177
178
|
- lib/wrest/uri.rb
|
178
179
|
- lib/wrest/uri_template.rb
|
179
180
|
- lib/wrest/version.rb
|