rest-more 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (81) hide show
  1. data/.gitignore +6 -0
  2. data/.gitmodules +3 -0
  3. data/.travis.yml +13 -0
  4. data/CHANGES.md +7 -0
  5. data/Gemfile +26 -0
  6. data/LICENSE +201 -0
  7. data/README.md +72 -0
  8. data/Rakefile +63 -0
  9. data/TODO.md +14 -0
  10. data/example/rails2/Gemfile +21 -0
  11. data/example/rails2/README +4 -0
  12. data/example/rails2/Rakefile +11 -0
  13. data/example/rails2/app/controllers/application_controller.rb +134 -0
  14. data/example/rails2/app/views/application/helper.html.erb +2 -0
  15. data/example/rails2/config/boot.rb +130 -0
  16. data/example/rails2/config/environment.rb +15 -0
  17. data/example/rails2/config/environments/development.rb +17 -0
  18. data/example/rails2/config/environments/production.rb +28 -0
  19. data/example/rails2/config/environments/test.rb +30 -0
  20. data/example/rails2/config/initializers/cookie_verification_secret.rb +7 -0
  21. data/example/rails2/config/initializers/new_rails_defaults.rb +21 -0
  22. data/example/rails2/config/initializers/session_store.rb +15 -0
  23. data/example/rails2/config/preinitializer.rb +23 -0
  24. data/example/rails2/config/rest-core.yaml +16 -0
  25. data/example/rails2/config/routes.rb +43 -0
  26. data/example/rails2/log +0 -0
  27. data/example/rails2/test/functional/application_controller_test.rb +219 -0
  28. data/example/rails2/test/test_helper.rb +18 -0
  29. data/example/rails2/test/unit/rails_util_test.rb +44 -0
  30. data/example/rails3/Gemfile +20 -0
  31. data/example/rails3/README +4 -0
  32. data/example/rails3/Rakefile +7 -0
  33. data/example/rails3/app/controllers/application_controller.rb +134 -0
  34. data/example/rails3/app/views/application/helper.html.erb +2 -0
  35. data/example/rails3/config.ru +4 -0
  36. data/example/rails3/config/application.rb +23 -0
  37. data/example/rails3/config/boot.rb +6 -0
  38. data/example/rails3/config/environment.rb +5 -0
  39. data/example/rails3/config/environments/development.rb +23 -0
  40. data/example/rails3/config/environments/production.rb +49 -0
  41. data/example/rails3/config/environments/test.rb +30 -0
  42. data/example/rails3/config/initializers/secret_token.rb +7 -0
  43. data/example/rails3/config/initializers/session_store.rb +8 -0
  44. data/example/rails3/config/rest-core.yaml +16 -0
  45. data/example/rails3/config/routes.rb +5 -0
  46. data/example/rails3/test/functional/application_controller_test.rb +219 -0
  47. data/example/rails3/test/test_helper.rb +18 -0
  48. data/example/rails3/test/unit/rails_util_test.rb +44 -0
  49. data/example/sinatra/config.ru +16 -0
  50. data/lib/rest-core/client/facebook.rb +265 -0
  51. data/lib/rest-core/client/facebook/rails_util.rb +334 -0
  52. data/lib/rest-core/client/flurry.rb +107 -0
  53. data/lib/rest-core/client/flurry/rails_util.rb +74 -0
  54. data/lib/rest-core/client/github.rb +18 -0
  55. data/lib/rest-core/client/linkedin.rb +59 -0
  56. data/lib/rest-core/client/mixi.rb +47 -0
  57. data/lib/rest-core/client/simple.rb +2 -0
  58. data/lib/rest-core/client/twitter.rb +101 -0
  59. data/lib/rest-core/client/universal.rb +18 -0
  60. data/lib/rest-more.rb +11 -0
  61. data/lib/rest-more/version.rb +4 -0
  62. data/rest-more.gemspec +127 -0
  63. data/task/.gitignore +1 -0
  64. data/task/gemgem.rb +265 -0
  65. data/test/client/facebook/config/rest-core.yaml +8 -0
  66. data/test/client/facebook/test_api.rb +97 -0
  67. data/test/client/facebook/test_cache.rb +58 -0
  68. data/test/client/facebook/test_default.rb +23 -0
  69. data/test/client/facebook/test_error.rb +65 -0
  70. data/test/client/facebook/test_handler.rb +84 -0
  71. data/test/client/facebook/test_load_config.rb +39 -0
  72. data/test/client/facebook/test_misc.rb +72 -0
  73. data/test/client/facebook/test_oauth.rb +38 -0
  74. data/test/client/facebook/test_old.rb +114 -0
  75. data/test/client/facebook/test_page.rb +106 -0
  76. data/test/client/facebook/test_parse.rb +166 -0
  77. data/test/client/facebook/test_serialize.rb +43 -0
  78. data/test/client/facebook/test_timeout.rb +22 -0
  79. data/test/client/flurry/test_metrics.rb +83 -0
  80. data/test/client/twitter/test_api.rb +37 -0
  81. metadata +155 -0
@@ -0,0 +1,16 @@
1
+
2
+ development: &default
3
+ facebook:
4
+ app_id: '123'
5
+ secret: '456'
6
+ canvas: 'can'
7
+
8
+ flurry:
9
+ api_key: 'key'
10
+ access_code: 'code'
11
+
12
+ production:
13
+ *default
14
+
15
+ test:
16
+ *default
@@ -0,0 +1,43 @@
1
+ ActionController::Routing::Routes.draw do |map|
2
+ # The priority is based upon order of creation: first created -> highest priority.
3
+
4
+ # Sample of regular route:
5
+ # map.connect 'products/:id', :controller => 'catalog', :action => 'view'
6
+ # Keep in mind you can assign values other than :controller and :action
7
+
8
+ # Sample of named route:
9
+ # map.purchase 'products/:id/purchase', :controller => 'catalog', :action => 'purchase'
10
+ # This route can be invoked with purchase_url(:id => product.id)
11
+
12
+ # Sample resource route (maps HTTP verbs to controller actions automatically):
13
+ # map.resources :products
14
+
15
+ # Sample resource route with options:
16
+ # map.resources :products, :member => { :short => :get, :toggle => :post }, :collection => { :sold => :get }
17
+
18
+ # Sample resource route with sub-resources:
19
+ # map.resources :products, :has_many => [ :comments, :sales ], :has_one => :seller
20
+
21
+ # Sample resource route with more complex sub-resources
22
+ # map.resources :products do |products|
23
+ # products.resources :comments
24
+ # products.resources :sales, :collection => { :recent => :get }
25
+ # end
26
+
27
+ # Sample resource route within a namespace:
28
+ # map.namespace :admin do |admin|
29
+ # # Directs /admin/products/* to Admin::ProductsController (app/controllers/admin/products_controller.rb)
30
+ # admin.resources :products
31
+ # end
32
+
33
+ # You can have the root of your site routed with map.root -- just remember to delete public/index.html.
34
+ map.connect ':action', :controller => 'application'
35
+
36
+ # See how all your routes lay out with "rake routes"
37
+
38
+ # Install the default routes as the lowest priority.
39
+ # Note: These default routes make all actions in every controller accessible via GET requests. You should
40
+ # consider removing or commenting them out if you're using named routes and resources.
41
+ # map.connect ':controller/:action/:id'
42
+ # map.connect ':controller/:action/:id.:format'
43
+ end
File without changes
@@ -0,0 +1,219 @@
1
+
2
+ require 'test_helper'
3
+ require 'webmock'
4
+ require 'rr'
5
+
6
+ WebMock.disable_net_connect!
7
+
8
+ class ApplicationControllerTest < ActionController::TestCase
9
+ include WebMock::API
10
+ include RR::Adapters::TestUnit
11
+
12
+ def setup
13
+ body = rand(2) == 0 ? '{"error":{"type":"OAuthException"}}' :
14
+ '{"error_code":104}'
15
+
16
+ stub_request(:get, 'https://graph.facebook.com/me').
17
+ to_return(:body => body)
18
+ end
19
+
20
+ def teardown
21
+ RR.verify
22
+ WebMock.reset!
23
+ end
24
+
25
+ def assert_url expected
26
+ assert_equal(expected, normalize_url(assigns(:rc_facebook_authorize_url)))
27
+ if @response.status == 200 # js redirect
28
+ assert_equal(
29
+ expected,
30
+ normalize_url(
31
+ @response.body.match(/window\.top\.location\.href = '(.+?)'/)[1]))
32
+
33
+ assert_equal(
34
+ CGI.escapeHTML(expected),
35
+ normalize_url(
36
+ @response.body.match(/content="0;url=(.+?)"/)[1], '&amp;'))
37
+
38
+ assert_equal(
39
+ CGI.escapeHTML(expected),
40
+ normalize_url(
41
+ @response.body.match(/<a href="(.+?)" target="_top">/)[1], '&amp;'))
42
+ end
43
+ end
44
+
45
+ def test_index
46
+ get(:index)
47
+ assert_response :redirect
48
+
49
+ url = normalize_url(
50
+ 'https://graph.facebook.com/oauth/authorize?client_id=123&' \
51
+ 'scope=&redirect_uri=http%3A%2F%2Ftest.host%2F')
52
+
53
+ assert_url(url)
54
+ end
55
+
56
+ def test_canvas
57
+ get(:canvas)
58
+ assert_response :success
59
+
60
+ url = normalize_url(
61
+ 'https://graph.facebook.com/oauth/authorize?client_id=123&' \
62
+ 'scope=publish_stream&' \
63
+ 'redirect_uri=http%3A%2F%2Fapps.facebook.com%2Fcan%2Fcanvas')
64
+
65
+ assert_url(url)
66
+ end
67
+
68
+ def test_diff_canvas
69
+ get(:diff_canvas)
70
+ assert_response :success
71
+
72
+ url = normalize_url(
73
+ 'https://graph.facebook.com/oauth/authorize?client_id=123&' \
74
+ 'scope=email&' \
75
+ 'redirect_uri=http%3A%2F%2Fapps.facebook.com%2FToT%2Fdiff_canvas')
76
+
77
+ assert_url(url)
78
+ end
79
+
80
+ def test_iframe_canvas
81
+ get(:iframe_canvas)
82
+ assert_response :success
83
+
84
+ url = normalize_url(
85
+ 'https://graph.facebook.com/oauth/authorize?client_id=123&' \
86
+ 'scope=&' \
87
+ 'redirect_uri=http%3A%2F%2Fapps.facebook.com%2Fzzz%2Fiframe_canvas')
88
+
89
+ assert_url(url)
90
+ end
91
+
92
+ def test_options
93
+ get(:options)
94
+ assert_response :redirect
95
+
96
+ url = normalize_url(
97
+ 'https://graph.facebook.com/oauth/authorize?client_id=123&' \
98
+ 'scope=bogus&' \
99
+ 'redirect_uri=http%3A%2F%2Ftest.host%2Foptions')
100
+
101
+ assert_url(url)
102
+ end
103
+
104
+ def test_protected
105
+ assert_nil @controller.public_methods.find{ |m| m.to_s =~ /^rc_/ }
106
+ end
107
+
108
+ def test_no_auto
109
+ get(:no_auto)
110
+ assert_response :success
111
+ assert_equal 'XD', @response.body
112
+ end
113
+
114
+ def test_app_id
115
+ get(:diff_app_id)
116
+ assert_response :success
117
+ assert_equal 'zzz', @response.body
118
+ end
119
+
120
+ def test_cache
121
+ WebMock.reset!
122
+ stub_request(:get, 'https://graph.facebook.com/cache').
123
+ to_return(:body => '{"message":"ok"}')
124
+
125
+ get(:cache)
126
+ assert_response :success
127
+ assert_equal '{"message":"ok"}', @response.body
128
+ end
129
+
130
+ def test_handler
131
+ WebMock.reset!
132
+ stub_request(:get, 'https://graph.facebook.com/me?access_token=aloha').
133
+ to_return(:body => '["snowman"]')
134
+
135
+ Rails.cache[:fbs] = RestCore::Facebook.new(:access_token => 'aloha').fbs
136
+ get(:handler_)
137
+ assert_response :success
138
+ assert_equal '["snowman"]', @response.body
139
+ ensure
140
+ Rails.cache.clear
141
+ end
142
+
143
+ def test_session
144
+ WebMock.reset!
145
+ stub_request(:get, 'https://graph.facebook.com/me?access_token=wozilla').
146
+ to_return(:body => '["fireball"]')
147
+
148
+ @request.session[RestCore::Facebook::RailsUtil.rc_facebook_storage_key] =
149
+ RestCore::Facebook.new(:access_token => 'wozilla').fbs
150
+
151
+ get(:session_)
152
+ assert_response :success
153
+ assert_equal '["fireball"]', @response.body
154
+ end
155
+
156
+ def test_cookies
157
+ WebMock.reset!
158
+ stub_request(:get, 'https://graph.facebook.com/me?access_token=blizzard').
159
+ to_return(:body => '["yeti"]')
160
+
161
+ @request.cookies[RestCore::Facebook::RailsUtil.rc_facebook_storage_key] =
162
+ RestCore::Facebook.new(:access_token => 'blizzard').fbs
163
+
164
+ get(:cookies_)
165
+ assert_response :success
166
+ assert_equal '["yeti"]', @response.body
167
+ end
168
+
169
+ def test_error
170
+ get(:error)
171
+ rescue => e
172
+ assert_equal RestCore::Facebook::Error, e.class
173
+ end
174
+
175
+ def test_reinitailize
176
+ get(:reinitialize)
177
+ assert_response :success
178
+ assert_equal({'a' => 'b'}, YAML.load(@response.body))
179
+ end
180
+
181
+ def test_helper
182
+ get(:helper)
183
+ assert_response :success
184
+ assert_equal "#{RestCore::Facebook.default_app_id}\n" \
185
+ "#{RestCore::Flurry .default_api_key}",
186
+ @response.body.strip
187
+ end
188
+
189
+ def test_defaults
190
+ get(:defaults)
191
+ assert_response :success
192
+ assert_equal 'true', @response.body.strip
193
+ end
194
+
195
+ def setup_cookies key
196
+ cookies = {"#{key}_#{RC::Facebook.default_app_id}" => 'dummy'}
197
+ stub(@controller).cookies{cookies}
198
+ f = RC::Facebook.new
199
+ stub(@controller).rc_facebook{f}
200
+ mock(f).parse_cookies!(cookies)
201
+ end
202
+
203
+ def test_parse_cookies_fbs
204
+ setup_cookies('fbs')
205
+ get(:parse_cookies)
206
+ end
207
+
208
+ def test_parse_cookies_fbsr
209
+ setup_cookies('fbsr')
210
+ get(:parse_cookies)
211
+ end
212
+
213
+ # regression test
214
+ def test_no_ns_pollution
215
+ get(:no_ns_pollution)
216
+ assert_response :success
217
+ assert_equal 'Timeout::Error', @response.body.strip
218
+ end
219
+ end
@@ -0,0 +1,18 @@
1
+
2
+ ENV["RAILS_ENV"] = "test"
3
+ require File.expand_path('../../config/environment', __FILE__)
4
+ begin
5
+ require 'rails/test_help'
6
+ rescue LoadError # for rails2
7
+ require 'test_help'
8
+ end
9
+
10
+ class ActiveSupport::TestCase
11
+ def normalize_query query, amp='&'
12
+ '?' + query[1..-1].split(amp).sort.join(amp)
13
+ end
14
+
15
+ def normalize_url url, amp='&'
16
+ url.sub(/\?.+/){ |query| normalize_query(query, amp) }
17
+ end
18
+ end
@@ -0,0 +1,44 @@
1
+
2
+ require 'test_helper'
3
+ require 'rr'
4
+
5
+ class RailsUtilTest < ActiveSupport::TestCase
6
+ include RR::Adapters::TestUnit
7
+
8
+ def setup_mock url
9
+ mock(RestCore::Facebook::RailsUtil).rc_facebook_in_canvas?{ false }
10
+ mock(RestCore::Facebook::RailsUtil).request{
11
+ mock(Object.new).url{ url }
12
+ }
13
+ end
14
+
15
+ def test_rest_graph_normalized_request_uri_0
16
+ setup_mock( 'http://test.com/?code=123&lang=en')
17
+ assert_equal('http://test.com/?lang=en',
18
+ RestCore::Facebook::RailsUtil.rc_facebook_normalized_request_uri)
19
+ end
20
+
21
+ def test_rest_graph_normalized_request_uri_1
22
+ setup_mock( 'http://test.com/?lang=en&code=123')
23
+ assert_equal('http://test.com/?lang=en',
24
+ RestCore::Facebook::RailsUtil.rc_facebook_normalized_request_uri)
25
+ end
26
+
27
+ def test_rest_graph_normalized_request_uri_2
28
+ setup_mock( 'http://test.com/?session=abc&lang=en&code=123')
29
+ assert_equal('http://test.com/?lang=en',
30
+ RestCore::Facebook::RailsUtil.rc_facebook_normalized_request_uri)
31
+ end
32
+
33
+ def test_rest_graph_normalized_request_uri_3
34
+ setup_mock( 'http://test.com/?code=123')
35
+ assert_equal('http://test.com/',
36
+ RestCore::Facebook::RailsUtil.rc_facebook_normalized_request_uri)
37
+ end
38
+
39
+ def test_rest_graph_normalized_request_uri_4
40
+ setup_mock( 'http://test.com/?signed_request=abc&code=123')
41
+ assert_equal('http://test.com/',
42
+ RestCore::Facebook::RailsUtil.rc_facebook_normalized_request_uri)
43
+ end
44
+ end
@@ -0,0 +1,20 @@
1
+
2
+ source 'http://rubygems.org'
3
+
4
+ gem 'rails', '3.0.9'
5
+
6
+ gem 'rest-client' # for rest-core
7
+ gem 'rest-more', :path => '../../'
8
+
9
+ group 'test' do
10
+ gem 'rr'
11
+ gem 'webmock'
12
+ end
13
+
14
+ platforms(:ruby) do
15
+ gem 'yajl-ruby'
16
+ end
17
+
18
+ platforms(:jruby) do
19
+ gem 'jruby-openssl'
20
+ end
@@ -0,0 +1,4 @@
1
+
2
+ Please fill config/rest-core.yaml with your app_id, secret, etc., to
3
+ see if this example is working or not. The default setup is designed for
4
+ standalone site with Facebook JavaScript SDK.
@@ -0,0 +1,7 @@
1
+ # Add your own tasks in files placed in lib/tasks ending in .rake,
2
+ # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
3
+
4
+ require File.expand_path('../config/application', __FILE__)
5
+ require 'rake'
6
+
7
+ Rails3::Application.load_tasks
@@ -0,0 +1,134 @@
1
+
2
+ class ApplicationController < ActionController::Base
3
+ protect_from_forgery
4
+
5
+ include RestCore::Facebook::RailsUtil
6
+ include RestCore::Flurry::RailsUtil
7
+
8
+ before_filter :filter_common , :only => [:index]
9
+ before_filter :filter_canvas , :only => [:canvas]
10
+ before_filter :filter_options , :only => [:options]
11
+ before_filter :filter_no_auto , :only => [:no_auto]
12
+ before_filter :filter_diff_app_id , :only => [:diff_app_id]
13
+ before_filter :filter_diff_canvas , :only => [:diff_canvas]
14
+ before_filter :filter_iframe_canvas, :only => [:iframe_canvas]
15
+ before_filter :filter_cache , :only => [:cache]
16
+ before_filter :filter_hanlder , :only => [:handler_]
17
+ before_filter :filter_session , :only => [:session_]
18
+ before_filter :filter_cookies , :only => [:cookies_]
19
+
20
+ def index
21
+ render :text => rc_facebook.get('me').to_json
22
+ end
23
+ alias_method :canvas , :index
24
+ alias_method :options , :index
25
+ alias_method :diff_canvas , :index
26
+ alias_method :iframe_canvas, :index
27
+ alias_method :handler_ , :index
28
+ alias_method :session_ , :index
29
+ alias_method :cookies_ , :index
30
+
31
+ def no_auto
32
+ rc_facebook.get('me')
33
+ rescue RestCore::Facebook::Error
34
+ render :text => 'XD'
35
+ end
36
+
37
+ def diff_app_id
38
+ render :text => rc_facebook.app_id
39
+ end
40
+
41
+ def cache
42
+ url = rc_facebook.url('cache')
43
+ rc_facebook.get('cache')
44
+ rc_facebook.get('cache')
45
+ render :text => Rails.cache.read(Digest::MD5.hexdigest(url))
46
+ end
47
+
48
+ def error
49
+ raise RestCore::Facebook::Error.new("don't rescue me")
50
+ end
51
+
52
+ def reinitialize
53
+ rc_facebook_setup(:cache => {'a' => 'b'})
54
+ render :text => YAML.dump(rc_facebook.cache)
55
+ end
56
+
57
+ def helper; end
58
+
59
+ def defaults
60
+ rc_facebook_setup
61
+ render :text => (rc_facebook.cache == Rails.cache &&
62
+ rc_facebook.log_method.receiver == Rails.logger)
63
+ end
64
+
65
+ def parse_cookies
66
+ rc_facebook_setup
67
+ render :text => 'dummy'
68
+ end
69
+
70
+ # regression test
71
+ def no_ns_pollution
72
+ render :text => Timeout::Error.name
73
+ end
74
+
75
+ private
76
+ def filter_common
77
+ rc_facebook_setup(:auto_authorize => true, :canvas => '')
78
+ end
79
+
80
+ def filter_canvas
81
+ rc_facebook_setup(:canvas => RestCore::Facebook.
82
+ default_canvas,
83
+ :auto_authorize_scope => 'publish_stream')
84
+ end
85
+
86
+ def filter_diff_canvas
87
+ rc_facebook_setup(:canvas => 'ToT',
88
+ :auto_authorize_scope => 'email')
89
+ end
90
+
91
+ def filter_iframe_canvas
92
+ rc_facebook_setup(:canvas => 'zzz',
93
+ :auto_authorize => true)
94
+ end
95
+
96
+ def filter_no_auto
97
+ rc_facebook_setup(:auto_authorize => false)
98
+ end
99
+
100
+ def filter_diff_app_id
101
+ rc_facebook_setup(:app_id => 'zzz',
102
+ :auto_authorize => true)
103
+ end
104
+
105
+ def filter_options
106
+ rc_facebook_setup(:auto_authorize_options => {:scope => 'bogus'},
107
+ :canvas => nil)
108
+ end
109
+
110
+ def filter_cache
111
+ rc_facebook_setup(:cache => Rails.cache)
112
+ end
113
+
114
+ def filter_hanlder
115
+ rc_facebook_setup(:write_handler => method(:write_handler),
116
+ :check_handler => method(:check_handler))
117
+ end
118
+
119
+ def write_handler fbs
120
+ Rails.cache[:fbs] = fbs
121
+ end
122
+
123
+ def check_handler
124
+ Rails.cache[:fbs]
125
+ end
126
+
127
+ def filter_session
128
+ rc_facebook_setup(:write_session => true)
129
+ end
130
+
131
+ def filter_cookies
132
+ rc_facebook_setup(:write_cookies => true)
133
+ end
134
+ end