wrest 4.0.0-universal-java-18

Sign up to get free protection for your applications and to get access to all the features.
Files changed (66) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG +169 -0
  3. data/LICENCE +7 -0
  4. data/README.md +436 -0
  5. data/bin/wrest +4 -0
  6. data/bin/wrest_shell.rb +23 -0
  7. data/lib/wrest/async_request/event_machine_backend.rb +32 -0
  8. data/lib/wrest/async_request/thread_backend.rb +34 -0
  9. data/lib/wrest/async_request/thread_pool.rb +29 -0
  10. data/lib/wrest/async_request.rb +51 -0
  11. data/lib/wrest/cache_proxy.rb +119 -0
  12. data/lib/wrest/caching/memcached.rb +37 -0
  13. data/lib/wrest/caching/redis.rb +38 -0
  14. data/lib/wrest/caching.rb +57 -0
  15. data/lib/wrest/callback.rb +70 -0
  16. data/lib/wrest/components/container/alias_accessors.rb +70 -0
  17. data/lib/wrest/components/container/typecaster.rb +178 -0
  18. data/lib/wrest/components/container.rb +204 -0
  19. data/lib/wrest/components/mutators/base.rb +65 -0
  20. data/lib/wrest/components/mutators/camel_to_snake_case.rb +26 -0
  21. data/lib/wrest/components/mutators/xml_type_caster.rb +56 -0
  22. data/lib/wrest/components/mutators.rb +42 -0
  23. data/lib/wrest/components/translators/content_types.rb +25 -0
  24. data/lib/wrest/components/translators/json.rb +36 -0
  25. data/lib/wrest/components/translators/txt.rb +35 -0
  26. data/lib/wrest/components/translators/xml/conversions.rb +56 -0
  27. data/lib/wrest/components/translators/xml.rb +77 -0
  28. data/lib/wrest/components/translators.rb +30 -0
  29. data/lib/wrest/components.rb +22 -0
  30. data/lib/wrest/core_ext/hash/conversions.rb +45 -0
  31. data/lib/wrest/core_ext/hash.rb +7 -0
  32. data/lib/wrest/core_ext/string/conversions.rb +38 -0
  33. data/lib/wrest/core_ext/string.rb +7 -0
  34. data/lib/wrest/exceptions.rb +38 -0
  35. data/lib/wrest/hash_with_case_insensitive_access.rb +52 -0
  36. data/lib/wrest/hash_with_indifferent_access.rb +442 -0
  37. data/lib/wrest/http_codes.rb +83 -0
  38. data/lib/wrest/http_shared/headers.rb +345 -0
  39. data/lib/wrest/http_shared/standard_headers.rb +22 -0
  40. data/lib/wrest/http_shared/standard_tokens.rb +21 -0
  41. data/lib/wrest/http_shared.rb +25 -0
  42. data/lib/wrest/multipart.rb +84 -0
  43. data/lib/wrest/native/connection_factory.rb +28 -0
  44. data/lib/wrest/native/delete.rb +27 -0
  45. data/lib/wrest/native/get.rb +83 -0
  46. data/lib/wrest/native/options.rb +27 -0
  47. data/lib/wrest/native/patch.rb +27 -0
  48. data/lib/wrest/native/post.rb +27 -0
  49. data/lib/wrest/native/post_multipart.rb +36 -0
  50. data/lib/wrest/native/put.rb +27 -0
  51. data/lib/wrest/native/put_multipart.rb +36 -0
  52. data/lib/wrest/native/redirection.rb +39 -0
  53. data/lib/wrest/native/request.rb +161 -0
  54. data/lib/wrest/native/response.rb +278 -0
  55. data/lib/wrest/native/session.rb +66 -0
  56. data/lib/wrest/native.rb +36 -0
  57. data/lib/wrest/test/request_patches.rb +12 -0
  58. data/lib/wrest/test.rb +3 -0
  59. data/lib/wrest/uri/builders.rb +48 -0
  60. data/lib/wrest/uri.rb +312 -0
  61. data/lib/wrest/uri_template.rb +63 -0
  62. data/lib/wrest/utils.rb +129 -0
  63. data/lib/wrest/version.rb +14 -0
  64. data/lib/wrest.rb +77 -0
  65. data/lib/wrest_no_ext.rb +7 -0
  66. metadata +286 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 768ed7a4410b06057c4c23e37ab36ade44d060c1313109bbe7d9d195a9bfe602
4
+ data.tar.gz: 29a699466377f026813099511222a65915992bcced7f72e8b2f82dda70291762
5
+ SHA512:
6
+ metadata.gz: 3e49857f01c85efa8b22d8dd8207a42bcf7e0107f599d7e725c434d9b314a616f041321db13f7900790fb09d8ef2ea02016dee57ab823dbee493c61ece91e40c
7
+ data.tar.gz: e38818230a7917bb029d1b697a84cbd6c78b1db5d3550c392c432e9c5d54a051970dd12b0b888c2c9ab6b1157ec5437ef5ee59040c54c64f17c953fda7a889db
data/CHANGELOG ADDED
@@ -0,0 +1,169 @@
1
+ Features under the section marked 'Current' are completed but pending release as a gem. If you need any of these, you'll need to use the latest source from the git repository.
2
+
3
+ == Current
4
+ * Upgrade gems and fix build on Ruby 3.1.2
5
+
6
+ Features under a numbered section are complete and available in the Wrest gem.
7
+
8
+ == 2.2.0
9
+ * Add support for HTTP PATCH [Aditi Raveesh]
10
+
11
+ == 2.1.9
12
+ * Switch to using concurrent-ruby
13
+
14
+ == 2.1.6
15
+ * Logging payload by default
16
+
17
+ == 2.1.4
18
+ * Making cache keys include query params
19
+
20
+ == 2.1.3
21
+ * Adding cache expiry for caches using request headers
22
+
23
+ == 2.1.2
24
+ * Setting cache keys as URI strings
25
+
26
+ == 2.1.1
27
+ * Including Redis as a Caching Backend
28
+
29
+ == 2.0.0
30
+ * Removing support for libcurl
31
+
32
+ == 1.5.4
33
+ * Make thread pool configurable
34
+
35
+ == 1.5.3
36
+ * Implemented a thread pool for async requests using ThreadBackend
37
+ * Updated request/response logging to include current thread id
38
+
39
+ == 1.5.2
40
+ * Updated dependencies - ActiveSupport, MultipartPost, Dalli and JRuby OpenSSL
41
+
42
+ == 1.5.1
43
+ * Supports Ruby 2.x.x, JRuby 1.7.6 (and higher), JRuby 9.0.0.0.pre2
44
+
45
+ == 1.5.0
46
+ * Introducing content type text/plain - pull request by buchin
47
+
48
+ == 1.4.7
49
+ * Switch to using multi_json to manage Json backend to play nice with Rails 3.1.
50
+
51
+ == 1.4.6
52
+ * Bump Builder dependency to allow both Builder 2.x and 3.x to be used
53
+
54
+ == 1.4.5
55
+ * GH#107 alias :deserialize :deserialise
56
+
57
+ == 1.4.4
58
+ * GH#105 Reduce Rubgems version requirements to 1.3.x or greater from > 1.5
59
+
60
+ == 1.4.3
61
+ * GH#104 Allow default headers to be added to the uri that are then used for all requests
62
+ * GH#31 Add explicit API for cookies
63
+
64
+ == 1.4.2
65
+ * GH#102 Code and gemspec mismatch for json-jruby
66
+
67
+ == 1.4.1
68
+ * GH#66 HTTPS requests using the Net::HTTP backend failing on Ruby 1.8.x
69
+
70
+ == 1.4.0
71
+ * GH#96 Naive implementation of asynchronous requests using Eventmachine
72
+ * GH#93 Since Wrest::Response is immutable, the results of Response#deserialise are cached
73
+
74
+ == 1.3.0
75
+ * GH#95 Asynchronous requests on Wrest::Uri.
76
+
77
+ == 1.2.1
78
+ * GH#91 Remove dependency on tzinfo
79
+
80
+ == 1.2.0
81
+ * GH#80 Add a convenience API to make it easy to use the existing URI callback API
82
+
83
+ == 1.1.0
84
+ * Caching support introduced. Includes Memcached support for the cache store. (GH# 69, 83, 87)
85
+
86
+ == 1.0.2
87
+ * GH#12 Post Multipart support when using the Patron adapter
88
+ * GH#72 Response code checkers: ok?, redirect?, created? etc.
89
+ * GH#85 Do not auto-load Nokogiri/libxml-ruby and remove warnings at startup
90
+ * GH#79 Update Patron version.
91
+
92
+ == 1.0.1
93
+ * GH#61 Option propagate while converting uri to UriTemplate and back.
94
+ * GH#64 Removed Caching.
95
+ * GH#32 Callback for response status codes.
96
+ * GH#53 Add to_uri_template helper to String
97
+ * GH#12 Add Post/Put Multipart support when using the Patron adapter
98
+ * GH#72 Response code checkers: ok?, redirect?, created? etc.
99
+
100
+ == 1.0.0
101
+ * GH#56 Detailed Net::HTTP debug output option added to Wrest::Native:Request.
102
+ * GH#60 Wrest debug log arrow direction changed to be consistent with Net::HTPP debug log
103
+ * GH#59 follow_redirects_limit is off by one
104
+ * GH#24 Add support for xpath based filtering of xml response bodies to the response chain
105
+ * GH#46 Response.deserialise for Json responses
106
+ * GH#52 Wrest console (bin/wrest) fails on 1.9.2
107
+ * GH#55 Fix connection.verify_mode for Net::HTTP https connection to VERIFY_PEER
108
+ * GH#57 Feature #24 doesn't autoload correctly
109
+
110
+ == 1.0.0.beta7
111
+ * GH#46 Response.deserialise for Json responses
112
+
113
+ == 1.0.0.beta6
114
+ * GH#35 Wrest::UriTemplate extensions swallow existing path
115
+ * GH#41 Make Hash core_ext opt out
116
+
117
+ == 1.0.0.beta5
118
+ * GH#30 Replace rails app in spec/sample_rails_app with a lighter sinatra app
119
+ * GH#37 Allow opting out of Adding to_uri to string
120
+
121
+ == 1.0.0.beta4
122
+ * GH#34 Wrest::Uri extensions swallow existing path
123
+
124
+ == 1.0.0.beta3
125
+ * GH#13 Works for get and delete with parameters appended to the uri string and
126
+ with ? and no parameters appended to the uri string
127
+ * GH#26 'http://localhost'.to_uri['foo/bar'] fails
128
+ * GH#11 Handle gem dependencies using bundler
129
+ * GH#22 Converts Uri to UriTemplate
130
+ * GH#23 Supports extension of UriTemplate via #[]
131
+
132
+ == 1.0.0.beta2
133
+ * GH#2 Make Components::Containers hash store a HashWithIndifferentAccess
134
+ * GH#6 Net::HTTP based calls to Twitter public APIs fail
135
+ * GH#5 Patron (curl) support failing for responses where headers have multiple values
136
+ * GH#7 Build fails on Ruby 1.9
137
+ * GH#16 Remove jeweler and use .gemspec direcly
138
+
139
+ == 1.0.0.beta1
140
+ * If libxml-ruby isn't found but nokogiri, don't warn about libxml-ruby
141
+
142
+ == 1.0.0.beta0
143
+ * Dependencies updated to ActiveSupport 3.0.0.rc and RSpec 2.0.0.beta19
144
+
145
+ == 0.1.2
146
+ * Locked gem dependencies to ActiveSupport 2.3.8 and rspec 1.3.0 to avoid breakages caused by inadvertently requiring the Rails 3.0 and Rspec 2.0 gems which have changed greatly
147
+
148
+ == 0.1.1
149
+ * Multipart post and put using Net::Http
150
+
151
+ == 0.1.0
152
+ * Added Nokogiri as a fallback for LibXML Ruby before we give up and use REXML. (Nokogiri is available on JRuby, unlike LibXML-ruby)
153
+ * Added code to attempt to load JREXML (for what it's worth) when using REXML on JRuby
154
+ * Ensure that Wrest throws an exception when an actual request is made in a test environment in Rails (RAILS_ENV == 'test')
155
+ * Support for keep-alive connections via Wrest::Http::Session and Wrest::Http::Response#connection_closed?
156
+ * Create two convenience Post methods on Uri - one for application/x-www-form-urlencoded and the other normal
157
+ * Optional libcurl implementations of the Request/Response classes (for those on MRI who need better performance). See examples/delicious.rb, examples/facebook.rb. Note that while there is a great deal of API compatibility, everything may not behave exactly the same under libcurl. Please do report inconsistencies.
158
+ * Added live functional tests to codebase (rake spec:functional, needs spec/functional/sample_rails_app running on port 3000)
159
+
160
+ == 0.0.9
161
+ * Added redirect follow support for GET requests (see examples/redirection.rb)
162
+ * Auto redirection limit
163
+ * Request timeout limit config
164
+ * Added an alias_accessor macro to allow alternative names for attribute accessors (see examples/wow_realm_status.rb)
165
+ * Removed the need to explicitly include AttributesContainer::Typecaster or to use the enable_typecasting_support macro. The call to typecast now does this for us. See AttributesContainer#typecast and AttributesContainer::Typecaster#typecast
166
+
167
+ == 0.0.8
168
+ * Added URI extension using the [] method (see examples/delicious.rb)
169
+ * Refactored out Request as the first step to adding Response caching
data/LICENCE ADDED
@@ -0,0 +1,7 @@
1
+ Copyright 2009 - 2022 Sidu Ponnappa
2
+ Licensed under the Apache License, Version 2.0 (the "License");
3
+ you may not use this file except in compliance with the License.
4
+ You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
5
+ Unless required by applicable law or agreed to in writing, software distributed under the License
6
+ is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
7
+ See the License for the specific language governing permissions and limitations under the License.
data/README.md ADDED
@@ -0,0 +1,436 @@
1
+ # Wrest 4.0.0.dev
2
+
3
+ <p align="center"><img src="docs/wrest-logo-320x320.png" width="320"></p>
4
+ <p align="center">
5
+ <a href="https://rubygems.org/gems/wrest">
6
+ <img src="https://badge.fury.io/rb/wrest.svg" alt="Gem Version" height="18">
7
+ </a>
8
+ <a href="https://github.com/kaiwren/wrest/actions/workflows/ruby.yml">
9
+ <img src="https://github.com/kaiwren/wrest/actions/workflows/ruby.yml/badge.svg" alt="Ruby" style="max-width: 100%;">
10
+ </a>
11
+ <a href="https://github.com/kaiwren/wrest/actions/workflows/jruby.yml">
12
+ <img src="https://github.com/kaiwren/wrest/actions/workflows/jruby.yml/badge.svg" alt="JRuby" style="max-width: 100%;">
13
+ </a>
14
+ <a href="https://codeclimate.com/github/kaiwren/wrest/maintainability">
15
+ <img src="https://api.codeclimate.com/v1/badges/dc0366a60000561951ab/maintainability" />
16
+ </a>
17
+ </p>
18
+
19
+
20
+ Wrest is a ruby REST/HTTP client library. It is currently in use at 10x🦄 scale across all Ruby/JRuby systems at [Gojek](https://twitter.com/GojekTech).
21
+
22
+ * Quick tool to wrangle APIs to powerful library to build complex production grade systems, Wrest does it all
23
+ * Clean, object-oriented API with URLs as first class entities
24
+ * Supports RFC 2616 based [caching](https://github.com/kaiwren/wrest/blob/caching/Caching.markdown)
25
+ * Async http calls using Threads (truly useful only on JRuby due to [GIL](https://en.wikipedia.org/wiki/Global_interpreter_lock) limitations on CRuby) or EventMachine
26
+ * Is spec driven, strongly favours immutable objects and avoids class methods and setters making it better suited for use as a library, especially in multi-threaded environments
27
+ * Provides convenient HTTP wrappers, redirect handling, serialisation, deserialisation and xpath based lookup
28
+
29
+ Wrest is currently undergoing a substantial clean-up of syntax and dendencies to bring it up to speed on Ruby 3.x and JRuby 9.3.x. This will be released shortly as version 4.0.0.
30
+
31
+ ## Examples
32
+
33
+ For Facebook, Twitter, Delicious, GitHub and other API examples, see http://github.com/kaiwren/wrest/tree/master/examples
34
+
35
+ ### Basic Http Calls
36
+
37
+ #### GET
38
+
39
+ * Basic API calls
40
+
41
+ ```
42
+ # Works with json and xml out of the box to give you a hash
43
+ # See lib/wrest/components/translators to add other formats
44
+
45
+ 'https://api.github.com/repos/rails/rails/issues'.to_uri.get.deserialize
46
+ ```
47
+
48
+ * Timeout support
49
+
50
+ ```
51
+ 'https://api.github.com/repos/c42/wrest/issues'.to_uri(timeout: 5).get.body
52
+ ```
53
+
54
+ * Redirect support
55
+
56
+ ```
57
+ 'http://google.com'.to_uri(follow_redirects: false).get
58
+
59
+ 'http://google.com'.to_uri(follow_redirects_limit: 1).get
60
+ ```
61
+
62
+ :follow_redirects_limit defaults to 5 if not specified.
63
+
64
+ * Deserialise with XPath filtering
65
+
66
+ ```
67
+ 'http://twitter.com/statuses/public_timeline.xml'.to_uri.get.deserialise(
68
+ xpath: '//user/name/text()'
69
+ )
70
+ ```
71
+
72
+ * More complex request with parameters and a custom deserialiser
73
+
74
+ ```
75
+ 'api.openweathermap.org/data/2.5/weather'.to_uri.get(
76
+ lat: 35,
77
+ lon: 139
78
+ ).deserialise_using(
79
+ Wrest::Components::Translators::Xml
80
+ )
81
+ ```
82
+
83
+ * Basic HTTP auth and URI extensions using Wrest::Uri#[]
84
+
85
+ ```
86
+ base_uri = 'https://api.del.icio.us/v1'.to_uri(username: 'kaiwren', password: 'fupupp1es')
87
+ bookmarks = base_uri['/posts/get'].get.deserialise
88
+ ```
89
+
90
+ * Detailed debugging and logging (NOT FOR USE IN PRODUCTION! SEE API DOCS!)
91
+
92
+ ```
93
+ 'https://api.github.com/repos/c42/wrest/issues'.to_uri(detailed_http_logging: $stdout).get.deserialize
94
+ ```
95
+
96
+ #### POST
97
+
98
+ * Regular, vanilla Post with a body and headers
99
+
100
+ ```
101
+ 'http://my.api.com'.to_uri.post('YAML encoded body', 'Content-Type' => 'text/x-yaml')
102
+ ```
103
+
104
+ * Form encoded post
105
+
106
+ ```
107
+ 'https://api.del.icio.us/v1/posts/add'.to_uri(
108
+ username: 'kaiwren', password: 'fupupp1es'
109
+ ).post_form(
110
+ url: 'http://blog.sidu.in/search/label/ruby',
111
+ description: 'The Ruby related posts on my blog!',
112
+ extended: "All posts tagged with 'ruby'",
113
+ tags: 'ruby hacking'
114
+ )
115
+ ```
116
+
117
+ * Multipart posts
118
+
119
+ ```
120
+ 'http://imgur.com/api/upload.xml'.to_uri.post_multipart(
121
+ image: UploadIO.new(File.open(file_path), "image/png", file_path),
122
+ key: imgur_key
123
+ ).deserialise
124
+ ```
125
+
126
+ Note: To enable Multipart support, you'll have to explicitly require 'wrest/multipart', which depends on the multipart-post gem.
127
+
128
+ #### DELETE
129
+
130
+ To delete a resource:
131
+
132
+ ```
133
+ 'https://api.del.icio.us/v1/posts/delete'.to_uri(
134
+ username: 'kaiwren',
135
+ password: 'fupupp1es'
136
+ ).delete(
137
+ url: 'http://c2.com'
138
+ )
139
+ ```
140
+
141
+
142
+ ### Caching
143
+
144
+ Wrest supports caching with the following pluggable back-ends:
145
+ - Hash
146
+ - Memcached
147
+ - Redis
148
+
149
+ #### Hash
150
+
151
+ Use the following method to enable caching for all requests, and set Hash as the default cache store.
152
+ Note: Hash should NEVER be used in a production environment. It is unbounded and will keep increasing in size.
153
+
154
+ ```
155
+ Wrest::Caching.default_to_hash!
156
+ c42 = 'http://c42.in'.to_uri.get
157
+ ```
158
+
159
+ To use Hash as a cache store in an explicit request (without setting hash as default), use the following API:
160
+
161
+ ```
162
+ r1 = "http://c42.in".to_uri.using_hash.get
163
+ ```
164
+
165
+ #### Memcached
166
+
167
+ 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).
168
+ The Dalli gem is used by Wrest to interface with Memcached. Install dalli using 'gem install dalli'.
169
+
170
+ Use the following method to enable caching for all requests, and set Memcached as the default back-end.
171
+
172
+ ```
173
+ Wrest::Caching.default_to_memcached!
174
+ ```
175
+
176
+ To use Memcached as a cache store in an explicit request (without setting memcached as default), use the following API:
177
+
178
+ ```
179
+ Wrest::Caching.enable_memcached
180
+ r2 = "http://c42.in".to_uri.using_memcached.get
181
+ ```
182
+
183
+ #### Redis
184
+
185
+ Wrest also supports a Redis based caching back-end. Follow the guide [here](http://redis.io/topics/quickstart) to install Redis in your system.
186
+ It uses [redis-rd](https://github.com/redis/redis-rb) to interface with Redis. Install redis-rb using `gem install redis`.
187
+
188
+ Use the following method to enable caching for all requests, and set Redis as the default back-end.
189
+
190
+ ```
191
+ Wrest::Caching.default_to_redis!
192
+ ```
193
+
194
+ To use Redis as a cache store in an explicit request (without setting redis as default), use the following API:
195
+
196
+ ```
197
+ Wrest::Caching.enable_redis
198
+ r3 = "http://c42.in".to_uri.using_redis.get
199
+ ```
200
+
201
+ 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)
202
+
203
+ 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
204
+
205
+ To explicitly disable caching for specific requests:
206
+
207
+ ```
208
+ "http://c42.in".to_uri.disable_cache.get
209
+ ```
210
+
211
+ ### Callbacks
212
+
213
+ #### Uri level callbacks
214
+
215
+ You can define a set of callbacks that are invoked based on the http codes of the responses to any requests on a given uri.
216
+
217
+ ```
218
+ "http://google.com".to_uri(callback: {
219
+ 200 => lambda {|response| Wrest.logger.info "Ok." },
220
+ 400..499 => lambda {|response| Wrest.logger.error "Invalid. #{response.body}"},
221
+ 300..302 => lambda {|response| Wrest.logger.debug "Redirected. #{response.message}" }
222
+ }).get
223
+ ```
224
+
225
+ #### Per request callbacks
226
+
227
+ You can also define callbacks that are invoked based on the http code of the response to a particular request.
228
+
229
+ ```
230
+ "http://google.com".to_uri.get do |callback|
231
+ callback.on_ok do |response|
232
+ Wrest.logger.info "Ok."
233
+ end
234
+
235
+ callback.on(202) do |response|
236
+ Wrest.logger.info "Accepted."
237
+ end
238
+
239
+ callback.on(200..206) do |response|
240
+ Wrest.logger.info "Successful."
241
+ end
242
+ end
243
+ ```
244
+
245
+ Please note that Wrest is a synchronous library. All requests are blocking, and will not return till the request is completed and appropriate callbacks executed.
246
+
247
+ ### Asynchronous requests
248
+
249
+ Asynchronous requests are non-blocking. They do not return a response and the request is executed on a separate thread. The only way to access the response
250
+ while using asynchronous request is through callbacks.
251
+
252
+ Asynchronous requests support pluggable backends. The default backend used for asynchronous requests is ruby threads, which is only reliable when using JRuby.
253
+
254
+ ```
255
+ "http://c42.in".to_uri.get_async do |callback|
256
+ callback.on_ok do |response|
257
+ Wrest.logger.info "Ok."
258
+ end
259
+ end
260
+
261
+ # Wait until the background threads finish execution before letting the program end.
262
+ Wrest::AsyncRequest.wait_for_thread_pool!
263
+ ```
264
+
265
+ You can change the default to eventmachine or to threads.
266
+
267
+ ```
268
+ Wrest::AsyncRequest.default_to_em!
269
+ ```
270
+ or
271
+ ```
272
+ Wrest::AsyncRequest.default_to_threads!
273
+ ```
274
+
275
+ You can also override the default on Uri objects.
276
+
277
+ ```
278
+ "http://c42.in".to_uri.using_em.get_async do |callback|
279
+ callback.on_ok do |response|
280
+ Wrest.logger.info "Ok."
281
+ end
282
+ end
283
+ ```
284
+
285
+ You can decide which AsyncBackend to use at runtime through to `to_uri`'s options hash.
286
+
287
+ ```
288
+ "http://c42.in".to_uri(asynchronous_backend: ThreadBackend.new(number_of_threads)).get_async do |callback|
289
+ callback.on_ok do |response|
290
+ Wrest.logger.info "Ok."
291
+ end
292
+ end
293
+ ```
294
+
295
+
296
+ ### Other useful stuff
297
+
298
+ #### Hash container with ActiveResource-like semantics
299
+
300
+ Allows any class to hold an attributes hash, somewhat like ActiveResource. It also supports several extensions to this base fuctionality such as support for typecasting attribute values. See examples/twitter.rb and examples/wow_realm_status.rb for more samples.
301
+
302
+ Example:
303
+
304
+ ```
305
+ class Demon
306
+ include Wrest::Components::Container
307
+
308
+ always_has :id
309
+ typecast :age => as_integer,
310
+ :chi => lambda{|chi| Chi.new(chi)}
311
+
312
+ alias_accessors :chi => :energy
313
+ end
314
+
315
+ kai_wren = Demon.new('id' => '1', 'age' => '1500', 'chi' => '1024', 'teacher' => 'Viss')
316
+ kai_wren.id # => '1'
317
+ kai_wren.age # => 1500
318
+ kai_wren.chi # => #<Chi:0x113af8c @count="1024">
319
+ kai_wren.energy # => #<Chi:0x113af8c @count="1024">
320
+ kai_wren.teacher # => 'Viss'
321
+ ```
322
+
323
+ #### Opt-out of core extensions
324
+
325
+ Uncomfortable with extending `String` to add `to_uri`? Simply do
326
+
327
+ ```
328
+ gem "wrest", :require => "wrest_no_ext"
329
+ ```
330
+
331
+ in your Gemfile. You can now do `Uri.new('http://localhost')` to build Uris.
332
+
333
+ ### Logging
334
+
335
+ 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 :
336
+
337
+ ```
338
+ Wrest.logger = Rails.logger
339
+ ```
340
+
341
+ Every request and response is logged at level `debug`.
342
+
343
+ Here is an sample request log message:
344
+ ```
345
+ <- (POST 515036017 732688777 2010) http://localhost:3000/events.json
346
+ ```
347
+
348
+ The request log consists of request type (POST), request hash (515036017), connection hash (732688777), thread id (2010), URI (http://localhost:3000/events.json)
349
+
350
+ Here is a sample response log message:
351
+ ```
352
+ -> (POST 515036017 732688777 2010) 200 OK (0 bytes 0.01s)
353
+ ```
354
+ The response log consists of request type that generated the response (POST), hash of the request that generated the response (515036017), hash of the connection (732688777), thread id (2010), status (200 OK), response body length (0 bytes) and time taken (0.01)s.
355
+
356
+ The thread id, request hash and connection hashes are used to track requests and their corresponding responses when using asynchronous requests and/or http connection pooling.
357
+
358
+ Detailed http debug logging can be turned on like so (DO NOT USE IN PRODUCTION! SEE API DOCS.):
359
+
360
+ ```
361
+ 'https://api.github.com/repos/c42/wrest/issues'.to_uri(detailed_http_logging: $stdout).get.deserialize
362
+ ```
363
+
364
+ ### Build
365
+
366
+ Standard options are available and can be listed using `rake -T`. Use rake:rcov for coverage and rake:rdoc to generate documentation. The link to the continuous integration build is over at the C42 Engineering [open source](http://c42.in/open_source) page.
367
+
368
+ ## Documentation
369
+
370
+ Wrest RDocs can be found at http://wrest.rubyforge.org
371
+
372
+ ## Roadmap
373
+
374
+ Features that are planned, in progress or already implemented are documented in the [CHANGELOG](http://github.com/kaiwren/wrest/tree/master/CHANGELOG) starting from version 0.0.8.
375
+
376
+ ## Installation
377
+
378
+ The source is available at git://github.com/kaiwren/wrest.git
379
+
380
+ To install the Wrest gem, do `(sudo) gem install wrest`.
381
+
382
+ Wrest is currently available as a gem for for Ruby and JRuby.
383
+
384
+ ### Shell
385
+
386
+ You can launch the interactive Wrest shell by running bin/wrest if you have the source or invoking `wrest` from your prompt if you've installed the gem.
387
+
388
+ ```
389
+ $ wrest
390
+ >> y 'http://twitter.com/statuses/public_timeline.json'.to_uri(timeout: 5).get.deserialise
391
+ ```
392
+
393
+ ### Testing
394
+
395
+ Start the Sinatra test server for functional test. The dependencies for the test app are managed separately by a Gemfile under spec/sample_app.
396
+
397
+ To start the sample application:
398
+
399
+ ```
400
+ cd spec/sample_app
401
+ bundle install
402
+ bundle exec rake # runs sample app on port 3000
403
+ ```
404
+
405
+ Start a memcached daemon/process on port 11211 and redis on 6379 (both default ports)
406
+
407
+ ```
408
+ brew install memcached
409
+ brew install redis
410
+ brew services start memcached
411
+ brew services start redis
412
+ ```
413
+
414
+ Run the tests in a different terminal:
415
+
416
+ ```
417
+ # Run the normal test suite.
418
+ bundle exec rake
419
+
420
+ # Runs the functional test suite.
421
+ bundle exec rake rspec:functional
422
+ ```
423
+
424
+ ## Contributors
425
+
426
+ * Sidu Ponnappa : [kaiwren](http://github.com/kaiwren)
427
+ * Niranjan Paranjape : [achamian](http://github.com/achamian)
428
+ * Aakash Dharmadhkari : [aakashd](http://github.com/aakashd)
429
+ * Srushti : [srushti](http://github.com/srushti)
430
+ * Preethi Ramdev : [preethiramdev](http://github.com/preethiramdev)
431
+ * Nikhil Vallishayee : [nikhilvallishayee](http://github.com/nikhilvallishayee)
432
+ * Jacques Crocker : [railsjedi](http://github.com/railsjedi)
433
+ * Jasim A Basheer: [jasim](http://github.com/jasim)
434
+ * Arvind Laxminarayan: [ardsrk](http://github.com/ardsrk)
435
+
436
+ ©️ Copyright 2009-2022 [Sidu Ponnappa](http://twitter.com/ponnappa). All Rights Reserved.
data/bin/wrest ADDED
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ load(File.join(__dir__, 'wrest_shell.rb'))
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ puts "Ruby #{RUBY_VERSION}, #{RUBY_RELEASE_DATE}, #{RUBY_PLATFORM}"
4
+
5
+ entry_point = File.join(__dir__, '..', 'lib', 'wrest.rb')
6
+ version_file = File.expand_path(File.join(__dir__, '..', 'lib', 'wrest', 'version.rb'))
7
+
8
+ irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
9
+
10
+ require 'optparse'
11
+ options = { irb: irb }
12
+ OptionParser.new do |opt|
13
+ opt.banner = 'Usage: console [options]'
14
+ opt.on("--irb=[#{irb}]", 'Invoke a different irb.') { |v| options[:irb] = v }
15
+ opt.parse!(ARGV)
16
+ end
17
+
18
+ libs = ' -r irb/completion ' \
19
+ "-r #{entry_point}"
20
+
21
+ require version_file
22
+ puts "Loading Wrest #{Wrest::VERSION}"
23
+ exec "#{options[:irb]} #{libs} --simple-prompt"
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright 2009 Sidu Ponnappa
4
+
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at native://www.apache.org/licenses/LICENSE-2.0
8
+ # Unless required by applicable law or agreed to in writing, software distributed under the License
9
+ # is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10
+ # See the License for the specific language governing permissions and limitations under the License.
11
+
12
+ begin
13
+ gem 'eventmachine'
14
+ rescue Gem::LoadError => e
15
+ Wrest.logger.debug 'Eventmachine ~> 0.12.10 not found. Wrest uses Eventmachine to perform evented asynchronous requests'
16
+ raise e
17
+ end
18
+
19
+ require 'eventmachine'
20
+
21
+ module Wrest
22
+ module AsyncRequest
23
+ class EventMachineBackend
24
+ def execute(request)
25
+ EventMachine.run do
26
+ request.invoke
27
+ EventMachine.stop
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end