rest-more 1.0.2 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (72) hide show
  1. data/.gitignore +0 -5
  2. data/.travis.yml +4 -6
  3. data/CHANGES.md +16 -0
  4. data/Gemfile +6 -3
  5. data/README.md +43 -129
  6. data/Rakefile +2 -3
  7. data/example/multi.rb +12 -23
  8. data/example/rails3/Gemfile +2 -1
  9. data/example/rails3/app/controllers/application_controller.rb +9 -4
  10. data/example/rails3/test/functional/application_controller_test.rb +1 -1
  11. data/example/rainbows.rb +48 -7
  12. data/example/simple.rb +3 -3
  13. data/example/sinatra/config.ru +1 -1
  14. data/lib/rest-core/client/dropbox.rb +19 -27
  15. data/lib/rest-core/client/facebook.rb +28 -51
  16. data/lib/rest-core/client/facebook/rails_util.rb +13 -11
  17. data/lib/rest-core/client/github.rb +13 -21
  18. data/lib/rest-core/client/linkedin.rb +19 -27
  19. data/lib/rest-core/client/twitter.rb +13 -21
  20. data/lib/rest-more.rb +0 -2
  21. data/lib/rest-more/test.rb +11 -0
  22. data/lib/rest-more/version.rb +1 -1
  23. data/rest-more.gemspec +7 -41
  24. data/task/.gitignore +1 -0
  25. data/task/gemgem.rb +267 -0
  26. data/test/dropbox/test_api.rb +1 -1
  27. data/test/facebook/config/rest-core.yaml +1 -1
  28. data/test/facebook/test_api.rb +10 -14
  29. data/test/facebook/test_error.rb +2 -2
  30. data/test/facebook/test_handler.rb +6 -6
  31. data/test/facebook/test_load_config.rb +4 -4
  32. data/test/facebook/test_misc.rb +3 -3
  33. data/test/facebook/test_old.rb +2 -37
  34. data/test/facebook/test_page.rb +26 -24
  35. data/test/facebook/test_parse.rb +1 -1
  36. data/test/facebook/test_serialize.rb +3 -19
  37. data/test/facebook/test_timeout.rb +6 -2
  38. data/test/twitter/test_api.rb +1 -1
  39. metadata +6 -40
  40. data/doc/ToC.md +0 -9
  41. data/doc/dependency.md +0 -4
  42. data/doc/design.md +0 -4
  43. data/doc/rest-graph.md +0 -4
  44. data/example/async.rb +0 -89
  45. data/example/facebook.rb +0 -13
  46. data/example/rails2/Gemfile +0 -22
  47. data/example/rails2/README +0 -4
  48. data/example/rails2/Rakefile +0 -11
  49. data/example/rails2/app/controllers/application_controller.rb +0 -138
  50. data/example/rails2/app/views/application/helper.html.erb +0 -1
  51. data/example/rails2/config/boot.rb +0 -130
  52. data/example/rails2/config/environment.rb +0 -25
  53. data/example/rails2/config/environments/development.rb +0 -17
  54. data/example/rails2/config/environments/production.rb +0 -28
  55. data/example/rails2/config/environments/test.rb +0 -30
  56. data/example/rails2/config/initializers/cookie_verification_secret.rb +0 -7
  57. data/example/rails2/config/initializers/new_rails_defaults.rb +0 -21
  58. data/example/rails2/config/initializers/session_store.rb +0 -15
  59. data/example/rails2/config/preinitializer.rb +0 -23
  60. data/example/rails2/config/rest-core.yaml +0 -12
  61. data/example/rails2/config/routes.rb +0 -43
  62. data/example/rails2/log +0 -0
  63. data/example/rails2/test/functional/application_controller_test.rb +0 -259
  64. data/example/rails2/test/test_helper.rb +0 -18
  65. data/example/rails2/test/unit/rails_util_test.rb +0 -49
  66. data/lib/rest-core/client/bing.rb +0 -102
  67. data/lib/rest-core/client/bing/rails_util.rb +0 -13
  68. data/lib/rest-core/client/mixi.rb +0 -60
  69. data/lib/rest-core/client/mixi/rails_util.rb +0 -13
  70. data/test/bing/test_api.rb +0 -34
  71. data/test/facebook/test_cache.rb +0 -58
  72. data/test/mixi/test_api.rb +0 -20
data/.gitignore CHANGED
@@ -1,7 +1,2 @@
1
1
  pkg
2
- rdoc
3
2
  *.rbc
4
- .bundle
5
- .yardoc
6
- Gemfile.lock
7
- task
data/.travis.yml CHANGED
@@ -2,12 +2,10 @@ before_install: 'git submodule update --init'
2
2
  script: 'ruby -r bundler/setup -S rake test:travis'
3
3
 
4
4
  env:
5
- - 'RESTMORE=rest-more'
6
- - 'RESTMORE=rails2'
7
- - 'RESTMORE=rails3'
5
+ - 'RESTMORE=rest-more RBXOPT=-X19'
6
+ - 'RESTMORE=rails3 RBXOPT=-X19'
8
7
 
9
8
  rvm:
10
- - 1.9.2
11
9
  - 1.9.3
12
- - rbx-19mode
13
- - jruby-19mode
10
+ - rbx-head
11
+ - jruby-head
data/CHANGES.md CHANGED
@@ -1,5 +1,21 @@
1
1
  # CHANGES
2
2
 
3
+ ## rest-more 2.0.0 -- 2012-10-31
4
+
5
+ * Adopted to rest-core 2.0.0, a whole new generation.
6
+ * We no longer test against Ruby 1.9.2- and Rails 2-
7
+
8
+ ### Incompatible changes
9
+
10
+ * Bing (no longer useable) and Mixi (anyone?) clients are removed.
11
+ * Option for `:json_decode` is renamed to `:json_response` in rest-core.
12
+
13
+ ### Enhancement
14
+
15
+ * [Facebook::RailsUtil] Now we cache `@rc_facebook_normalized_request_uri`
16
+ to avoid paring broken URI from Facebook. Facebook is returning some
17
+ URI which are not properly escaped.
18
+
3
19
  ## rest-more 1.0.2 -- 2012-07-13
4
20
 
5
21
  * [Facebook::RailsUtil] Change the redirect log level from WARN to INFO.
data/Gemfile CHANGED
@@ -1,12 +1,14 @@
1
1
 
2
2
  source 'http://rubygems.org'
3
3
 
4
+ gemspec
5
+
4
6
  # this is for travis-ci
5
7
  gem 'rest-core', :path => 'rest-core' if
6
8
  File.exist?("#{File.dirname(File.expand_path(__FILE__))}/rest-core/Gemfile")
9
+
7
10
  gem 'rest-client'
8
11
  gem 'em-http-request'
9
- gem 'rest-more', :path => '.'
10
12
 
11
13
  gem 'rake'
12
14
  gem 'bacon'
@@ -15,17 +17,18 @@ gem 'webmock'
15
17
 
16
18
  gem 'json'
17
19
  gem 'json_pure'
20
+ gem 'multi_json'
18
21
 
19
22
  gem 'rack'
20
23
  gem 'ruby-hmac'
21
24
 
22
25
  platforms(:ruby) do
23
26
  gem 'yajl-ruby'
27
+ gem 'psych' if ENV['RESTMORE'] == 'rails3' # why?
24
28
  end
25
29
 
26
30
  platforms(:jruby) do
27
31
  gem 'jruby-openssl'
28
32
  end
29
33
 
30
- gem 'rails', '2.3.14' if ENV['RESTMORE'] == 'rails2'
31
- gem 'rails', '3.2.1' if ENV['RESTMORE'] == 'rails3'
34
+ gem 'rails', '3.2.8' if ENV['RESTMORE'] == 'rails3'
data/README.md CHANGED
@@ -6,8 +6,6 @@ Lin Jen-Shin ([godfat][]) had given a talk about rest-core on
6
6
  [RubyConf Taiwan 2011][talk]. The slide is in English, but the
7
7
  talk is in Mandarin.
8
8
 
9
- You can also read some other topics at [doc](https://github.com/cardinalblue/rest-core/blob/master/doc/ToC.md).
10
-
11
9
  [godfat]: https://github.com/godfat
12
10
  [talk]: http://rubyconf.tw/2011/#6
13
11
 
@@ -28,12 +26,10 @@ Various REST clients such as Facebook and Twitter built with [rest-core][]
28
26
 
29
27
  Out-of-box REST clients built with rest-core for:
30
28
 
31
- * Bing
32
29
  * Dropbox
33
30
  * Facebook
34
31
  * Github
35
32
  * Linkedin
36
- * Mixi
37
33
  * Twitter
38
34
 
39
35
  Rails utilities are also included.
@@ -42,30 +38,29 @@ Rails utilities are also included.
42
38
 
43
39
  ### Mandatory:
44
40
 
45
- * MRI (official CRuby) 1.8.7, 1.9.2, 1.9.3, Rubinius 1.8/1.9 and JRuby 1.8/1.9
41
+ * MRI (official CRuby) 1.9.3, Rubinius 1.9 and JRuby 1.9
42
+ * gem rest-core
46
43
  * gem rest-client
47
44
 
48
45
  ### Optional:
49
46
 
50
- * Fibers only work on Ruby 1.9+
51
47
  * gem [em-http-request][] (if using eventmachine)
52
- * gem [cool.io-http][] (if using cool.io)
53
- * gem json or yajl-ruby (if using `JsonDecode` middleware)
48
+ * gem json or yajl-ruby, or multi_json (if `JsonResponse` or
49
+ `JsonRequest` middlewares are used)
54
50
 
55
51
  [em-http-request]: https://github.com/igrigorik/em-http-request
56
- [cool.io-http]: https://github.com/godfat/cool.io-http
57
52
 
58
53
  ## INSTALLATION:
59
54
 
60
55
  ``` shell
61
- gem install rest-more
56
+ gem install rest-more
62
57
  ```
63
58
 
64
59
  Or if you want development version, put this in Gemfile:
65
60
 
66
61
  ``` ruby
67
- gem 'rest-more', :git => 'git://github.com/cardinalblue/rest-more.git',
68
- :submodules => true
62
+ gem 'rest-more', :git => 'git://github.com/cardinalblue/rest-more.git',
63
+ :submodules => true
69
64
  ```
70
65
 
71
66
  ## SYNOPSIS:
@@ -73,152 +68,71 @@ Or if you want development version, put this in Gemfile:
73
68
  The simplest usage:
74
69
 
75
70
  ``` ruby
76
- require 'rest-more'
71
+ require 'rest-more'
77
72
 
78
- RestCore::Twitter.new.statuses('_cardinalblue') # get user tweets
79
- RestCore::Github.new.get('users/cardinalblue') # get user info
73
+ RC::Twitter.new.statuses('_cardinalblue') # get user tweets
74
+ RC::Github.new.get('users/cardinalblue') # get user info
80
75
 
81
- linkedin = RestCore::Linkedin.new(:consumer_key => '...',
82
- :consumer_secret => '...')
83
- linkedin.authorize_url! # copy and paste the URL in browser to authorize
84
- linkedin.authorize!('..') # paste your code from browser
85
- linkedin.me # get current user info
76
+ linkedin = RC::Linkedin.new(:consumer_key => '...',
77
+ :consumer_secret => '...')
78
+ linkedin.authorize_url! # copy and paste the URL in browser to authorize
79
+ linkedin.authorize!('..') # paste your code from browser
80
+ linkedin.me # get current user info
86
81
 
87
- RestCore::Facebook.new.get('4') # get user info
82
+ RC::Facebook.new.get('4') # get user info
88
83
  ```
89
84
 
90
- Runnable example is here: [example/simple.rb][]. Please see [slides][] from
85
+ Runnable example is at: [example/simple.rb][]. Please see [slides][] from
91
86
  [rubyconf.tw/2011][rubyconf.tw] for concepts.
92
87
 
93
88
  [example/simple.rb]: https://github.com/cardinalblue/rest-more/blob/master/example/simple.rb
94
89
  [slides]: http://www.godfat.org/slide/2011-08-27-rest-core.html
95
90
  [rubyconf.tw]: http://rubyconf.tw/2011/#6
96
91
 
97
- ## Asynchronous HTTP Requests:
98
-
99
- I/O bound operations shouldn't be blocking the CPU! If you have a reactor,
100
- i.e. event loop, you should take the advantage of that to make HTTP requests
101
- not block the whole process/thread. For now, we support eventmachine and
102
- cool.io. By default, all clients are using `RestClient`, which is a
103
- synchronous HTTP client, thus blocking. It's very easy to use, but not
104
- very scalable (not concurrent-efficient). You can change the default app
105
- (i.e. HTTP clients) to an asynchronous one:
106
-
107
- ``` ruby
108
- require 'rest-more'
109
- RestCore::Builder.default_app = RestCore::EmHttpRequest
110
- ```
111
-
112
- or an auto-picking one, which would try to infer the correct HTTP client
113
- depending on the context. Either em-http-request, cool.io-http, or even
114
- rest-client if none of reactors is available.
115
-
116
- ``` ruby
117
- require 'rest-more'
118
- RestCore::Builder.default_app = RestCore::Auto
119
- ```
120
-
121
- You can also setup the `default_app` for a specific client, so that it won't
122
- change all the `default_app` for all the clients, like this:
92
+ ## Concurrent HTTP Requests:
123
93
 
124
- ``` ruby
125
- require 'rest-more'
126
- RestCore::Facebook.builder.default_app = RestCore::Auto
127
- ```
128
-
129
- If you're passing a block, the block is called after the response is
130
- available. That is the block is the callback for the request.
131
-
132
- ``` ruby
133
- require 'rest-more'
134
- require 'eventmachine'
135
- RestCore::Builder.default_app = RestCore::EmHttpRequest
136
- EM.run{
137
- RestCore::Facebook.new.get('4'){ |response|
138
- p response
139
- EM.stop
140
- }
141
- puts "It's not blocking..."
142
- }
143
- ```
144
-
145
- Otherwise, if you don't pass a block as the callback, EmHttpRequest (i.e.
146
- the HTTP client for eventmachine) would call `Fiber.yield` to yield to the
147
- original fiber, making asynchronous HTTP requests look like synchronous.
148
- If you don't understand what does this mean, you can take a look at
149
- [em-synchrony][]. It's basically the same idea.
94
+ Inherited from rest-core, you can do concurrent requests quite easily.
95
+ Here's a very quick example of getting Facebook users' names for UID 4 and 5:
150
96
 
151
97
  ``` ruby
152
- require 'rest-more'
153
- require 'eventmachine'
154
-
155
- RestCore::Builder.default_app = RestCore::EmHttpRequest
156
-
157
- EM.run{
158
- Fiber.new{
159
- p RestCore::Facebook.new.get('4')
160
- EM.stop
161
- }.resume
162
- puts "It's not blocking..."
163
- }
98
+ require 'rest-more'
99
+ facebook = RC::Facebook.new(:log_method => method(:puts))
100
+ puts "rest-client with threads doing concurrent requests"
101
+ a = [facebook.get('4'), facebook.get('5')]
102
+ puts "It's not blocking... but doing concurrent requests underneath"
103
+ p a.map{ |r| r['name'] } # here we want the values, so it blocks here
104
+ puts "DONE"
164
105
  ```
165
106
 
166
- [em-synchrony]: https://github.com/igrigorik/em-synchrony
167
-
168
- Runnable example is here: [example/facebook.rb][].
169
- You can also make multi-requests synchronously like this:
107
+ If you prefer callback based solution, this would also work:
170
108
 
171
109
  ``` ruby
172
- require 'rest-more'
173
- require 'eventmachine'
174
-
175
- RestCore::Builder.default_app = RestCore::Auto
176
- facebook = RestCore::Facebook.new(:log_method => method(:puts))
177
-
178
- EM.run{
179
- Fiber.new{
180
- fiber = Fiber.current
181
- result = {}
182
- facebook.get('4'){ |response|
183
- result[0] = response
184
- fiber.resume(result) if result.size == 2
185
- }
186
- puts "It's not blocking..."
187
- facebook.get('4'){ |response|
188
- result[1] = response
189
- fiber.resume(result) if result.size == 2
190
- }
191
- p Fiber.yield
192
- EM.stop
193
- }.resume
194
- puts "It's not blocking..."
195
- }
110
+ require 'rest-more'
111
+ facebook = RC::Facebook.new(:log_method => method(:puts))
112
+ puts "callback also works"
113
+ facebook.get('6'){ |r|
114
+ p r['name']
115
+ }
116
+ puts "It's not blocking... but doing concurrent requests underneath"
117
+ facebook.wait # we block here to wait for the request done
118
+ puts "DONE"
196
119
  ```
197
120
 
198
- Runnable example is here: [example/multi.rb][]. All `RestCore::EmHttpRequest`
199
- above is interchangeable with `RestCore::Auto` because `RestCore::Auto` would
200
- pick the right HTTP client when running inside the eventmachine's event loop.
121
+ Runnable example is at: [example/multi.rb][]. For a detailed demonstration,
122
+ see: [Advanced Concurrent HTTP Requests -- Embrace the Future][future]
201
123
 
202
- [example/facebook.rb]: https://github.com/cardinalblue/rest-more/blob/master/example/facebook.rb
203
124
  [example/multi.rb]: https://github.com/cardinalblue/rest-more/blob/master/example/multi.rb
204
-
205
- ## Supported HTTP clients:
206
-
207
- * `RestCore::RestClient` (gem rest-client)
208
- * `RestCore::EmHttpRequest` (gem em-http-request)
209
- * `RestCore::Coolio` (gem cool.io)
210
- * `RestCore::Auto` (which would pick one of the above depending on the
211
- context)
125
+ [future]: https://github.com/cardinalblue/rest-core#advanced-concurrent-http-requests----embrace-the-future
212
126
 
213
127
  ## Rails Utilities
214
128
 
215
- To be added. But you can take a look at the [Facebook tutorial][] first.
129
+ To be added. But you can take a look at [Facebook tutorial][] first.
216
130
 
217
131
  [Facebook tutorial]: https://github.com/cardinalblue/rest-more/blob/master/doc/tutorial/facebook.md
218
132
 
219
133
  ## EventMachine inside Rainbows!
220
134
 
221
- To be added. But you can take a look at the [Rainbows configuration][] first.
135
+ To be added. But you can take a look at [Rainbows configuration][] first.
222
136
 
223
137
  [Rainbows configuration]: https://github.com/cardinalblue/rest-more/blob/master/example/rainbows.rb
224
138
 
@@ -231,7 +145,7 @@ You need to install [rib][] in order to try this interactive shell:
231
145
  Then you can try this by running `rib rest-core`:
232
146
 
233
147
  rest-core>> self.site = 'https://api.github.com/users/'
234
- rest-core>> self.json_decode = true
148
+ rest-core>> self.json_response = true
235
149
  rest-core>> get 'cardinalblue'
236
150
 
237
151
  Which is using `RestCore::Universal` for accessing arbitrary websites.
data/Rakefile CHANGED
@@ -19,7 +19,7 @@ task 'gem:spec' do
19
19
  s.version = RestMore::VERSION
20
20
  s.homepage = 'https://github.com/cardinalblue/rest-more'
21
21
 
22
- %w[rest-core].each{ |g| s.add_runtime_dependency(g, '>=1.0.0') }
22
+ %w[rest-core].each{ |g| s.add_runtime_dependency(g, '>=2.0.0') }
23
23
 
24
24
  s.authors = ['Cardinal Blue', 'Lin Jen-Shin (godfat)']
25
25
  s.email = ['dev (XD) cardinalblue.com']
@@ -50,7 +50,7 @@ end
50
50
 
51
51
  desc 'Run example tests'
52
52
  task 'test:example' do
53
- Gemgem.test_rails('rails3', 'rails2')
53
+ Gemgem.test_rails('rails3')
54
54
  end
55
55
 
56
56
  desc 'Run all tests'
@@ -66,7 +66,6 @@ end
66
66
  task 'test:travis' do
67
67
  case ENV['RESTMORE']
68
68
  when 'rails3'; Gemgem.test_rails('rails3')
69
- when 'rails2'; Gemgem.test_rails('rails2')
70
69
  else ; Rake::Task['test'].invoke
71
70
  end
72
71
  end
data/example/multi.rb CHANGED
@@ -1,28 +1,17 @@
1
1
 
2
2
  require 'rest-more'
3
- require 'eventmachine'
4
- RestCore::EmHttpRequest # there might be a autoload bug?
5
- # omitting this line would cause
6
- # stack level too deep (SystemStackError)
7
3
 
8
- RestCore::Builder.default_app = RestCore::Auto
9
- facebook = RestCore::Facebook.new(:log_method => method(:puts))
4
+ facebook = RC::Facebook.new(:log_method => method(:puts))
5
+ puts "rest-client with threads doing concurrent requests"
6
+ a = [facebook.get('4'), facebook.get('5')]
7
+ puts "It's not blocking... but doing concurrent requests underneath"
8
+ p a.map{ |r| r['name'] } # here we want the values, so it blocks here
9
+ puts "DONE"
10
10
 
11
- EM.run{
12
- Fiber.new{
13
- fiber = Fiber.current
14
- result = {}
15
- facebook.get('4'){ |response|
16
- result[0] = response
17
- fiber.resume(result) if result.size == 2
18
- }
19
- puts "It's not blocking..."
20
- facebook.get('4'){ |response|
21
- result[1] = response
22
- fiber.resume(result) if result.size == 2
23
- }
24
- p Fiber.yield
25
- EM.stop
26
- }.resume
27
- puts "It's not blocking..."
11
+ puts "callback also works"
12
+ facebook.get('6'){ |r|
13
+ p r['name']
28
14
  }
15
+ puts "It's not blocking... but doing concurrent requests underneath"
16
+ facebook.wait # we block here to wait for the request done
17
+ puts "DONE"
@@ -1,7 +1,7 @@
1
1
 
2
2
  source 'http://rubygems.org'
3
3
 
4
- gem 'rails', '3.2.6'
4
+ gem 'rails', '3.2.8'
5
5
 
6
6
  gem 'rest-client' # for rest-core
7
7
  gem 'rest-core', :path => '../../rest-core'
@@ -14,6 +14,7 @@ end
14
14
 
15
15
  platforms(:ruby) do
16
16
  gem 'yajl-ruby'
17
+ gem 'psych' # why?
17
18
  end
18
19
 
19
20
  platforms(:jruby) do
@@ -28,7 +28,7 @@ class ApplicationController < ActionController::Base
28
28
  alias_method :cookies_ , :index
29
29
 
30
30
  def no_auto
31
- rc_facebook.get('me')
31
+ rc_facebook.get('me').tap{}
32
32
  rescue RestCore::Facebook::Error
33
33
  render :text => 'XD'
34
34
  end
@@ -39,9 +39,14 @@ class ApplicationController < ActionController::Base
39
39
 
40
40
  def cache
41
41
  url = rc_facebook.url('cache')
42
- rc_facebook.get('cache')
43
- rc_facebook.get('cache')
44
- render :text => Rails.cache.read(Digest::MD5.hexdigest(url))
42
+ rc_facebook.get('cache').tap{}
43
+ rc_facebook.get('cache').tap{}
44
+ key = RC::Cache.new(nil, nil, nil).cache_key(
45
+ rc_facebook.dry.call(
46
+ RC::REQUEST_METHOD => :get,
47
+ RC::REQUEST_PATH => rc_facebook.url('cache'),
48
+ &RC::Middleware.id))
49
+ render :text => Rails.cache.read(key)
45
50
  end
46
51
 
47
52
  def error