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 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.2
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 = ActiveRecord::Base.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"
@@ -19,9 +19,9 @@ require 'eventmachine'
19
19
  module Wrest
20
20
  module AsyncRequest
21
21
  class EventMachineBackend
22
- def execute(block)
22
+ def execute(request)
23
23
  EventMachine.run do
24
- block.invoke
24
+ request.invoke
25
25
  EventMachine.stop
26
26
  end
27
27
  end
@@ -10,9 +10,9 @@
10
10
  module Wrest
11
11
  module AsyncRequest
12
12
  class ThreadBackend
13
- def execute(block)
13
+ def execute(request)
14
14
  Thread.new do
15
- block.invoke
15
+ request.invoke
16
16
  end
17
17
  end
18
18
  end
@@ -14,6 +14,7 @@ module Wrest
14
14
  ContentType = 'content-type'
15
15
  ContentLength = 'content-length'
16
16
  IfNoneMatch = 'if-none-match'
17
+ Cookie = 'cookie'
17
18
  end
18
19
  end
19
20
  end
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
- # See Wrest::Native::Request for the available options and their default values.
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
- @options = options.clone
30
- @uri_string = uri_string.to_s
31
- @uri = URI.parse(@uri_string)
32
- uri_scheme = URI.split(@uri_string)
33
- @uri_path = uri_scheme[-4].split('?').first || ''
34
- @uri_path = (@uri_path.empty? ? '/' : @uri_path)
35
- @query = uri_scheme[-2] || ''
36
- @username = (@options[:username] ||= @uri.user)
37
- @password = (@options[:password] ||= @uri.password)
38
- @options[:callback] = Callback.new(@options[:callback]) if @options[:callback]
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
- # Returns a Uri object that uses threads to perform asynchronous requests.
92
- def using_threads
93
- options = @options.clone
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
- # Returns a Uri object that uses eventmachine to perform asynchronous requests.
99
- def using_em
100
- options = @options.clone
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
- # Returns a Uri object that uses hash for caching responses.
107
- def using_hash
108
- options = @options.clone
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
- # Returns a Uri object that uses memcached for caching responses.
114
- def using_memcached
115
- options = @options.clone
116
- Wrest::Caching.enable_memcached
117
- options[:cache_store] = Wrest::Caching::Memcached.new
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
- def disable_cache
122
- options = @options.clone
123
- Wrest::Caching.enable_memcached
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
- Http::Get.new(self, parameters, headers, block ? @options.merge(:callback_block => block) : @options).invoke
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
- (@options[:asynchronous_backend] || Wrest::AsyncRequest.default_backend).execute(Http::Get.new(self, parameters, headers, block ? @options.merge(:callback_block => block) : @options))
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
- Http::Put.new(self, body.to_s, headers, parameters, block ? @options.merge(:callback_block => block) : @options).invoke
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
- (@options[:asynchronous_backend] || Wrest::AsyncRequest.default_backend).execute(Http::Put.new(self, body.to_s, headers, parameters, block ? @options.merge(:callback_block => block) : @options))
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
- Http::Post.new(self, body.to_s, headers, parameters, block ? @options.merge(:callback_block => block) : @options).invoke
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
- (@options[:asynchronous_backend] || Wrest::AsyncRequest.default_backend).execute(Http::Post.new(self, body.to_s, headers, parameters, block ? @options.merge(:callback_block => block) : @options))
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
- headers = headers.merge(Wrest::H::ContentType => Wrest::T::FormEncoded)
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
- headers = headers.merge(Wrest::H::ContentType => Wrest::T::FormEncoded)
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
- Http::Delete.new(self, parameters, headers, block ? @options.merge(:callback_block => block) : @options).invoke
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
- (@options[:asynchronous_backend] || Wrest::AsyncRequest.default_backend).execute(Http::Delete.new(self, parameters, headers, block ? @options.merge(:callback_block => block) : @options))
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
@@ -8,5 +8,5 @@
8
8
  # See the License for the specific language governing permissions and limitations under the License.
9
9
 
10
10
  module Wrest
11
- VERSION = "1.4.2"
11
+ VERSION = "1.4.3"
12
12
  end
@@ -6,5 +6,3 @@ module ActiveSupport
6
6
  delegate :filter, :to => :backend
7
7
  end
8
8
  end
9
-
10
-
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: wrest
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 1.4.2
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-04-05 00:00:00 +05:30
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