djanowski-facebooker 1.0.1

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 (111) hide show
  1. data/CHANGELOG.txt +0 -0
  2. data/COPYING +19 -0
  3. data/History.txt +16 -0
  4. data/Manifest.txt +110 -0
  5. data/README +46 -0
  6. data/README.txt +81 -0
  7. data/Rakefile +76 -0
  8. data/TODO.txt +10 -0
  9. data/generators/facebook/facebook_generator.rb +14 -0
  10. data/generators/facebook/templates/config/facebooker.yml +41 -0
  11. data/generators/facebook/templates/public/javascripts/facebooker.js +106 -0
  12. data/generators/facebook_controller/USAGE +33 -0
  13. data/generators/facebook_controller/facebook_controller_generator.rb +40 -0
  14. data/generators/facebook_controller/templates/controller.rb +7 -0
  15. data/generators/facebook_controller/templates/functional_test.rb +11 -0
  16. data/generators/facebook_controller/templates/helper.rb +2 -0
  17. data/generators/facebook_controller/templates/view.fbml.erb +2 -0
  18. data/generators/facebook_controller/templates/view.html.erb +2 -0
  19. data/generators/facebook_publisher/facebook_publisher_generator.rb +14 -0
  20. data/generators/facebook_publisher/templates/create_facebook_templates.rb +15 -0
  21. data/generators/facebook_publisher/templates/publisher.rb +3 -0
  22. data/generators/facebook_scaffold/USAGE +27 -0
  23. data/generators/facebook_scaffold/facebook_scaffold_generator.rb +118 -0
  24. data/generators/facebook_scaffold/templates/controller.rb +93 -0
  25. data/generators/facebook_scaffold/templates/facebook_style.css +2579 -0
  26. data/generators/facebook_scaffold/templates/functional_test.rb +89 -0
  27. data/generators/facebook_scaffold/templates/helper.rb +2 -0
  28. data/generators/facebook_scaffold/templates/layout.fbml.erb +6 -0
  29. data/generators/facebook_scaffold/templates/layout.html.erb +17 -0
  30. data/generators/facebook_scaffold/templates/style.css +74 -0
  31. data/generators/facebook_scaffold/templates/view_edit.fbml.erb +13 -0
  32. data/generators/facebook_scaffold/templates/view_edit.html.erb +18 -0
  33. data/generators/facebook_scaffold/templates/view_index.fbml.erb +24 -0
  34. data/generators/facebook_scaffold/templates/view_index.html.erb +24 -0
  35. data/generators/facebook_scaffold/templates/view_new.fbml.erb +12 -0
  36. data/generators/facebook_scaffold/templates/view_new.html.erb +17 -0
  37. data/generators/facebook_scaffold/templates/view_show.fbml.erb +10 -0
  38. data/generators/facebook_scaffold/templates/view_show.html.erb +10 -0
  39. data/generators/publisher/publisher_generator.rb +14 -0
  40. data/init.rb +54 -0
  41. data/install.rb +12 -0
  42. data/lib/facebooker/adapters/adapter_base.rb +87 -0
  43. data/lib/facebooker/adapters/bebo_adapter.rb +73 -0
  44. data/lib/facebooker/adapters/facebook_adapter.rb +48 -0
  45. data/lib/facebooker/admin.rb +28 -0
  46. data/lib/facebooker/batch_request.rb +44 -0
  47. data/lib/facebooker/data.rb +57 -0
  48. data/lib/facebooker/feed.rb +78 -0
  49. data/lib/facebooker/logging.rb +51 -0
  50. data/lib/facebooker/model.rb +123 -0
  51. data/lib/facebooker/models/affiliation.rb +10 -0
  52. data/lib/facebooker/models/album.rb +11 -0
  53. data/lib/facebooker/models/applicationproperties.rb +39 -0
  54. data/lib/facebooker/models/cookie.rb +10 -0
  55. data/lib/facebooker/models/education_info.rb +11 -0
  56. data/lib/facebooker/models/event.rb +26 -0
  57. data/lib/facebooker/models/friend_list.rb +14 -0
  58. data/lib/facebooker/models/group.rb +35 -0
  59. data/lib/facebooker/models/info_item.rb +10 -0
  60. data/lib/facebooker/models/info_section.rb +10 -0
  61. data/lib/facebooker/models/location.rb +8 -0
  62. data/lib/facebooker/models/notifications.rb +17 -0
  63. data/lib/facebooker/models/page.rb +27 -0
  64. data/lib/facebooker/models/photo.rb +10 -0
  65. data/lib/facebooker/models/tag.rb +12 -0
  66. data/lib/facebooker/models/user.rb +352 -0
  67. data/lib/facebooker/models/work_info.rb +9 -0
  68. data/lib/facebooker/parser.rb +540 -0
  69. data/lib/facebooker/rails/controller.rb +246 -0
  70. data/lib/facebooker/rails/facebook_asset_path.rb +18 -0
  71. data/lib/facebooker/rails/facebook_form_builder.rb +112 -0
  72. data/lib/facebooker/rails/facebook_pretty_errors.rb +14 -0
  73. data/lib/facebooker/rails/facebook_request_fix.rb +24 -0
  74. data/lib/facebooker/rails/facebook_session_handling.rb +69 -0
  75. data/lib/facebooker/rails/facebook_url_rewriting.rb +39 -0
  76. data/lib/facebooker/rails/helpers.rb +615 -0
  77. data/lib/facebooker/rails/profile_publisher_extensions.rb +42 -0
  78. data/lib/facebooker/rails/publisher.rb +492 -0
  79. data/lib/facebooker/rails/routing.rb +49 -0
  80. data/lib/facebooker/rails/test_helpers.rb +88 -0
  81. data/lib/facebooker/rails/utilities.rb +22 -0
  82. data/lib/facebooker/server_cache.rb +24 -0
  83. data/lib/facebooker/service.rb +31 -0
  84. data/lib/facebooker/session.rb +564 -0
  85. data/lib/facebooker/version.rb +9 -0
  86. data/lib/facebooker.rb +143 -0
  87. data/lib/net/http_multipart_post.rb +123 -0
  88. data/lib/tasks/facebooker.rake +18 -0
  89. data/lib/tasks/tunnel.rake +46 -0
  90. data/rails/init.rb +1 -0
  91. data/setup.rb +1585 -0
  92. data/templates/layout.erb +24 -0
  93. data/test/adapters_test.rb +98 -0
  94. data/test/batch_request_test.rb +82 -0
  95. data/test/event_test.rb +15 -0
  96. data/test/facebook_admin_test.rb +75 -0
  97. data/test/facebook_cache_test.rb +43 -0
  98. data/test/facebook_data_test.rb +84 -0
  99. data/test/facebooker_test.rb +855 -0
  100. data/test/fixtures/multipart_post_body_with_only_parameters.txt +33 -0
  101. data/test/fixtures/multipart_post_body_with_single_file.txt +38 -0
  102. data/test/fixtures/multipart_post_body_with_single_file_that_has_nil_key.txt +38 -0
  103. data/test/http_multipart_post_test.rb +54 -0
  104. data/test/logging_test.rb +43 -0
  105. data/test/model_test.rb +91 -0
  106. data/test/publisher_test.rb +436 -0
  107. data/test/rails_integration_test.rb +1126 -0
  108. data/test/session_test.rb +587 -0
  109. data/test/test_helper.rb +60 -0
  110. data/test/user_test.rb +229 -0
  111. metadata +181 -0
@@ -0,0 +1,436 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+ require 'rubygems'
3
+ require 'flexmock/test_unit'
4
+ require 'action_controller'
5
+ require 'action_controller/test_process'
6
+ require 'active_record'
7
+ require File.dirname(__FILE__)+'/../init'
8
+ require 'facebooker/rails/controller'
9
+ require 'facebooker/rails/helpers'
10
+ require 'facebooker/rails/publisher'
11
+
12
+
13
+ module SymbolHelper
14
+ def symbol_helper_loaded
15
+ true
16
+ end
17
+ end
18
+
19
+ module ModuleHelper
20
+ def module_helper_loaded
21
+ true
22
+ end
23
+ end
24
+
25
+ ::RAILS_DEFAULT_LOGGER = Logger.new(STDOUT)
26
+
27
+ class TestPublisher < Facebooker::Rails::Publisher
28
+
29
+ helper :symbol
30
+ helper ModuleHelper
31
+
32
+ def action(f)
33
+ send_as :action
34
+ from f
35
+ title "Action Title"
36
+ end
37
+
38
+ def templatized_action(f)
39
+ send_as :templatized_action
40
+ from f
41
+ title_template "Templatized Action Title"
42
+ end
43
+
44
+ def story(to)
45
+ send_as :story
46
+ recipients to
47
+ title 'Story Title'
48
+ end
49
+
50
+ def notification(to,f)
51
+ send_as :notification
52
+ recipients to
53
+ from f
54
+ fbml "Not"
55
+ end
56
+
57
+ def email(to,f)
58
+ send_as :email
59
+ recipients to
60
+ from f
61
+ title "Email"
62
+ fbml 'text'
63
+ text fbml
64
+ end
65
+
66
+ def render_notification(to,f)
67
+ send_as :notification
68
+ recipients to
69
+ from f
70
+ fbml render(:inline=>"<%=module_helper_loaded%>")
71
+ end
72
+
73
+
74
+ def profile_update(to,f)
75
+ send_as :profile
76
+ recipients to
77
+ profile "profile"
78
+ profile_action "profile_action"
79
+ mobile_profile "mobile_profile"
80
+
81
+ end
82
+
83
+ def profile_update_with_profile_main(to,f)
84
+ send_as :profile
85
+ recipients to
86
+ from f
87
+ profile "profile"
88
+ profile_action "profile_action"
89
+ mobile_profile "mobile_profile"
90
+ profile_main "profile_main"
91
+ end
92
+
93
+ def ref_update(user)
94
+ send_as :ref
95
+ fbml "fbml"
96
+ handle "handle"
97
+ end
98
+
99
+ def user_action_template
100
+ one_line_story_template "{*actor*} did stuff with {*friend*}"
101
+ short_story_template "{*actor*} has a title {*friend*}", render(:inline=>"This is a test render")
102
+ full_story_template "{*actor*} did a lot","This is the full body",:img=>{:some_params=>true}
103
+ end
104
+
105
+ def simple_user_action_template
106
+ one_line_story_template "{*actor*} did stuff with {*friend*}"
107
+ end
108
+
109
+ def user_action_with_action_links_template
110
+ one_line_story_template "{*actor*} did stuff with {*friend*}"
111
+ short_story_template "{*actor*} has a title {*friend*}", render(:inline=>"This is a test render")
112
+ full_story_template "{*actor*} did a lot","This is the full body",:img=>{:some_params=>true}
113
+ action_links action_link("Source","HREF")
114
+ end
115
+
116
+ def user_action(user)
117
+ send_as :user_action
118
+ from user
119
+ data :friend=>"Mike"
120
+ end
121
+ def user_action_no_data(user)
122
+ send_as :user_action
123
+ from user
124
+ end
125
+
126
+ def no_send_as(to)
127
+ recipients to
128
+ end
129
+
130
+ def invalid_send_as(to)
131
+ send_as :fake
132
+ recipients to
133
+ end
134
+
135
+ end
136
+
137
+ class FacebookTemplateTest < Test::Unit::TestCase
138
+ FacebookTemplate = Facebooker::Rails::Publisher::FacebookTemplate
139
+
140
+ def setup
141
+ super
142
+ @template = mock("facebook template")
143
+ FacebookTemplate.stubs(:register).returns(@template)
144
+ FacebookTemplate.clear_cache!
145
+ end
146
+
147
+ def test_find_or_register_calls_find_cached
148
+ FacebookTemplate.expects(:find_cached).with(TestPublisher,"simple_user_action").returns(@template)
149
+ assert_equal FacebookTemplate.for_class_and_method(TestPublisher,"simple_user_action"),@template
150
+ end
151
+
152
+ def test_find_cached_should_use_cached_if_it_exists
153
+ FacebookTemplate.cache(TestPublisher,"simple_user_action",@template)
154
+ assert_equal FacebookTemplate.find_cached(TestPublisher,"simple_user_action"), @template
155
+
156
+ end
157
+
158
+ def test_find_cached_should_call_find_in_db_if_not_in_cache
159
+ FacebookTemplate.expects(:find_in_db).with(TestPublisher,"simple_user_action").returns(@template)
160
+ assert_equal FacebookTemplate.find_cached(TestPublisher,"simple_user_action"), @template
161
+ end
162
+
163
+ def test_find_in_db_should_run_find
164
+ FacebookTemplate.expects(:find_by_template_name).with("TestPublisher::simple_user_action").returns(@template)
165
+ @template.stubs(:template_changed?).returns(false)
166
+ assert_equal FacebookTemplate.find_in_db(TestPublisher,"simple_user_action"), @template
167
+ end
168
+
169
+ def test_find_in_db_should_register_if_not_found
170
+ FacebookTemplate.expects(:find_by_template_name).with("TestPublisher::simple_user_action").returns(nil)
171
+ FacebookTemplate.expects(:register).with(TestPublisher,"simple_user_action").returns(@template)
172
+ FacebookTemplate.find_cached(TestPublisher,"simple_user_action")
173
+
174
+ end
175
+
176
+ def test_find_in_db_should_check_for_change_if_found
177
+ FacebookTemplate.stubs(:find_by_template_name).returns(@template)
178
+ FacebookTemplate.stubs(:hashed_content).returns("MY CONTENT")
179
+ @template.expects(:template_changed?).with("MY CONTENT").returns(false)
180
+ FacebookTemplate.find_in_db(TestPublisher,"simple_user_action")
181
+ end
182
+
183
+ def test_find_in_db_should_destroy_old_record_if_changed
184
+ FacebookTemplate.stubs(:find_by_template_name).returns(@template)
185
+ FacebookTemplate.stubs(:hashed_content).returns("MY CONTENT")
186
+ @template.stubs(:template_changed?).returns(true)
187
+ @template.expects(:destroy)
188
+ FacebookTemplate.find_in_db(TestPublisher,"simple_user_action")
189
+ end
190
+
191
+ def test_find_in_db_should_re_register_if_changed
192
+ FacebookTemplate.stubs(:find_by_template_name).with("TestPublisher::simple_user_action").returns(@template)
193
+ FacebookTemplate.stubs(:hashed_content).returns("MY CONTENT")
194
+ @template.stubs(:template_changed?).returns(true)
195
+ @template.stubs(:destroy)
196
+ FacebookTemplate.expects(:register).with(TestPublisher,"simple_user_action").returns(@template)
197
+ FacebookTemplate.find_in_db(TestPublisher,"simple_user_action")
198
+ end
199
+ end
200
+
201
+ class PublisherTest < Test::Unit::TestCase
202
+ FacebookTemplate = Facebooker::Rails::Publisher::FacebookTemplate
203
+
204
+ def setup
205
+ super
206
+ @user = Facebooker::User.new
207
+ @user.id=4
208
+ @session = "session"
209
+ @user.stubs(:session).returns(@session)
210
+ end
211
+
212
+ def teardown
213
+ super
214
+ end
215
+
216
+ def test_create_action
217
+ action=TestPublisher.create_action(@user)
218
+ assert_equal Facebooker::Feed::Action,action.class
219
+ assert_equal "Action Title",action.title
220
+ end
221
+
222
+ def test_deliver_action
223
+ @user.expects(:publish_action)
224
+ TestPublisher.deliver_action(@user)
225
+ end
226
+
227
+ def test_create_story
228
+ action=TestPublisher.create_story(@user)
229
+ assert_equal Facebooker::Feed::Story,action.class
230
+ assert_equal "Story Title",action.title
231
+ end
232
+
233
+ def test_deliver_story
234
+ @user.expects(:publish_story)
235
+ TestPublisher.deliver_story(@user)
236
+ end
237
+
238
+ def test_create_notification
239
+ notification=TestPublisher.create_notification(12451752,@user)
240
+ assert_equal Facebooker::Rails::Publisher::Notification,notification.class
241
+ assert_equal "Not",notification.fbml
242
+ end
243
+
244
+ def test_deliver_notification
245
+ @session.expects(:send_notification)
246
+ TestPublisher.deliver_notification("12451752",@user)
247
+ end
248
+
249
+ def test_create_email
250
+ email=TestPublisher.create_email("12451752",@user)
251
+ assert_equal Facebooker::Rails::Publisher::Email,email.class
252
+ assert_equal "Email",email.title
253
+ assert_equal "text",email.text
254
+ assert_equal "text",email.fbml
255
+ end
256
+
257
+ def test_deliver_email
258
+ @session.expects(:send_email)
259
+ TestPublisher.deliver_email("12451752",@user)
260
+ end
261
+
262
+ def test_create_templatized_action
263
+ ta=TestPublisher.create_templatized_action(@user)
264
+ assert_equal Facebooker::Feed::TemplatizedAction,ta.class
265
+ assert_equal "Templatized Action Title",ta.title_template
266
+
267
+ end
268
+
269
+
270
+
271
+ def test_deliver_templatized_action
272
+ @user.expects(:publish_action)
273
+ TestPublisher.deliver_templatized_action(@user)
274
+ end
275
+ def test_create_profile_update
276
+ p=TestPublisher.create_profile_update(@user,@user)
277
+ assert_equal Facebooker::Rails::Publisher::Profile,p.class
278
+ assert_equal "profile",p.profile
279
+ assert_equal "profile_action",p.profile_action
280
+ assert_equal "mobile_profile",p.mobile_profile
281
+ end
282
+ def test_create_profile_update_with_profile_main
283
+ p=TestPublisher.create_profile_update_with_profile_main(@user,@user)
284
+ assert_equal Facebooker::Rails::Publisher::Profile,p.class
285
+ assert_equal "profile",p.profile
286
+ assert_equal "profile_action",p.profile_action
287
+ assert_equal "mobile_profile",p.mobile_profile
288
+ assert_equal "profile_main",p.profile_main
289
+ end
290
+
291
+
292
+ def test_deliver_profile
293
+ Facebooker::User.stubs(:new).returns(@user)
294
+ @user.expects(:set_profile_fbml).with('profile', 'mobile_profile', 'profile_action',nil)
295
+ TestPublisher.deliver_profile_update(@user,@user)
296
+ end
297
+
298
+ def test_deliver_profile_with_main
299
+ Facebooker::User.stubs(:new).returns(@user)
300
+ @user.expects(:set_profile_fbml).with('profile', 'mobile_profile', 'profile_action','profile_main')
301
+ TestPublisher.deliver_profile_update_with_profile_main(@user,@user)
302
+ end
303
+
304
+
305
+ def test_create_ref_update
306
+ p=TestPublisher.create_ref_update(@user)
307
+ assert_equal Facebooker::Rails::Publisher::Ref,p.class
308
+ assert_equal "fbml",p.fbml
309
+ assert_equal "handle",p.handle
310
+ end
311
+
312
+ def test_deliver_ref_update
313
+ Facebooker::Session.stubs(:create).returns(@session)
314
+ @server_cache="server_cache"
315
+ @session.expects(:server_cache).returns(@server_cache)
316
+ @server_cache.expects(:set_ref_handle).with("handle","fbml")
317
+ TestPublisher.deliver_ref_update(@user)
318
+ end
319
+
320
+ def test_register_user_action
321
+ Facebooker::Rails::Publisher::FacebookTemplate.expects(:register)
322
+ TestPublisher.register_user_action
323
+ end
324
+ def test_register_user_action_with_action_links
325
+ ActionController::Base.append_view_path("./test/../../app/views")
326
+ Facebooker::Rails::Publisher::FacebookTemplate.expects(:register)
327
+ TestPublisher.register_user_action_with_action_links
328
+ end
329
+
330
+ def test_create_user_action
331
+ @from_user = Facebooker::User.new
332
+ @session = Facebooker::Session.new("","")
333
+ @from_user.stubs(:session).returns(@session)
334
+ Facebooker::Rails::Publisher::FacebookTemplate.expects(:bundle_id_for_class_and_method).
335
+ with(TestPublisher,'user_action').
336
+ returns(20309041537)
337
+ ua = TestPublisher.create_user_action(@from_user)
338
+ assert_equal "user_action", ua.template_name
339
+ end
340
+
341
+ def test_publisher_user_action
342
+ @from_user = Facebooker::User.new
343
+ @session = Facebooker::Session.new("","")
344
+ @from_user.stubs(:session).returns(@session)
345
+ @session.expects(:publish_user_action).with(20309041537,{:friend=>"Mike"},nil,nil)
346
+
347
+ Facebooker::Rails::Publisher::FacebookTemplate.expects(:bundle_id_for_class_and_method).
348
+ with(TestPublisher, 'user_action').
349
+ returns(20309041537)
350
+ # pseudo_template = Struct.new(:bundle_id, :content_hash).new(20309041537, '')
351
+ # pseudo_template.expects(:matches_content?).returns(true)
352
+ # Facebooker::Rails::Publisher::FacebookTemplate.expects(:for).returns(pseudo_template)
353
+ TestPublisher.deliver_user_action(@from_user)
354
+ end
355
+
356
+ def test_publishing_user_data_no_action_gives_nil_hash
357
+ @from_user = Facebooker::User.new
358
+ @session = Facebooker::Session.new("","")
359
+ @from_user.stubs(:session).returns(@session)
360
+ @session.expects(:publish_user_action).with(20309041537,{},nil,nil)
361
+
362
+ Facebooker::Rails::Publisher::FacebookTemplate.stubs(:bundle_id_for_class_and_method).returns(20309041537)
363
+ TestPublisher.deliver_user_action_no_data(@from_user)
364
+ end
365
+ def test_no_sends_as_raises
366
+ assert_raises(Facebooker::Rails::Publisher::UnspecifiedBodyType) {
367
+ TestPublisher.deliver_no_send_as(@user)
368
+ }
369
+ end
370
+
371
+ def test_invalid_send_as_raises
372
+ assert_raises(Facebooker::Rails::Publisher::UnknownBodyType) {
373
+ TestPublisher.deliver_invalid_send_as(@user)
374
+ }
375
+ end
376
+
377
+
378
+ def test_keeps_class_method_missing
379
+ assert_raises(NoMethodError) {
380
+ TestPublisher.fake
381
+ }
382
+ end
383
+ def test_keeps_instance_method_missing
384
+ assert_raises(NoMethodError) {
385
+ TestPublisher.new.fake
386
+ }
387
+ end
388
+
389
+ def test_image_urls
390
+ Facebooker.expects(:facebook_path_prefix).returns("/mike")
391
+ assert_equal({:src => '/images/image.png', :href => 'raw_string' },
392
+ TestPublisher.new.image('image.png', 'raw_string'))
393
+ assert_equal({:src => '/images/image.png', :href => 'http://apps.facebook.com/mike/pokes/do/1' },
394
+ TestPublisher.new.image('image.png', {:only_path=>false,:controller => :pokes, :action => :do, :id => 1}))
395
+ end
396
+
397
+ def test_action_link
398
+ assert_equal({:text=>"text", :href=>"href"}, TestPublisher.new.action_link("text","href"))
399
+ end
400
+
401
+ def test_default_url_options
402
+ Facebooker.expects(:facebook_path_prefix).returns("/mike")
403
+ assert_equal({:host=>"apps.facebook.com/mike"},TestPublisher.default_url_options)
404
+ end
405
+
406
+ def test_recipients
407
+ tp=TestPublisher.new
408
+ tp.recipients "a"
409
+ assert_equal("a",tp.recipients)
410
+ end
411
+
412
+ def test_symbol_helper
413
+ assert TestPublisher.new.symbol_helper_loaded
414
+ end
415
+ def test_module_helper
416
+ assert TestPublisher.new.module_helper_loaded
417
+ end
418
+
419
+ def test_with_render
420
+ #normally Rails would do this for us
421
+ if ActionController::Base.respond_to?(:append_view_path)
422
+ ActionController::Base.append_view_path("./test/../../app/views")
423
+ end
424
+ notification=TestPublisher.create_render_notification(12451752,@user)
425
+ assert_equal "true",notification.fbml
426
+ end
427
+
428
+ def test_notification_as_announcement
429
+ #normally Rails would do this for us
430
+ if ActionController::Base.respond_to?(:append_view_path)
431
+ ActionController::Base.append_view_path("./test/../../app/views")
432
+ end
433
+ notification=TestPublisher.create_render_notification(12451752,nil)
434
+ assert_equal "true",notification.fbml
435
+ end
436
+ end