wrest 1.5.4 → 2.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c2ef12f85a25ae1566e9dc1b687a364d765f6a08
4
- data.tar.gz: fb83d920f115473b271d16cad06195fb73524d00
3
+ metadata.gz: 71e16e78be5f22404a1639ad9166c893c35a40b2
4
+ data.tar.gz: 4713b8f3483fc1b80a48b4feac5802c7a991db63
5
5
  SHA512:
6
- metadata.gz: a88c38044cafcad5a161e4fd974b8023600c830865f1734451a5e948720d647b828f73caeca7b89ef008b6f8c472c44e2a05cc8b4602458939280427dd7121ab
7
- data.tar.gz: baff6d4f993a61763836866bd838b0517c802aaa1547593aeb07b92c5a599defa77a70db5616e2e3bdacec48b343c4c78f1513990e4bb1727d537d69f5a17688
6
+ metadata.gz: 04113e29601ed0953ca5c8f995f0771d15cc722c6712403dd31761ad1c9fea19adfe66b5e0aeb22e9c5edb567f16e5788f195a5b57a6e4b896d2042d0874786d
7
+ data.tar.gz: fad5508d9f0baee2ba934f6cfa7712f103b1d48c110ebe838423b61929dd09759965fdda4717a760f8f3b49e386eec2aa685795ec9f07ad1df681b900ab902d1
data/CHANGELOG CHANGED
@@ -2,6 +2,12 @@ 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
+ == 2.1.1
6
+ * Including Redis as a Caching Backend
7
+
8
+ == 2.0.0
9
+ * Removing support for libcurl
10
+
5
11
  == 1.5.4
6
12
  * Make thread pool configurable
7
13
 
data/README.md CHANGED
@@ -1,12 +1,12 @@
1
1
  [![Build Status](https://travis-ci.org/c42/wrest.svg?branch=master)](https://travis-ci.org/c42/wrest)
2
2
 
3
- # Wrest 1.5.4
3
+ # Wrest 2.1.0
4
4
 
5
5
  (c) Copyright 2009-2015 [Sidu Ponnappa](http://twitter.com/ponnappa). All Rights Reserved.
6
6
 
7
7
  Wrest is a ruby REST/HTTP client library which
8
8
 
9
- * Allows you to use Net::HTTP or libCurl
9
+ * Allows you to use Net::HTTP
10
10
  * Allows you to pick your Ruby: use 2.x.x, JRuby 1.7.6 (and higher), JRuby 9.0.0.0.pre2
11
11
  * Supports RFC 2616 based [caching](https://github.com/kaiwren/wrest/blob/caching/Caching.markdown)
12
12
  * Async http calls using Threads (reliable only on JRuby) or EventMachine
@@ -19,7 +19,7 @@ To receive notifications whenever new features are added to Wrest, please subscr
19
19
 
20
20
  ##Examples
21
21
 
22
- For Facebook, Twitter, Delicious, GitHub and other API examples, see http://github.com/kaiwren/wrest/tree/master/examples
22
+ For Facebook, Twitter, Delicious, GitHub and other API examples, see http://github.com/c42/wrest/tree/master/examples
23
23
 
24
24
  ### Basic Http Calls
25
25
 
@@ -63,13 +63,10 @@ For Facebook, Twitter, Delicious, GitHub and other API examples, see http://gith
63
63
  * More complex request with parameters and a custom deserialiser
64
64
 
65
65
  ```
66
- 'http://search.yahooapis.com/NewsSearchService/V1/newsSearch'.to_uri.get(
67
- :appid => 'YahooDemo',
68
- :output => 'xml',
69
- :query => 'India',
70
- :results=> '3',
71
- :start => '1'
72
- ).deserialise_using(
66
+ 'api.openweathermap.org/data/2.5/weather'.to_uri.get(
67
+ :lat => 35,
68
+ :lon => 139
69
+ ).deserialise_using(
73
70
  Wrest::Components::Translators::Xml
74
71
  )
75
72
  ```
@@ -113,7 +110,7 @@ For Facebook, Twitter, Delicious, GitHub and other API examples, see http://gith
113
110
 
114
111
  Note: To enable Multipart support, you'll have to explicitly require 'wrest/multipart', which depends on the multipart-post gem.
115
112
 
116
- # DELETE
113
+ #### DELETE
117
114
 
118
115
  To delete a resource:
119
116
 
@@ -128,13 +125,29 @@ To delete a resource:
128
125
 
129
126
  ### Caching
130
127
 
131
- Wrest supports caching with pluggable back-ends.
128
+ Wrest supports caching with the following pluggable back-ends:
129
+ - Hash
130
+ - Memcached
131
+ - Redis
132
+
133
+ ####Hash
134
+
135
+ Use the following method to enable caching for all requests, and set Hash as the default cache store.
136
+ Note: Hash should NEVER be used in a production environment. It is unbounded and will keep increasing in size.
132
137
 
133
138
  ```
134
- Wrest::Caching.default_to_hash! # Hash should NEVER be used in a production environment. It is unbounded and will keep increasing in size.
135
- c42 = "http://c42.in".to_uri.get
139
+ Wrest::Caching.default_to_hash!
140
+ c42 = 'http://c42.in'.to_uri.get
136
141
  ```
137
142
 
143
+ To use Hash as a cache store in an explicit request (without setting hash as default), use the following API:
144
+
145
+ ```
146
+ r1 = "http://c42.in".to_uri.using_hash.get
147
+ ```
148
+
149
+ ####Memcached
150
+
138
151
  A Memcached based caching back-end is available in Wrest. You can get instructions on how to install Memcached on your system [here](http://code.google.com/p/memcached/wiki/NewInstallFromPackage).
139
152
  The Dalli gem is used by Wrest to interface with Memcached. Install dalli using 'gem install dalli'.
140
153
 
@@ -144,16 +157,34 @@ Use the following method to enable caching for all requests, and set Memcached a
144
157
  Wrest::Caching.default_to_memcached!
145
158
  ```
146
159
 
147
- For fine-grained control over the cache store (or to use multiple cache stores in the same codebase), you can use this API:
160
+ To use Memcached as a cache store in an explicit request (without setting memcached as default), use the following API:
161
+
162
+ ```
163
+ Wrest::Caching.enable_memcached
164
+ r2 = "http://c42.in".to_uri.using_memcached.get
165
+ ```
166
+
167
+ ####Redis
168
+
169
+ Wrest also supports a Redis based caching back-end. Follow the guide [here](http://redis.io/topics/quickstart) to install Redis in your system.
170
+ It uses [redis-rd](https://github.com/redis/redis-rb) to interface with Redis. Install redis-rb using `gem install redis`.
171
+
172
+ Use the following method to enable caching for all requests, and set Redis as the default back-end.
173
+
174
+ ```
175
+ Wrest::Caching.default_to_redis!
176
+ ```
177
+
178
+ To use Redis as a cache store in an explicit request (without setting redis as default), use the following API:
148
179
 
149
180
  ```
150
- r1 = "http://c42.in".to_uri.using_memcached.get
151
- r2 = "http://c42.in".to_uri.using_hash.get
181
+ Wrest::Caching.enable_redis
182
+ r3 = "http://c42.in".to_uri.using_redis.get
152
183
  ```
153
184
 
154
- A detailed writeup regarding caching as defined by RFC 2616, and how Wrest implements caching is at [Wrest Caching Doc](https://github.com/kaiwren/wrest/blob/master/Caching.markdown)
185
+ A detailed writeup regarding caching as defined by RFC 2616, and how Wrest implements caching is at [Wrest Caching Doc](https://github.com/c42/wrest/blob/master/Caching.markdown)
155
186
 
156
- You can create your own back-ends for Wrest caching by implementing the interface implemented in https://github.com/kaiwren/wrest/blob/master/lib/wrest/components/cache_store/memcached.rb
187
+ You can create your own back-ends for Wrest caching by implementing the interface implemented in https://github.com/c42/wrest/blob/master/lib/wrest/caching/redis.rb
157
188
 
158
189
  To explicitly disable caching for specific requests:
159
190
 
@@ -42,12 +42,6 @@ module Wrest
42
42
  def self.use_native!
43
43
  silence_warnings{ Wrest.const_set('Http', Wrest::Native) }
44
44
  end
45
-
46
- # Switch Wrest to using libcurl.
47
- def self.use_curl!
48
- require "wrest/curl"
49
- silence_warnings{ Wrest.const_set('Http', Wrest::Curl) }
50
- end
51
45
  end
52
46
 
53
47
  Wrest.logger = ActiveSupport::Logger.new(STDOUT)
@@ -13,12 +13,17 @@ module Wrest
13
13
  def self.enable_memcached
14
14
  require "wrest/caching/memcached"
15
15
  end
16
+
17
+ # Loads the Redis caching back-end and the Redis gem
18
+ def self.enable_redis
19
+ require "wrest/caching/redis"
20
+ end
16
21
 
17
22
  # Configures Wrest to cache all requests. This will use a Ruby Hash.
18
- # WARNING: This should NEVER be used in a real environment. The Hash will keep on growing since Wrest does not limit the size of a cache store.
23
+ # WARNING: This should NEVER be used in a real environment. The Hash will
24
+ # keep growing since Wrest does not limit the size of a cache store.
19
25
  #
20
- # Use the Memcached caching back-end for production since the Memcached process uses an LRU based cache removal policy
21
- # that keeps the number of entries stored within bounds.
26
+ # Please switch to the memcached or redis back-end for production use.
22
27
  def self.default_to_hash!
23
28
  self.default_store = Hash.new
24
29
  end
@@ -28,6 +33,14 @@ module Wrest
28
33
  self.enable_memcached
29
34
  self.default_store = Wrest::Caching::Memcached.new
30
35
  end
36
+
37
+ # Default Wrest to using redis for caching requests.
38
+ #
39
+ # Options to configuring the redis gem can be passed as arguments.
40
+ def self.default_to_redis!(redis_options = {})
41
+ self.enable_redis
42
+ self.default_store = Wrest::Caching::Redis.new(redis_options)
43
+ end
31
44
 
32
45
  # Assign the default cache store to be used. Default is none.
33
46
  def self.default_store=(store)
@@ -1,7 +1,7 @@
1
1
  begin
2
2
  gem 'dalli', '~> 2'
3
3
  rescue Gem::LoadError => e
4
- Wrest.logger.debug "Dalli ~> 2 not found. Dalli is necessary to use the memcached caching back-end. To install dalli run `(sudo) gem install dalli`."
4
+ Wrest.logger.debug "Dalli ~> 2 not found. The Dalli gem is necessary to use the memcached caching back-end."
5
5
  raise e
6
6
  end
7
7
 
@@ -0,0 +1,37 @@
1
+ begin
2
+ gem 'redis', '~> 3'
3
+ rescue Gem::LoadError => e
4
+ Wrest.logger.debug "Redis ~> 3 not found. The Redis gem is necessary to use redis as a caching back-end."
5
+ raise e
6
+ end
7
+
8
+ require 'redis'
9
+ require 'yaml'
10
+
11
+ module Wrest::Caching
12
+ class Redis
13
+
14
+ def initialize(redis_options = {})
15
+ @redis = ::Redis.new(redis_options)
16
+ end
17
+
18
+ def [](key)
19
+ value = @redis.get(key)
20
+ unmarshalled_value = value.nil? ? nil : YAML::load(value)
21
+ unmarshalled_value
22
+ end
23
+
24
+ def []=(key, value)
25
+ marshalled_value = YAML::dump(value)
26
+ @redis.set(key, marshalled_value)
27
+ end
28
+
29
+ def delete(key)
30
+ value = self[key]
31
+
32
+ @redis.del(key)
33
+
34
+ return value
35
+ end
36
+ end
37
+ end
@@ -21,7 +21,7 @@ module Wrest
21
21
  # To enable Multipart support, use
22
22
  # require 'wrest/multipart'
23
23
  #
24
- # Multipart support is currently only available on Net::Http and not when using libcurl.
24
+ # Multipart support is currently only available on Net::Http
25
25
  # It depends on the multipart-post gem being available. To install multipart-post
26
26
  # (sudo) gem install multipart-post
27
27
  #
@@ -173,7 +173,6 @@ module Wrest #:nodoc:
173
173
 
174
174
  # Makes a POST request to this URI. This is a convenience API
175
175
  # that creates a Wrest::Native::Post, executes it and returns a Wrest::Native::Response.
176
- # Note that sending an empty body will blow up if you're using libcurl.
177
176
  #
178
177
  # Remember to escape all parameter strings if necessary, using URI.escape
179
178
  def post(body = '', headers = {}, parameters = {}, &block)
@@ -182,8 +181,6 @@ module Wrest #:nodoc:
182
181
 
183
182
  # Makes a POST request to this URI. This is a convenience API
184
183
  # that creates a Wrest::Native::Post.
185
- # Note that sending an empty body will blow up if you're using libcurl.
186
- #
187
184
  # Remember to escape all parameter strings if necessary, using URI.escape
188
185
  #
189
186
  # Note: post_async does not return a response and the response should be accessed through callbacks.
@@ -27,6 +27,10 @@ module Wrest
27
27
  clone(:cache_store => Wrest::Caching::Memcached.new)
28
28
  end
29
29
 
30
+ def using_redis
31
+ clone(:cache_store => Wrest::Caching::Redis.new)
32
+ end
33
+
30
34
  # Disables using the globally configured cache for GET requests
31
35
  # made using the Uri returned by this method.
32
36
  def disable_cache
@@ -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.5.4"
11
+ VERSION = "2.1.1"
12
12
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wrest
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.5.4
4
+ version: 2.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sidu Ponnappa
@@ -9,90 +9,90 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-08-05 00:00:00.000000000 Z
12
+ date: 2015-11-26 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
16
16
  requirement: !ruby/object:Gem::Requirement
17
17
  requirements:
18
- - - "~>"
18
+ - - ~>
19
19
  - !ruby/object:Gem::Version
20
20
  version: '3.3'
21
21
  type: :development
22
22
  prerelease: false
23
23
  version_requirements: !ruby/object:Gem::Requirement
24
24
  requirements:
25
- - - "~>"
25
+ - - ~>
26
26
  - !ruby/object:Gem::Version
27
27
  version: '3.3'
28
28
  - !ruby/object:Gem::Dependency
29
29
  name: sinatra
30
30
  requirement: !ruby/object:Gem::Requirement
31
31
  requirements:
32
- - - "~>"
32
+ - - ~>
33
33
  - !ruby/object:Gem::Version
34
34
  version: 1.0.0
35
35
  type: :development
36
36
  prerelease: false
37
37
  version_requirements: !ruby/object:Gem::Requirement
38
38
  requirements:
39
- - - "~>"
39
+ - - ~>
40
40
  - !ruby/object:Gem::Version
41
41
  version: 1.0.0
42
42
  - !ruby/object:Gem::Dependency
43
43
  name: metric_fu
44
44
  requirement: !ruby/object:Gem::Requirement
45
45
  requirements:
46
- - - ">="
46
+ - - '>='
47
47
  - !ruby/object:Gem::Version
48
48
  version: '0'
49
49
  type: :development
50
50
  prerelease: false
51
51
  version_requirements: !ruby/object:Gem::Requirement
52
52
  requirements:
53
- - - ">="
53
+ - - '>='
54
54
  - !ruby/object:Gem::Version
55
55
  version: '0'
56
56
  - !ruby/object:Gem::Dependency
57
57
  name: activesupport
58
58
  requirement: !ruby/object:Gem::Requirement
59
59
  requirements:
60
- - - "~>"
60
+ - - ~>
61
61
  - !ruby/object:Gem::Version
62
62
  version: '4'
63
63
  type: :runtime
64
64
  prerelease: false
65
65
  version_requirements: !ruby/object:Gem::Requirement
66
66
  requirements:
67
- - - "~>"
67
+ - - ~>
68
68
  - !ruby/object:Gem::Version
69
69
  version: '4'
70
70
  - !ruby/object:Gem::Dependency
71
71
  name: builder
72
72
  requirement: !ruby/object:Gem::Requirement
73
73
  requirements:
74
- - - ">"
74
+ - - '>'
75
75
  - !ruby/object:Gem::Version
76
76
  version: '2.0'
77
77
  type: :runtime
78
78
  prerelease: false
79
79
  version_requirements: !ruby/object:Gem::Requirement
80
80
  requirements:
81
- - - ">"
81
+ - - '>'
82
82
  - !ruby/object:Gem::Version
83
83
  version: '2.0'
84
84
  - !ruby/object:Gem::Dependency
85
85
  name: multi_json
86
86
  requirement: !ruby/object:Gem::Requirement
87
87
  requirements:
88
- - - "~>"
88
+ - - ~>
89
89
  - !ruby/object:Gem::Version
90
90
  version: '1.0'
91
91
  type: :runtime
92
92
  prerelease: false
93
93
  version_requirements: !ruby/object:Gem::Requirement
94
94
  requirements:
95
- - - "~>"
95
+ - - ~>
96
96
  - !ruby/object:Gem::Version
97
97
  version: '1.0'
98
98
  description: Wrest is a fluent, easy-to-use, object oriented Ruby HTTP/REST client
@@ -106,58 +106,43 @@ extensions: []
106
106
  extra_rdoc_files:
107
107
  - README.md
108
108
  files:
109
- - CHANGELOG
110
- - LICENCE
111
- - README.md
112
109
  - bin/wrest
113
110
  - bin/wrest_shell.rb
114
- - lib/wrest.rb
115
- - lib/wrest/async_request.rb
116
111
  - lib/wrest/async_request/event_machine_backend.rb
117
112
  - lib/wrest/async_request/thread_backend.rb
118
113
  - lib/wrest/async_request/thread_pool.rb
114
+ - lib/wrest/async_request.rb
119
115
  - lib/wrest/cache_proxy.rb
120
- - lib/wrest/caching.rb
121
116
  - lib/wrest/caching/memcached.rb
117
+ - lib/wrest/caching/redis.rb
118
+ - lib/wrest/caching.rb
122
119
  - lib/wrest/callback.rb
123
- - lib/wrest/components.rb
124
- - lib/wrest/components/container.rb
125
120
  - lib/wrest/components/container/alias_accessors.rb
126
121
  - lib/wrest/components/container/typecaster.rb
127
- - lib/wrest/components/mutators.rb
122
+ - lib/wrest/components/container.rb
128
123
  - lib/wrest/components/mutators/base.rb
129
124
  - lib/wrest/components/mutators/camel_to_snake_case.rb
130
125
  - lib/wrest/components/mutators/xml_mini_type_caster.rb
131
126
  - lib/wrest/components/mutators/xml_simple_type_caster.rb
132
- - lib/wrest/components/translators.rb
127
+ - lib/wrest/components/mutators.rb
133
128
  - lib/wrest/components/translators/content_types.rb
134
129
  - lib/wrest/components/translators/json.rb
135
130
  - lib/wrest/components/translators/txt.rb
136
131
  - lib/wrest/components/translators/xml.rb
137
- - lib/wrest/core_ext/hash.rb
132
+ - lib/wrest/components/translators.rb
133
+ - lib/wrest/components.rb
138
134
  - lib/wrest/core_ext/hash/conversions.rb
139
- - lib/wrest/core_ext/string.rb
135
+ - lib/wrest/core_ext/hash.rb
140
136
  - lib/wrest/core_ext/string/conversions.rb
141
- - lib/wrest/curl.rb
142
- - lib/wrest/curl/delete.rb
143
- - lib/wrest/curl/get.rb
144
- - lib/wrest/curl/options.rb
145
- - lib/wrest/curl/post.rb
146
- - lib/wrest/curl/post_multipart.rb
147
- - lib/wrest/curl/put.rb
148
- - lib/wrest/curl/put_multipart.rb
149
- - lib/wrest/curl/request.rb
150
- - lib/wrest/curl/response.rb
151
- - lib/wrest/curl/session.rb
137
+ - lib/wrest/core_ext/string.rb
152
138
  - lib/wrest/exceptions.rb
153
139
  - lib/wrest/hash_with_case_insensitive_access.rb
154
140
  - lib/wrest/http_codes.rb
155
- - lib/wrest/http_shared.rb
156
141
  - lib/wrest/http_shared/headers.rb
157
142
  - lib/wrest/http_shared/standard_headers.rb
158
143
  - lib/wrest/http_shared/standard_tokens.rb
144
+ - lib/wrest/http_shared.rb
159
145
  - lib/wrest/multipart.rb
160
- - lib/wrest/native.rb
161
146
  - lib/wrest/native/connection_factory.rb
162
147
  - lib/wrest/native/delete.rb
163
148
  - lib/wrest/native/get.rb
@@ -170,38 +155,43 @@ files:
170
155
  - lib/wrest/native/request.rb
171
156
  - lib/wrest/native/response.rb
172
157
  - lib/wrest/native/session.rb
173
- - lib/wrest/test.rb
158
+ - lib/wrest/native.rb
174
159
  - lib/wrest/test/request_patches.rb
175
- - lib/wrest/uri.rb
160
+ - lib/wrest/test.rb
176
161
  - lib/wrest/uri/builders.rb
162
+ - lib/wrest/uri.rb
177
163
  - lib/wrest/uri_template.rb
178
164
  - lib/wrest/version.rb
179
- - lib/wrest/xml_mini.rb
180
- - lib/wrest/xml_mini/jdom.rb
181
165
  - lib/wrest/xml_mini/jdom/xpath_filter.rb
182
- - lib/wrest/xml_mini/libxml.rb
166
+ - lib/wrest/xml_mini/jdom.rb
183
167
  - lib/wrest/xml_mini/libxml/xpath_filter.rb
184
- - lib/wrest/xml_mini/nokogiri.rb
168
+ - lib/wrest/xml_mini/libxml.rb
185
169
  - lib/wrest/xml_mini/nokogiri/xpath_filter.rb
186
- - lib/wrest/xml_mini/rexml.rb
170
+ - lib/wrest/xml_mini/nokogiri.rb
187
171
  - lib/wrest/xml_mini/rexml/xpath_filter.rb
172
+ - lib/wrest/xml_mini/rexml.rb
173
+ - lib/wrest/xml_mini.rb
174
+ - lib/wrest.rb
188
175
  - lib/wrest_no_ext.rb
176
+ - README.md
177
+ - CHANGELOG
178
+ - LICENCE
189
179
  homepage: http://c42.in/open_source
190
180
  licenses: []
191
181
  metadata: {}
192
182
  post_install_message:
193
183
  rdoc_options:
194
- - "--charset=UTF-8"
184
+ - --charset=UTF-8
195
185
  require_paths:
196
186
  - lib
197
187
  required_ruby_version: !ruby/object:Gem::Requirement
198
188
  requirements:
199
- - - ">="
189
+ - - '>='
200
190
  - !ruby/object:Gem::Version
201
191
  version: '0'
202
192
  required_rubygems_version: !ruby/object:Gem::Requirement
203
193
  requirements:
204
- - - ">="
194
+ - - '>='
205
195
  - !ruby/object:Gem::Version
206
196
  version: 1.3.0
207
197
  requirements:
@@ -211,10 +201,9 @@ requirements:
211
201
  (and should be unneccessary) on jruby.
212
202
  - To use eventmachine as a parallel backend, install the 'eventmachine' gem.
213
203
  rubyforge_project: wrest
214
- rubygems_version: 2.4.5
204
+ rubygems_version: 2.0.14
215
205
  signing_key:
216
206
  specification_version: 4
217
207
  summary: Wrest is a fluent, object oriented HTTP client library for 2.x.x, JRuby 1.7.6
218
208
  (and higher), JRuby 9.0.0.0.pre2.
219
209
  test_files: []
220
- has_rdoc:
@@ -1,51 +0,0 @@
1
- # Copyright 2009 Sidu Ponnappa
2
-
3
- # Licensed under the Apache License, Version 2.0 (the "License");
4
- # you may not use this file except in compliance with the License.
5
- # You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
6
- # Unless required by applicable law or agreed to in writing, software distributed under the License
7
- # is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
8
- # See the License for the specific language governing permissions and limitations under the License.
9
-
10
- begin
11
- gem 'patron', '~> 0.4'
12
- rescue Gem::LoadError => e
13
- Wrest.logger.debug "Patron ~> 0.4.x not found. Patron is necessary to use libcurl. To install Patron run `sudo gem install patron` (patron is not available on JRuby, but you shouldn't need it anyway)."
14
- raise e
15
- end
16
- require 'patron'
17
-
18
- module Wrest
19
- # Contains all HTTP protocol related classes such as
20
- # Get, Post, Request, Response etc. and uses Curl for
21
- # better performance, but works only on CRuby and only on a *nix OS.
22
- #
23
- # This functionality is under development and the Wrest API
24
- # may not cover using it fully.
25
- #
26
- # Note:
27
- # * The Curl based APIs do *not* support the HTTP 'options' verb.
28
- # * Since auto-redirect is natively supported by Curl, auto-redirect may
29
- # behave differently from the native implementation for now.
30
- module Curl
31
- include Wrest::HttpShared
32
- end
33
- end
34
-
35
- module Patron #:nodoc:
36
- # Patching Patron::Session#handle_request to make it public.
37
- class Session
38
- public :handle_request
39
- end
40
- end
41
-
42
- require "wrest/curl/response"
43
- require "wrest/curl/request"
44
- require "wrest/curl/get"
45
- require "wrest/curl/put"
46
- require "wrest/curl/post"
47
- require "wrest/curl/delete"
48
- require "wrest/curl/options"
49
- require "wrest/curl/post_multipart"
50
- require "wrest/curl/put_multipart"
51
- # require "wrest/curl/session"
@@ -1,23 +0,0 @@
1
- # Copyright 2009 Sidu Ponnappa
2
-
3
- # Licensed under the Apache License, Version 2.0 (the "License");
4
- # you may not use this file except in compliance with the License.
5
- # You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
6
- # Unless required by applicable law or agreed to in writing, software distributed under the License
7
- # is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
8
- # See the License for the specific language governing permissions and limitations under the License.
9
-
10
- module Wrest::Curl
11
- class Delete < Request
12
- def initialize(wrest_uri, parameters = {}, headers = {}, options = {})
13
- super(
14
- wrest_uri,
15
- :delete,
16
- parameters,
17
- nil,
18
- headers,
19
- options
20
- )
21
- end
22
- end
23
- end
@@ -1,23 +0,0 @@
1
- # Copyright 2009 Sidu Ponnappa
2
-
3
- # Licensed under the Apache License, Version 2.0 (the "License");
4
- # you may not use this file except in compliance with the License.
5
- # You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
6
- # Unless required by applicable law or agreed to in writing, software distributed under the License
7
- # is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
8
- # See the License for the specific language governing permissions and limitations under the License.
9
-
10
- module Wrest::Curl
11
- class Get < Request
12
- def initialize(wrest_uri, parameters = {}, headers = {}, options = {})
13
- super(
14
- wrest_uri,
15
- :get,
16
- parameters,
17
- nil,
18
- headers,
19
- options
20
- )
21
- end
22
- end
23
- end
@@ -1,16 +0,0 @@
1
- # Copyright 2009 Sidu Ponnappa
2
-
3
- # Licensed under the Apache License, Version 2.0 (the "License");
4
- # you may not use this file except in compliance with the License.
5
- # You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
6
- # Unless required by applicable law or agreed to in writing, software distributed under the License
7
- # is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
8
- # See the License for the specific language governing permissions and limitations under the License.
9
-
10
- module Wrest::Curl
11
- class Options < Request
12
- def initialize(*args)
13
- raise Wrest::Exceptions::UnsupportedHttpVerb, 'Options'
14
- end
15
- end
16
- end
@@ -1,23 +0,0 @@
1
- # Copyright 2009 Sidu Ponnappa
2
-
3
- # Licensed under the Apache License, Version 2.0 (the "License");
4
- # you may not use this file except in compliance with the License.
5
- # You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
6
- # Unless required by applicable law or agreed to in writing, software distributed under the License
7
- # is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
8
- # See the License for the specific language governing permissions and limitations under the License.
9
-
10
- module Wrest::Curl
11
- class Post < Request
12
- def initialize(wrest_uri, body = '', headers = {}, parameters = {}, options = {})
13
- super(
14
- wrest_uri,
15
- :post,
16
- parameters,
17
- body,
18
- headers,
19
- options
20
- )
21
- end
22
- end
23
- end
@@ -1,31 +0,0 @@
1
- # Copyright 2009 - 2010 Sidu Ponnappa
2
-
3
- # Licensed under the Apache License, Version 2.0 (the "License");
4
- # you may not use this file except in compliance with the License.
5
- # You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
6
- # Unless required by applicable law or agreed to in writing, software distributed under the License
7
- # is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
8
- # See the License for the specific language governing permissions and limitations under the License.
9
-
10
- module Wrest::Curl
11
- class PostMultipart < Request
12
- def initialize(wrest_uri, parameters = {}, headers = {}, options = {})
13
- parameters = parameters.symbolize_keys
14
-
15
- data = parameters[:data] ? {:data => parameters[:data]} : {:data => " "}
16
- file = parameters[:file].is_a?(File) ? {:file => parameters[:file].path} : {:file => parameters[:file]}
17
-
18
- options = options.merge({:data => data, :file => file, :multipart => true})
19
- parameters.delete(:data)
20
- parameters.delete(:file)
21
- super(
22
- wrest_uri,
23
- :post,
24
- parameters,
25
- options[:data],
26
- headers,
27
- options
28
- )
29
- end
30
- end
31
- end
@@ -1,23 +0,0 @@
1
- # Copyright 2009 Sidu Ponnappa
2
-
3
- # Licensed under the Apache License, Version 2.0 (the "License");
4
- # you may not use this file except in compliance with the License.
5
- # You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
6
- # Unless required by applicable law or agreed to in writing, software distributed under the License
7
- # is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
8
- # See the License for the specific language governing permissions and limitations under the License.
9
-
10
- module Wrest::Curl
11
- class Put < Request
12
- def initialize(wrest_uri, body = '', headers = {}, parameters = {}, options = {})
13
- super(
14
- wrest_uri,
15
- :put,
16
- parameters,
17
- body,
18
- headers,
19
- options
20
- )
21
- end
22
- end
23
- end
@@ -1,16 +0,0 @@
1
- # Copyright 2009 - 2010 Sidu Ponnappa
2
-
3
- # Licensed under the Apache License, Version 2.0 (the "License");
4
- # you may not use this file except in compliance with the License.
5
- # You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
6
- # Unless required by applicable law or agreed to in writing, software distributed under the License
7
- # is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
8
- # See the License for the specific language governing permissions and limitations under the License.
9
-
10
- module Wrest::Curl
11
- class PutMultipart < Request
12
- def initialize(wrest_uri, parameters = {}, headers = {}, options = {})
13
- raise Wrest::Exceptions::UnsupportedFeature, 'put_multipart'
14
- end
15
- end
16
- end
@@ -1,102 +0,0 @@
1
- # Copyright 2009 Sidu Ponnappa
2
-
3
- # Licensed under the Apache License, Version 2.0 (the "License");
4
- # you may not use this file except in compliance with the License.
5
- # You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
6
- # Unless required by applicable law or agreed to in writing, software distributed under the License
7
- # is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
8
- # See the License for the specific language governing permissions and limitations under the License.
9
-
10
- module Wrest
11
- module Curl
12
- # This represents a HTTP request. Typically you will never need to instantiate
13
- # one of these yourself - you can use one of the more conveient APIs via Wrest::Uri
14
- # or Wrest::Curl::Get etc. instead.
15
- class Request
16
- attr_reader :http_request, :uri, :body, :headers, :username, :password, :follow_redirects,
17
- :follow_redirects_limit, :timeout, :connection, :parameters, :auth_type, :multipart, :file_name
18
- # Valid tuples for the options are:
19
- # :username => String, defaults to nil
20
- # :password => String, defaults to nil
21
- # :follow_redirects => Boolean, defaults to true for Get, false for anything else
22
- # :follow_redirects_limit => Integer, defaults to 5. This is the number of redirects
23
- # that Wrest will automatically follow before raising an
24
- # Wrest::Exceptions::AutoRedirectLimitExceeded exception.
25
- # For example, if you set this to 1, the very first redirect
26
- # will raise the exception.
27
- # :timeout => The period, in seconds, after which a Timeout::Error is raised
28
- # in the event of a connection failing to open. Defaulted to 60 by Uri#create_connection.
29
- # :connection => The HTTP Connection object to use. This is how a keep-alive connection can be
30
- # used for multiple requests. Not yet fully implemented for Curl.
31
- #
32
- # Curl specific options:
33
- # :auth_type => This is a curl specific option and can be one of :basic, :digest, or :any. The default is :basic.
34
- def initialize(wrest_uri, http_verb, parameters = {}, body = nil, headers = {}, options = {})
35
- @uri = wrest_uri
36
- @headers = headers.stringify_keys
37
- @parameters = parameters
38
- @body = body
39
-
40
- @options = options.clone
41
- @multipart = @options[:multipart]
42
- @file_name = @options[:file]
43
- @auth_type = @options[:auth_type] || :basic
44
- @username = @options[:username]
45
- @password = @options[:password]
46
- @follow_redirects = (@options[:follow_redirects] ||= false)
47
-
48
- @follow_redirects_limit = (@options[:follow_redirects_limit] ||= 5)
49
- @timeout = @options[:timeout] || 60
50
- @connection = @options[:connection]
51
-
52
- @http_request = Patron::Request.new
53
- @http_request.action = http_verb
54
-
55
- @http_request.headers = headers
56
- @http_request.username = username
57
- @http_request.password = password
58
- @http_request.multipart = multipart
59
- @http_request.upload_data = body
60
- @http_request.file_name = file_name
61
- @http_request.auth_type = auth_type
62
- @http_request.url = parameters.empty? ? uri.to_s : "#{uri.to_s}?#{parameters.to_query}"
63
- @http_request.max_redirects = follow_redirects_limit if follow_redirects
64
- @http_request.timeout = @timeout
65
- end
66
-
67
- # Makes a request and returns a Wrest::Native::Response.
68
- # Data about the request is and logged to Wrest.logger
69
- # The log entry contains the following information:
70
- #
71
- # <- indicates a request
72
- # -> indicates a response
73
- #
74
- # The type of request is mentioned in caps, followed by a hash
75
- # uniquely uniquely identifying a particular request/response pair.
76
- # In a multi-process or multi-threaded scenario, this can be used
77
- # to identify request-response pairs.
78
- #
79
- # The request hash is followed by a connection hash; requests using the
80
- # same connection (effectively a keep-alive connection) will have the
81
- # same connection hash.
82
- #
83
- # This is followed by the response code, the payload size and the time taken.
84
- def invoke
85
- response = nil
86
-
87
- @connection ||= Patron::Session.new
88
- raise ArgumentError, "Empty URL" if http_request.url.empty?
89
-
90
- prefix = "#{http_request.action.to_s.upcase} #{http_request.hash} #{connection.hash} #{Thread.current.object_id}"
91
-
92
- Wrest.logger.debug "<- (#{prefix}) #{http_request.url}"
93
- time = Benchmark.realtime { response = Wrest::Curl::Response.new(connection.handle_request(http_request))}
94
- Wrest.logger.debug "-> (#{prefix}) %s (%d bytes %.2fs)" % [response.message, response.body ? response.body.length : 0, time]
95
-
96
- response
97
- rescue Patron::TimeoutError => e
98
- raise Wrest::Exceptions::Timeout.new(e)
99
- end
100
- end
101
- end
102
- end
@@ -1,85 +0,0 @@
1
- # Copyright 2009 Sidu Ponnappa
2
-
3
- # Licensed under the Apache License, Version 2.0 (the "License");
4
- # you may not use this file except in compliance with the License.
5
- # You may obtain a copy of the License at Http://www.apache.org/licenses/LICENSE-2.0
6
- # Unless required by applicable law or agreed to in writing, software distributed under the License
7
- # is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
8
- # See the License for the specific language governing permissions and limitations under the License.
9
-
10
- module Wrest #:nodoc:
11
- module Curl #:nodoc:
12
- # Decorates a response providing support for deserialisation.
13
- #
14
- # Also provides set of HTTP response code checkers. For instance, the method ok? checks if the response was
15
- # successful with HTTP code 200.
16
- # See HttpCodes for a list of all such response checkers.
17
- class Response
18
- attr_reader :http_response
19
- attr_accessor :deserialised_body
20
- include HttpShared::Headers
21
- include HttpCodes
22
-
23
- extend Forwardable
24
- def_delegators :@http_response, :body, :headers
25
-
26
- def initialize_http_header
27
- headers.merge!(headers.inject({}) do |downcased_headers, tuple|
28
- key, value = tuple
29
- downcased_headers[key.downcase] = value
30
- downcased_headers
31
- end)
32
- end
33
-
34
- def initialize(http_response)
35
- @http_response = http_response
36
- initialize_http_header
37
- end
38
-
39
- def deserialise(options = {})
40
- @deserialised_body ||= deserialise_using(Wrest::Components::Translators.lookup(content_type),options)
41
- end
42
-
43
- def deserialize(options = {})
44
- deserialise(options)
45
- end
46
-
47
- def deserialise_using(translator, options={})
48
- translator.deserialise(@http_response,options)
49
- end
50
-
51
- def deserialize_using(options = {})
52
- deserialise_using(options)
53
- end
54
-
55
- def code
56
- @http_response.status
57
- end
58
-
59
- def message
60
- @http_response.status_line
61
- end
62
-
63
- def content_length
64
- self[H::ContentLength].try(:to_i)
65
- end
66
-
67
- def content_type
68
- self[H::ContentType].split(';').first
69
- end
70
-
71
- # A null object implementation - invoking this method on
72
- # a response simply returns the same response unless
73
- # the response is a Redirection (code 3xx), in which case a
74
- # get is invoked on the url stored in the response headers
75
- # under the key 'location' and the new Response is returned.
76
- def follow(redirect_request_options = {})
77
- self
78
- end
79
-
80
- def connection_closed?
81
- self[StandardHeaders::Connection].downcase == StandardTokens::Close.downcase
82
- end
83
- end
84
- end
85
- end
@@ -1,57 +0,0 @@
1
- # Copyright 2009 Sidu Ponnappa
2
-
3
- # Licensed under the Apache License, Version 2.0 (the "License");
4
- # you may not use this file except in compliance with the License.
5
- # You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
6
- # Unless required by applicable law or agreed to in writing, software distributed under the License
7
- # is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
8
- # See the License for the specific language governing permissions and limitations under the License.
9
-
10
- module Wrest::Curl
11
- # This class is a wrapper for a keep-alive HTTP connection. It simply passes the
12
- # same connection instance as an option to all Wrest::Native::Request instances created using it.
13
- #
14
- # If at any point the server closes an existing connection during a Session by returning a
15
- # Connection: Close header the current connection is destroyed and a fresh one created for the next
16
- # request.
17
- #
18
- # The Session constructor can accept either a String URI or a Wrest::Uri as a parameter. If you
19
- # need HTTP authentication, you should use a Wrest::Uri.
20
- class Session
21
- attr_reader :uri
22
- def initialize(uri)
23
- @uri = Wrest::Uri.new(uri)
24
- @default_headers = { StandardHeaders::Connection => StandardTokens::KeepAlive }
25
-
26
- yield(self) if block_given?
27
- end
28
-
29
- def connection
30
- @connection ||= @uri.create_connection
31
- end
32
-
33
- def get(path = '', parameters = {}, headers = {})
34
- maybe_destroy_connection @uri[path, {:connection => self.connection}].get(parameters, headers.merge(@default_headers))
35
- end
36
-
37
- def post(path = '', body = '', headers = {}, params = {})
38
- maybe_destroy_connection @uri[path, {:connection => self.connection}].post(body, headers.merge(@default_headers), params)
39
- end
40
-
41
- def put(path = '', body = '', headers = {}, params = {})
42
- maybe_destroy_connection @uri[path, {:connection => self.connection}].put(body, headers.merge(@default_headers), params)
43
- end
44
-
45
- def delete(path = '', parameters = {}, headers = {})
46
- maybe_destroy_connection @uri[path, {:connection => self.connection}].delete(parameters, headers.merge(@default_headers))
47
- end
48
-
49
- def maybe_destroy_connection(response)
50
- if response.connection_closed?
51
- Wrest.logger.warn "Connection #{@connection.hash} has been closed by the server"
52
- @connection = nil
53
- end
54
- response
55
- end
56
- end
57
- end