rest-graph 1.7.0 → 1.8.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (63) hide show
  1. data/.gitignore +6 -0
  2. data/CHANGES +44 -0
  3. data/CONTRIBUTORS +1 -0
  4. data/README +221 -191
  5. data/README.md +367 -0
  6. data/Rakefile +28 -43
  7. data/TODO +1 -0
  8. data/doc/ToC.md +10 -0
  9. data/doc/dependency.md +78 -0
  10. data/doc/design.md +206 -0
  11. data/doc/rails.md +12 -0
  12. data/doc/test.md +46 -0
  13. data/doc/tutorial.md +142 -0
  14. data/example/rails2/Gemfile +13 -0
  15. data/example/rails2/app/controllers/application_controller.rb +10 -8
  16. data/example/rails2/app/views/application/helper.html.erb +1 -0
  17. data/example/rails2/config/boot.rb +16 -0
  18. data/example/rails2/config/environment.rb +3 -30
  19. data/example/rails2/config/preinitializer.rb +23 -0
  20. data/example/rails2/test/functional/application_controller_test.rb +72 -32
  21. data/example/rails2/test/test_helper.rb +10 -6
  22. data/example/rails3/Gemfile +13 -0
  23. data/example/rails3/Rakefile +7 -0
  24. data/example/rails3/app/controllers/application_controller.rb +118 -0
  25. data/example/rails3/app/views/application/helper.html.erb +1 -0
  26. data/example/rails3/config.ru +4 -0
  27. data/example/rails3/config/application.rb +23 -0
  28. data/example/rails3/config/environment.rb +5 -0
  29. data/example/rails3/config/environments/development.rb +26 -0
  30. data/example/rails3/config/environments/production.rb +49 -0
  31. data/example/rails3/config/environments/test.rb +30 -0
  32. data/example/rails3/config/initializers/secret_token.rb +7 -0
  33. data/example/rails3/config/initializers/session_store.rb +8 -0
  34. data/example/rails3/config/rest-graph.yaml +11 -0
  35. data/example/rails3/config/routes.rb +5 -0
  36. data/example/rails3/test/functional/application_controller_test.rb +183 -0
  37. data/example/rails3/test/test_helper.rb +18 -0
  38. data/example/rails3/test/unit/rails_util_test.rb +44 -0
  39. data/init.rb +1 -1
  40. data/lib/rest-graph.rb +5 -571
  41. data/lib/rest-graph/auto_load.rb +3 -3
  42. data/lib/rest-graph/autoload.rb +3 -3
  43. data/lib/rest-graph/config_util.rb +43 -0
  44. data/lib/rest-graph/core.rb +608 -0
  45. data/lib/rest-graph/facebook_util.rb +74 -0
  46. data/lib/rest-graph/rails_util.rb +85 -37
  47. data/lib/rest-graph/test_util.rb +18 -2
  48. data/lib/rest-graph/version.rb +2 -2
  49. data/rest-graph.gemspec +42 -47
  50. data/task/gemgem.rb +155 -0
  51. data/test/test_api.rb +16 -0
  52. data/test/test_cache.rb +28 -8
  53. data/test/test_error.rb +9 -0
  54. data/test/test_facebook.rb +36 -0
  55. data/test/test_load_config.rb +16 -14
  56. data/test/test_misc.rb +4 -4
  57. data/test/test_parse.rb +10 -4
  58. metadata +146 -186
  59. data/Gemfile.lock +0 -45
  60. data/README.rdoc +0 -337
  61. data/example/rails2/script/console +0 -3
  62. data/example/rails2/script/server +0 -3
  63. data/lib/rest-graph/load_config.rb +0 -41
@@ -0,0 +1,13 @@
1
+
2
+ source 'http://rubygems.org'
3
+
4
+ gem 'rails', '2.3.11'
5
+
6
+ gem 'rest-client' # for rest-graph
7
+ gem 'yajl-ruby' # for rest-graph
8
+ gem 'rest-graph'
9
+
10
+ group 'test' do
11
+ gem 'rr'
12
+ gem 'webmock'
13
+ end
@@ -1,12 +1,6 @@
1
- # Filters added to this controller apply to all controllers in the application.
2
- # Likewise, all the methods added will be available for all controllers.
3
1
 
4
2
  class ApplicationController < ActionController::Base
5
- helper :all # include all helpers, all the time
6
- protect_from_forgery # See ActionController::RequestForgeryProtection for details
7
-
8
- # Scrub sensitive parameters from your log
9
- # filter_parameter_logging :password
3
+ protect_from_forgery
10
4
 
11
5
  include RestGraph::RailsUtil
12
6
 
@@ -54,6 +48,15 @@ class ApplicationController < ActionController::Base
54
48
  raise RestGraph::Error.new("don't rescue me")
55
49
  end
56
50
 
51
+ def reinitialize
52
+ cache_nil = rest_graph.cache
53
+ rest_graph_setup(:cache => {'a' => 'b'})
54
+ cache = rest_graph.cache
55
+ render :text => YAML.dump([cache_nil, cache])
56
+ end
57
+
58
+ def helper; end
59
+
57
60
  private
58
61
  def filter_common
59
62
  rest_graph_setup(:auto_authorize => true, :canvas => '')
@@ -71,7 +74,6 @@ class ApplicationController < ActionController::Base
71
74
 
72
75
  def filter_iframe_canvas
73
76
  rest_graph_setup(:canvas => 'zzz',
74
- :iframe => true,
75
77
  :auto_authorize => true)
76
78
  end
77
79
 
@@ -0,0 +1 @@
1
+ <%= rest_graph.app_id %>
@@ -110,5 +110,21 @@ module Rails
110
110
  end
111
111
  end
112
112
 
113
+ # begin copied from http://gembundler.com/rails23.html
114
+ class Rails::Boot
115
+ def run
116
+ load_initializer
117
+
118
+ Rails::Initializer.class_eval do
119
+ def load_gems
120
+ @bundler_loaded ||= Bundler.require(:default, Rails.env.to_sym)
121
+ end
122
+ end
123
+
124
+ Rails::Initializer.run(:set_load_path)
125
+ end
126
+ end
127
+ # end copied from http://gembundler.com/rails23.html
128
+
113
129
  # All that for this:
114
130
  Rails.boot!
@@ -1,42 +1,15 @@
1
1
  # Be sure to restart your server when you modify this file
2
2
 
3
3
  # Specifies gem version of Rails to use when vendor/rails is not present
4
- RAILS_GEM_VERSION = '2.3.10' unless defined? RAILS_GEM_VERSION
4
+ RAILS_GEM_VERSION = '2.3.11' unless defined? RAILS_GEM_VERSION
5
5
 
6
6
  # Bootstrap the Rails environment, frameworks, and default configuration
7
7
  require File.join(File.dirname(__FILE__), 'boot')
8
8
 
9
9
  Rails::Initializer.run do |config|
10
- # Settings in config/environments/* take precedence over those specified here.
11
- # Application configuration should go into files in config/initializers
12
- # -- all .rb files in that directory are automatically loaded.
10
+ # we use bundler now, so don't do this at this example
11
+ # config.gem 'rest-graph'
13
12
 
14
- # Add additional load paths for your own custom dirs
15
- # config.autoload_paths += %W( #{RAILS_ROOT}/extras )
16
-
17
- # Specify gems that this application depends on and have them installed with rake gems:install
18
- # config.gem "bj"
19
- # config.gem "hpricot", :version => '0.6', :source => "http://code.whytheluckystiff.net"
20
- # config.gem "sqlite3-ruby", :lib => "sqlite3"
21
- # config.gem "aws-s3", :lib => "aws/s3"
22
- config.gem 'rest-graph', :lib => 'rest-graph/autoload'
23
-
24
- # Only load the plugins named here, in the order given (default is alphabetical).
25
- # :all can be used as a placeholder for all plugins not explicitly named
26
- # config.plugins = [ :exception_notification, :ssl_requirement, :all ]
27
-
28
- # Skip frameworks you're not going to use. To use Rails without a database,
29
- # you must remove the Active Record framework.
30
13
  config.frameworks -= [ :active_record, :active_resource, :action_mailer ]
31
-
32
- # Activate observers that should always be running
33
- # config.active_record.observers = :cacher, :garbage_collector, :forum_observer
34
-
35
- # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
36
- # Run "rake -D time" for a list of tasks for finding time zone names.
37
14
  config.time_zone = 'UTC'
38
-
39
- # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
40
- # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}')]
41
- # config.i18n.default_locale = :de
42
15
  end
@@ -0,0 +1,23 @@
1
+
2
+ # copied from http://gembundler.com/rails23.html
3
+
4
+ begin
5
+ require "rubygems"
6
+ require "bundler"
7
+ rescue LoadError
8
+ raise "Could not load the bundler gem. Install it with `gem install bundler`."
9
+ end
10
+
11
+ if Gem::Version.new(Bundler::VERSION) <= Gem::Version.new("0.9.24")
12
+ raise RuntimeError, "Your bundler version is too old for Rails 2.3." +
13
+ "Run `gem install bundler` to upgrade."
14
+ end
15
+
16
+ begin
17
+ # Set up load paths for all bundled gems
18
+ ENV["BUNDLE_GEMFILE"] = File.expand_path("../../Gemfile", __FILE__)
19
+ Bundler.setup
20
+ rescue Bundler::GemNotFound
21
+ raise RuntimeError, "Bundler couldn't find some gems." +
22
+ "Did you run `bundle install`?"
23
+ end
@@ -19,59 +19,87 @@ class ApplicationControllerTest < ActionController::TestCase
19
19
  WebMock.reset!
20
20
  end
21
21
 
22
+ def assert_url expected
23
+ assert_equal(expected, normalize_url(assigns(:rest_graph_authorize_url)))
24
+ if @response.status == 200 # js redirect
25
+ assert_equal(
26
+ expected,
27
+ normalize_url(
28
+ @response.body.match(/window\.top\.location\.href = '(.+?)'/)[1]))
29
+
30
+ assert_equal(
31
+ CGI.escapeHTML(expected),
32
+ normalize_url(
33
+ @response.body.match(/content="0;url=(.+?)"/)[1], '&amp;'))
34
+
35
+ assert_equal(
36
+ CGI.escapeHTML(expected),
37
+ normalize_url(
38
+ @response.body.match(/<a href="(.+?)" target="_top">/)[1], '&amp;'))
39
+ end
40
+ end
41
+
22
42
  def test_index
23
43
  get(:index)
24
44
  assert_response :redirect
25
- assert_equal(
26
- normalize_url(
27
- 'https://graph.facebook.com/oauth/authorize?client_id=123&' \
28
- 'scope=&' \
29
- 'redirect_uri=http%3A%2F%2Ftest.host%2F'),
30
- normalize_url(assigns(:rest_graph_authorize_url)))
45
+
46
+ url = normalize_url(
47
+ 'https://graph.facebook.com/oauth/authorize?client_id=123&' \
48
+ 'scope=&redirect_uri=http%3A%2F%2Ftest.host%2F')
49
+
50
+ assert_url(url)
31
51
  end
32
52
 
33
53
  def test_canvas
34
54
  get(:canvas)
35
- assert_response :redirect
36
- assert_equal(
37
- normalize_url(
38
- 'https://graph.facebook.com/oauth/authorize?client_id=123&' \
39
- 'scope=publish_stream&' \
40
- 'redirect_uri=http%3A%2F%2Fapps.facebook.com%2Fcan%2Fcanvas'),
41
- normalize_url(assigns(:rest_graph_authorize_url)))
55
+ assert_response :success
56
+
57
+ url = normalize_url(
58
+ 'https://graph.facebook.com/oauth/authorize?client_id=123&' \
59
+ 'scope=publish_stream&' \
60
+ 'redirect_uri=http%3A%2F%2Fapps.facebook.com%2Fcan%2Fcanvas')
61
+
62
+ assert_url(url)
42
63
  end
43
64
 
44
65
  def test_diff_canvas
45
66
  get(:diff_canvas)
46
- assert_response :redirect
47
- assert_equal(
48
- normalize_url(
49
- 'https://graph.facebook.com/oauth/authorize?client_id=123&' \
50
- 'scope=email&' \
51
- 'redirect_uri=http%3A%2F%2Fapps.facebook.com%2FToT%2Fdiff_canvas'),
52
- normalize_url(assigns(:rest_graph_authorize_url)))
67
+ assert_response :success
68
+
69
+ url = normalize_url(
70
+ 'https://graph.facebook.com/oauth/authorize?client_id=123&' \
71
+ 'scope=email&' \
72
+ 'redirect_uri=http%3A%2F%2Fapps.facebook.com%2FToT%2Fdiff_canvas')
73
+
74
+ assert_url(url)
53
75
  end
54
76
 
55
77
  def test_iframe_canvas
56
78
  get(:iframe_canvas)
57
79
  assert_response :success
58
- assert_equal(
59
- normalize_url(
60
- 'https://graph.facebook.com/oauth/authorize?client_id=123&' \
61
- 'scope=&' \
62
- 'redirect_uri=http%3A%2F%2Fapps.facebook.com%2Fzzz%2Fiframe_canvas'),
63
- normalize_url(assigns(:rest_graph_authorize_url)))
80
+
81
+ url = normalize_url(
82
+ 'https://graph.facebook.com/oauth/authorize?client_id=123&' \
83
+ 'scope=&' \
84
+ 'redirect_uri=http%3A%2F%2Fapps.facebook.com%2Fzzz%2Fiframe_canvas')
85
+
86
+ assert_url(url)
64
87
  end
65
88
 
66
89
  def test_options
67
90
  get(:options)
68
91
  assert_response :redirect
69
- assert_equal(
70
- normalize_url(
71
- 'https://graph.facebook.com/oauth/authorize?client_id=123&' \
72
- 'scope=bogus&' \
73
- 'redirect_uri=http%3A%2F%2Ftest.host%2Foptions'),
74
- normalize_url((assigns(:rest_graph_authorize_url))))
92
+
93
+ url = normalize_url(
94
+ 'https://graph.facebook.com/oauth/authorize?client_id=123&' \
95
+ 'scope=bogus&' \
96
+ 'redirect_uri=http%3A%2F%2Ftest.host%2Foptions')
97
+
98
+ assert_url(url)
99
+ end
100
+
101
+ def test_protected
102
+ assert_nil @controller.public_methods.find{ |m| m.to_s =~ /^rest_graph/ }
75
103
  end
76
104
 
77
105
  def test_no_auto
@@ -140,4 +168,16 @@ class ApplicationControllerTest < ActionController::TestCase
140
168
  rescue => e
141
169
  assert_equal RestGraph::Error, e.class
142
170
  end
171
+
172
+ def test_reinitailize
173
+ get(:reinitialize)
174
+ assert_response :success
175
+ assert_equal [nil, {'a' => 'b'}], YAML.load(@response.body)
176
+ end
177
+
178
+ def test_helper
179
+ get(:helper)
180
+ assert_response :success
181
+ assert_equal RestGraph.default_app_id, @response.body.strip
182
+ end
143
183
  end
@@ -1,14 +1,18 @@
1
1
 
2
2
  ENV["RAILS_ENV"] = "test"
3
- require File.expand_path(File.dirname(__FILE__) + "/../config/environment")
4
- require 'test_help'
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
5
9
 
6
10
  class ActiveSupport::TestCase
7
- def normalize_query query
8
- '?' + query[1..-1].split('&').sort.join('&')
11
+ def normalize_query query, amp='&'
12
+ '?' + query[1..-1].split(amp).sort.join(amp)
9
13
  end
10
14
 
11
- def normalize_url url
12
- url.sub(/\?.+/){ |query| normalize_query(query) }
15
+ def normalize_url url, amp='&'
16
+ url.sub(/\?.+/){ |query| normalize_query(query, amp) }
13
17
  end
14
18
  end
@@ -0,0 +1,13 @@
1
+
2
+ source 'http://rubygems.org'
3
+
4
+ gem 'rails', '3.0.5'
5
+
6
+ gem 'rest-client' # for rest-graph
7
+ gem 'yajl-ruby' # for rest-graph
8
+ gem 'rest-graph'
9
+
10
+ group 'test' do
11
+ gem 'rr'
12
+ gem 'webmock'
13
+ end
@@ -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,118 @@
1
+
2
+ class ApplicationController < ActionController::Base
3
+ protect_from_forgery
4
+
5
+ include RestGraph::RailsUtil
6
+
7
+ before_filter :filter_common , :only => [:index]
8
+ before_filter :filter_canvas , :only => [:canvas]
9
+ before_filter :filter_options , :only => [:options]
10
+ before_filter :filter_no_auto , :only => [:no_auto]
11
+ before_filter :filter_diff_app_id , :only => [:diff_app_id]
12
+ before_filter :filter_diff_canvas , :only => [:diff_canvas]
13
+ before_filter :filter_iframe_canvas, :only => [:iframe_canvas]
14
+ before_filter :filter_cache , :only => [:cache]
15
+ before_filter :filter_hanlder , :only => [:handler_]
16
+ before_filter :filter_session , :only => [:session_]
17
+ before_filter :filter_cookies , :only => [:cookies_]
18
+
19
+ def index
20
+ render :text => rest_graph.get('me').to_json
21
+ end
22
+ alias_method :canvas , :index
23
+ alias_method :options , :index
24
+ alias_method :diff_canvas , :index
25
+ alias_method :iframe_canvas, :index
26
+ alias_method :handler_ , :index
27
+ alias_method :session_ , :index
28
+ alias_method :cookies_ , :index
29
+
30
+ def no_auto
31
+ rest_graph.get('me')
32
+ rescue RestGraph::Error
33
+ render :text => 'XD'
34
+ end
35
+
36
+ def diff_app_id
37
+ render :text => rest_graph.app_id
38
+ end
39
+
40
+ def cache
41
+ url = rest_graph.url('cache')
42
+ rest_graph.get('cache')
43
+ rest_graph.get('cache')
44
+ render :text => Rails.cache.read(Digest::MD5.hexdigest(url))
45
+ end
46
+
47
+ def error
48
+ raise RestGraph::Error.new("don't rescue me")
49
+ end
50
+
51
+ def reinitialize
52
+ cache_nil = rest_graph.cache
53
+ rest_graph_setup(:cache => {'a' => 'b'})
54
+ cache = rest_graph.cache
55
+ render :text => YAML.dump([cache_nil, cache])
56
+ end
57
+
58
+ def helper; end
59
+
60
+ private
61
+ def filter_common
62
+ rest_graph_setup(:auto_authorize => true, :canvas => '')
63
+ end
64
+
65
+ def filter_canvas
66
+ rest_graph_setup(:canvas => RestGraph.default_canvas,
67
+ :auto_authorize_scope => 'publish_stream')
68
+ end
69
+
70
+ def filter_diff_canvas
71
+ rest_graph_setup(:canvas => 'ToT',
72
+ :auto_authorize_scope => 'email')
73
+ end
74
+
75
+ def filter_iframe_canvas
76
+ rest_graph_setup(:canvas => 'zzz',
77
+ :auto_authorize => true)
78
+ end
79
+
80
+ def filter_no_auto
81
+ rest_graph_setup(:auto_authorize => false)
82
+ end
83
+
84
+ def filter_diff_app_id
85
+ rest_graph_setup(:app_id => 'zzz',
86
+ :auto_authorize => true)
87
+ end
88
+
89
+ def filter_options
90
+ rest_graph_setup(:auto_authorize_options => {:scope => 'bogus'},
91
+ :canvas => nil)
92
+ end
93
+
94
+ def filter_cache
95
+ rest_graph_setup(:cache => Rails.cache)
96
+ end
97
+
98
+ def filter_hanlder
99
+ rest_graph_setup(:write_handler => method(:write_handler),
100
+ :check_handler => method(:check_handler))
101
+ end
102
+
103
+ def write_handler fbs
104
+ Rails.cache[:fbs] = fbs
105
+ end
106
+
107
+ def check_handler
108
+ Rails.cache[:fbs]
109
+ end
110
+
111
+ def filter_session
112
+ rest_graph_setup(:write_session => true)
113
+ end
114
+
115
+ def filter_cookies
116
+ rest_graph_setup(:write_cookies => true)
117
+ end
118
+ end