rest-more 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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,2 @@
1
+ <%= rc_facebook.app_id %>
2
+ <%= rc_flurry .api_key %>
@@ -0,0 +1,4 @@
1
+ # This file is used by Rack-based servers to start the application.
2
+
3
+ require ::File.expand_path('../config/environment', __FILE__)
4
+ run Rails3::Application
@@ -0,0 +1,23 @@
1
+
2
+ # Set up gems listed in the Gemfile.
3
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
4
+
5
+ require 'bundler/setup' if File.exists?(ENV['BUNDLE_GEMFILE'])
6
+
7
+ require 'action_controller/railtie'
8
+ require 'rails/test_unit/railtie'
9
+
10
+ Bundler.require(:default, Rails.env)
11
+
12
+ module Rails3
13
+ class Application < Rails::Application
14
+ config.encoding = 'utf-8'
15
+
16
+ logger = Logger.new($stdout)
17
+ logger.level = Logger::INFO
18
+ config.logger = logger
19
+
20
+ # Configure sensitive parameters which will be filtered from the log file.
21
+ config.filter_parameters += [:password]
22
+ end
23
+ end
@@ -0,0 +1,6 @@
1
+ require 'rubygems'
2
+
3
+ # Set up gems listed in the Gemfile.
4
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
5
+
6
+ require 'bundler/setup' if File.exists?(ENV['BUNDLE_GEMFILE'])
@@ -0,0 +1,5 @@
1
+ # Load the rails application
2
+ require File.expand_path('../application', __FILE__)
3
+
4
+ # Initialize the rails application
5
+ Rails3::Application.initialize!
@@ -0,0 +1,23 @@
1
+ Rails3::Application.configure do
2
+ # Settings specified here will take precedence over those in config/application.rb
3
+
4
+ # In the development environment your application's code is reloaded on
5
+ # every request. This slows down response time but is perfect for development
6
+ # since you don't have to restart the webserver when you make code changes.
7
+ config.cache_classes = false
8
+
9
+ # Log error messages when you accidentally call methods on nil.
10
+ config.whiny_nils = true
11
+
12
+ # Show full error reports and disable caching
13
+ config.consider_all_requests_local = true
14
+ config.action_view.debug_rjs = true
15
+ config.action_controller.perform_caching = false
16
+
17
+ # Print deprecation notices to the Rails logger
18
+ config.active_support.deprecation = :log
19
+
20
+ # Only use best-standards-support built into browsers
21
+ config.action_dispatch.best_standards_support = :builtin
22
+ end
23
+
@@ -0,0 +1,49 @@
1
+ Rails3::Application.configure do
2
+ # Settings specified here will take precedence over those in config/application.rb
3
+
4
+ # The production environment is meant for finished, "live" apps.
5
+ # Code is not reloaded between requests
6
+ config.cache_classes = true
7
+
8
+ # Full error reports are disabled and caching is turned on
9
+ config.consider_all_requests_local = false
10
+ config.action_controller.perform_caching = true
11
+
12
+ # Specifies the header that your server uses for sending files
13
+ config.action_dispatch.x_sendfile_header = "X-Sendfile"
14
+
15
+ # For nginx:
16
+ # config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect'
17
+
18
+ # If you have no front-end server that supports something like X-Sendfile,
19
+ # just comment this out and Rails will serve the files
20
+
21
+ # See everything in the log (default is :info)
22
+ # config.log_level = :debug
23
+
24
+ # Use a different logger for distributed setups
25
+ # config.logger = SyslogLogger.new
26
+
27
+ # Use a different cache store in production
28
+ # config.cache_store = :mem_cache_store
29
+
30
+ # Disable Rails's static asset server
31
+ # In production, Apache or nginx will already do this
32
+ config.serve_static_assets = false
33
+
34
+ # Enable serving of images, stylesheets, and javascripts from an asset server
35
+ # config.action_controller.asset_host = "http://assets.example.com"
36
+
37
+ # Disable delivery errors, bad email addresses will be ignored
38
+ # config.action_mailer.raise_delivery_errors = false
39
+
40
+ # Enable threaded mode
41
+ # config.threadsafe!
42
+
43
+ # Enable locale fallbacks for I18n (makes lookups for any locale fall back to
44
+ # the I18n.default_locale when a translation can not be found)
45
+ config.i18n.fallbacks = true
46
+
47
+ # Send deprecation notices to registered listeners
48
+ config.active_support.deprecation = :notify
49
+ end
@@ -0,0 +1,30 @@
1
+ Rails3::Application.configure do
2
+ # Settings specified here will take precedence over those in config/application.rb
3
+
4
+ # The test environment is used exclusively to run your application's
5
+ # test suite. You never need to work with it otherwise. Remember that
6
+ # your test database is "scratch space" for the test suite and is wiped
7
+ # and recreated between test runs. Don't rely on the data there!
8
+ config.cache_classes = true
9
+
10
+ # Log error messages when you accidentally call methods on nil.
11
+ config.whiny_nils = true
12
+
13
+ # Show full error reports and disable caching
14
+ config.consider_all_requests_local = true
15
+ config.action_controller.perform_caching = false
16
+
17
+ # Raise exceptions instead of rendering exception templates
18
+ config.action_dispatch.show_exceptions = false
19
+
20
+ # Disable request forgery protection in test environment
21
+ config.action_controller.allow_forgery_protection = false
22
+
23
+ # Use SQL instead of Active Record's schema dumper when creating the test database.
24
+ # This is necessary if your schema can't be completely dumped by the schema dumper,
25
+ # like if you have constraints or database-specific column types
26
+ # config.active_record.schema_format = :sql
27
+
28
+ # Print deprecation notices to the stderr
29
+ config.active_support.deprecation = :stderr
30
+ end
@@ -0,0 +1,7 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ # Your secret key for verifying the integrity of signed cookies.
4
+ # If you change this key, all old signed cookies will become invalid!
5
+ # Make sure the secret is at least 30 characters and all random,
6
+ # no regular words or you'll be exposed to dictionary attacks.
7
+ Rails3::Application.config.secret_token = '74c293b4c5df1981d2f92785aa5538a21b0901293d693e6bb828669cbb1f1d1f5ddd1b3e21325304c90e952a4866a9eec7620379b6f6c27aae0670cdefda97ae'
@@ -0,0 +1,8 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ Rails3::Application.config.session_store :cookie_store, :key => '_rails3_session'
4
+
5
+ # Use the database for sessions instead of the cookie-based default,
6
+ # which shouldn't be used to store highly confidential information
7
+ # (create the session table with "rails generate session_migration")
8
+ # Rails3::Application.config.session_store :active_record_store
@@ -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,5 @@
1
+
2
+ Rails3::Application.routes.draw do
3
+ root :controller => 'application', :action => 'index'
4
+ match ':action', :controller => 'application'
5
+ end
@@ -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