djanowski-mmangino-facebooker 1.0.4

Sign up to get free protection for your applications and to get access to all the features.
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 +70 -0
  41. data/install.rb +12 -0
  42. data/lib/facebooker.rb +143 -0
  43. data/lib/facebooker/adapters/adapter_base.rb +87 -0
  44. data/lib/facebooker/adapters/bebo_adapter.rb +75 -0
  45. data/lib/facebooker/adapters/facebook_adapter.rb +48 -0
  46. data/lib/facebooker/admin.rb +28 -0
  47. data/lib/facebooker/batch_request.rb +44 -0
  48. data/lib/facebooker/data.rb +57 -0
  49. data/lib/facebooker/feed.rb +78 -0
  50. data/lib/facebooker/logging.rb +51 -0
  51. data/lib/facebooker/model.rb +123 -0
  52. data/lib/facebooker/models/affiliation.rb +10 -0
  53. data/lib/facebooker/models/album.rb +11 -0
  54. data/lib/facebooker/models/applicationproperties.rb +39 -0
  55. data/lib/facebooker/models/cookie.rb +10 -0
  56. data/lib/facebooker/models/education_info.rb +11 -0
  57. data/lib/facebooker/models/event.rb +26 -0
  58. data/lib/facebooker/models/friend_list.rb +14 -0
  59. data/lib/facebooker/models/group.rb +35 -0
  60. data/lib/facebooker/models/info_item.rb +10 -0
  61. data/lib/facebooker/models/info_section.rb +10 -0
  62. data/lib/facebooker/models/location.rb +8 -0
  63. data/lib/facebooker/models/notifications.rb +17 -0
  64. data/lib/facebooker/models/page.rb +27 -0
  65. data/lib/facebooker/models/photo.rb +10 -0
  66. data/lib/facebooker/models/tag.rb +12 -0
  67. data/lib/facebooker/models/user.rb +367 -0
  68. data/lib/facebooker/models/work_info.rb +9 -0
  69. data/lib/facebooker/parser.rb +547 -0
  70. data/lib/facebooker/rails/controller.rb +246 -0
  71. data/lib/facebooker/rails/facebook_asset_path.rb +18 -0
  72. data/lib/facebooker/rails/facebook_form_builder.rb +112 -0
  73. data/lib/facebooker/rails/facebook_pretty_errors.rb +14 -0
  74. data/lib/facebooker/rails/facebook_request_fix.rb +24 -0
  75. data/lib/facebooker/rails/facebook_session_handling.rb +69 -0
  76. data/lib/facebooker/rails/facebook_url_rewriting.rb +39 -0
  77. data/lib/facebooker/rails/helpers.rb +615 -0
  78. data/lib/facebooker/rails/profile_publisher_extensions.rb +42 -0
  79. data/lib/facebooker/rails/publisher.rb +497 -0
  80. data/lib/facebooker/rails/routing.rb +49 -0
  81. data/lib/facebooker/rails/test_helpers.rb +88 -0
  82. data/lib/facebooker/rails/utilities.rb +22 -0
  83. data/lib/facebooker/server_cache.rb +24 -0
  84. data/lib/facebooker/service.rb +31 -0
  85. data/lib/facebooker/session.rb +564 -0
  86. data/lib/facebooker/version.rb +9 -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 +253 -0
  111. metadata +181 -0
@@ -0,0 +1,39 @@
1
+ module ::ActionController
2
+ class AbstractRequest
3
+ def relative_url_root
4
+ Facebooker.path_prefix
5
+ end
6
+ end
7
+
8
+ class Base
9
+ def self.relative_url_root
10
+ Facebooker.path_prefix
11
+ end
12
+ end
13
+
14
+ class UrlRewriter
15
+ RESERVED_OPTIONS << :canvas
16
+ def link_to_new_canvas?
17
+ @request.parameters["fb_sig_in_new_facebook"] == "1"
18
+ end
19
+ def link_to_canvas?(params, options)
20
+ option_override = options[:canvas]
21
+ return false if option_override == false # important to check for false. nil should use default behavior
22
+ option_override || @request.parameters["fb_sig_in_canvas"] == "1" || @request.parameters[:fb_sig_in_canvas] == "1"
23
+ end
24
+
25
+ def rewrite_url_with_facebooker(*args)
26
+ options = args.first.is_a?(Hash) ? args.first : args.last
27
+ is_link_to_canvas = link_to_canvas?(@request.request_parameters, options)
28
+ if is_link_to_canvas && !options.has_key?(:host)
29
+ options[:host] = Facebooker.canvas_server_base
30
+ end
31
+ options.delete(:canvas)
32
+ Facebooker.request_for_canvas(is_link_to_canvas) do
33
+ rewrite_url_without_facebooker(*args)
34
+ end
35
+ end
36
+
37
+ alias_method_chain :rewrite_url, :facebooker
38
+ end
39
+ end
@@ -0,0 +1,615 @@
1
+ module Facebooker
2
+ module Rails
3
+
4
+ # Facebook specific helpers for creating FBML
5
+ #
6
+ # All helpers that take a user as a parameter will get the Facebook UID from the facebook_id attribute if it exists.
7
+ # It will use to_s if the facebook_id attribute is not present.
8
+ #
9
+ module Helpers
10
+
11
+
12
+ # Create an fb:dialog
13
+ # id must be a unique name e.g. "my_dialog"
14
+ # cancel_button is true or false
15
+ def fb_dialog( id, cancel_button, &block )
16
+ content = capture(&block)
17
+ concat( content_tag("fb:dialog", content, {:id => id, :cancel_button => cancel_button}), block.binding )
18
+ end
19
+
20
+ def fb_dialog_title( title )
21
+ content_tag "fb:dialog-title", title
22
+ end
23
+
24
+ def fb_dialog_content( &block )
25
+ content = capture(&block)
26
+ concat( content_tag("fb:dialog-content", content), block.binding )
27
+ end
28
+
29
+ def fb_dialog_button( type, value, options={} )
30
+ options.assert_valid_keys FB_DIALOG_BUTTON_VALID_OPTION_KEYS
31
+ options.merge! :type => type, :value => value
32
+ tag "fb:dialog-button", options
33
+ end
34
+
35
+ FB_DIALOG_BUTTON_VALID_OPTION_KEYS = [:close_dialog, :href, :form_id, :clickrewriteurl, :clickrewriteid, :clickrewriteform]
36
+
37
+ # Create an fb:request-form without a selector
38
+ #
39
+ # The block passed to this tag is used as the content of the form
40
+ #
41
+ # The message param is the name sent to content_for that specifies the body of the message
42
+ #
43
+ # For example,
44
+ #
45
+ # <% content_for("invite_message") do %>
46
+ # This gets sent in the invite. <%= fb_req_choice("with a button!",new_poke_path) %>
47
+ # <% end %>
48
+ # <% fb_request_form("Poke","invite_message",create_poke_path) do %>
49
+ # Send a poke to: <%= fb_friend_selector %> <br />
50
+ # <%= fb_request_form_submit %>
51
+ # <% end %>
52
+ def fb_request_form(type,message_param,url,options={},&block)
53
+ content = capture(&block)
54
+ message = @template.instance_variable_get("@content_for_#{message_param}")
55
+ concat(content_tag("fb:request-form", content + token_tag,
56
+ {:action=>url,:method=>"post",:invite=>true,:type=>type,:content=>message}.merge(options)),
57
+ block.binding)
58
+ end
59
+
60
+ # Create a submit button for an <fb:request-form>
61
+ # If the request is for an individual user you can optionally
62
+ # Provide the user and a label for the request button.
63
+ # For example
64
+ # <% content_for("invite_user") do %>
65
+ # This gets sent in the invite. <%= fb_req_choice("Come join us!",new_invite_path) %>
66
+ # <% end %>
67
+ # <% fb_request_form("Invite","invite_user",create_invite_path) do %>
68
+ # Invite <%= fb_name(@facebook_user.friends.first.id)%> to the party <br />
69
+ # <%= fb_request_form_submit(@facebook_user.friends.first.id,"Invite %n") %>
70
+ # <% end %>
71
+ # <em>See:</em> http://wiki.developers.facebook.com/index.php/Fb:request-form-submit for options
72
+ def fb_request_form_submit(options={})
73
+ tag("fb:request-form-submit",stringify_vals(options))
74
+ end
75
+
76
+
77
+ # Create an fb:request-form with an fb_multi_friend_selector inside
78
+ #
79
+ # The content of the block are used as the message on the form,
80
+ #
81
+ # For example:
82
+ # <% fb_multi_friend_request("Poke","Choose some friends to Poke",create_poke_path) do %>
83
+ # If you select some friends, they will see this message.
84
+ # <%= fb_req_choice("They will get this button, too",new_poke_path) %>
85
+ # <% end %>
86
+ def fb_multi_friend_request(type,friend_selector_message,url,&block)
87
+ content = capture(&block)
88
+ concat(content_tag("fb:request-form",
89
+ fb_multi_friend_selector(friend_selector_message) + token_tag,
90
+ {:action=>url,:method=>"post",:invite=>true,:type=>type,:content=>content}
91
+ ),
92
+ block.binding)
93
+ end
94
+
95
+ # Render an <fb:friend-selector> element
96
+ # <em>See:</em> http://wiki.developers.facebook.com/index.php/Fb:friend-selector for options
97
+ #
98
+ def fb_friend_selector(options={})
99
+ tag("fb:friend-selector",stringify_vals(options))
100
+ end
101
+
102
+ # Render an <fb:multi-friend-input> element
103
+ # <em> See: </em> http://wiki.developers.facebook.com/index.php/Fb:multi-friend-input for options
104
+ def fb_multi_friend_input(options={})
105
+ tag "fb:multi-friend-input",stringify_vals(options)
106
+ end
107
+
108
+ # Render an <fb:multi-friend-selector> with the passed in welcome message
109
+ # Full version shows all profile pics for friends.
110
+ # <em> See: </em> http://wiki.developers.facebook.com/index.php/Fb:multi-friend-selector for options
111
+ # <em> Note: </em> I don't think the block is used here.
112
+ def fb_multi_friend_selector(message,options={},&block)
113
+ options = options.dup
114
+ tag("fb:multi-friend-selector",stringify_vals(options.merge(:showborder=>false,:actiontext=>message,:max=>20)))
115
+ end
116
+
117
+ # Render a condensed <fb:multi-friend-selector> with the passed in welcome message
118
+ # Condensed version show checkboxes for each friend.
119
+ # <em> See: </em> http://wiki.developers.facebook.com/index.php/Fb:multi-friend-selector_%28condensed%29 for options
120
+ # <em> Note: </em> I don't think the block is used here.
121
+ def fb_multi_friend_selector_condensed(options={},&block)
122
+ options = options.dup
123
+ tag("fb:multi-friend-selector",stringify_vals(options.merge(:condensed=>true)))
124
+ end
125
+
126
+ # Render a button in a request using the <fb:req-choice> tag
127
+ # url must be an absolute url
128
+ # This should be used inside the block of an fb_multi_friend_request
129
+ def fb_req_choice(label,url)
130
+ tag "fb:req-choice",:label=>label,:url=>url
131
+ end
132
+
133
+ # Create a facebook form using <fb:editor>
134
+ #
135
+ # It yields a form builder that will convert the standard rails form helpers
136
+ # into the facebook specific version.
137
+ #
138
+ # Example:
139
+ # <% facebook_form_for(:poke,@poke,:url => create_poke_path) do |f| %>
140
+ # <%= f.text_field :message, :label=>"message" %>
141
+ # <%= f.buttons "Save Poke" %>
142
+ # <% end %>
143
+ #
144
+ # will generate
145
+ #
146
+ # <fb:editor action="/pokes/create">
147
+ # <fb:editor-text name="poke[message]" id="poke_message" value="" label="message" />
148
+ # <fb:editor-buttonset>
149
+ # <fb:editor-button label="Save Poke"
150
+ # </fb:editor-buttonset>
151
+ # </fb:editor>
152
+ def facebook_form_for( record_or_name_or_array,*args, &proc)
153
+
154
+ raise ArgumentError, "Missing block" unless block_given?
155
+ options = args.last.is_a?(Hash) ? args.pop : {}
156
+
157
+ case record_or_name_or_array
158
+ when String, Symbol
159
+ object_name = record_or_name_or_array
160
+ when Array
161
+ object = record_or_name_or_array.last
162
+ object_name = ActionController::RecordIdentifier.singular_class_name(object)
163
+ apply_form_for_options!(record_or_name_or_array, options)
164
+ args.unshift object
165
+ else
166
+ object = record_or_name_or_array
167
+ object_name = ActionController::RecordIdentifier.singular_class_name(object)
168
+ apply_form_for_options!([object], options)
169
+ args.unshift object
170
+ end
171
+ method = (options[:html]||{})[:method]
172
+ options[:builder] ||= Facebooker::Rails::FacebookFormBuilder
173
+ editor_options={}
174
+
175
+ action=options.delete(:url)
176
+ editor_options[:action]= action unless action.blank?
177
+ width=options.delete(:width)
178
+ editor_options[:width]=width unless width.blank?
179
+ width=options.delete(:labelwidth)
180
+ editor_options[:labelwidth]=width unless width.blank?
181
+
182
+ concat(tag("fb:editor",editor_options,true) , proc.binding)
183
+ concat(tag(:input,{:type=>"hidden",:name=>:_method, :value=>method},false), proc.binding) unless method.blank?
184
+ concat(token_tag, proc.binding)
185
+ fields_for( object_name,*(args << options), &proc)
186
+ concat("</fb:editor>",proc.binding)
187
+ end
188
+
189
+ # Render an fb:name tag for the given user
190
+ # This renders the name of the user specified. You can use this tag as both subject and object of
191
+ # a sentence. <em> See </em> http://wiki.developers.facebook.com/index.php/Fb:name for full description.
192
+ # Use this tag on FBML pages instead of retrieving the user's info and rendering the name explicitly.
193
+ #
194
+ def fb_name(user, options={})
195
+ options = options.dup
196
+ options.transform_keys!(FB_NAME_OPTION_KEYS_TO_TRANSFORM)
197
+ options.assert_valid_keys(FB_NAME_VALID_OPTION_KEYS)
198
+ options.merge!(:uid => cast_to_facebook_id(user))
199
+ tag("fb:name", stringify_vals(options))
200
+ end
201
+
202
+ FB_NAME_OPTION_KEYS_TO_TRANSFORM = {:first_name_only => :firstnameonly,
203
+ :last_name_only => :lastnameonly,
204
+ :show_network => :shownetwork,
205
+ :use_you => :useyou,
206
+ :if_cant_see => :ifcantsee,
207
+ :subject_id => :subjectid}
208
+ FB_NAME_VALID_OPTION_KEYS = [:firstnameonly, :linked, :lastnameonly, :possessive, :reflexive,
209
+ :shownetwork, :useyou, :ifcantsee, :capitalize, :subjectid]
210
+
211
+ # Render an <fb:pronoun> tag for the specified user
212
+ # Options give flexibility for placing in any part of a sentence.
213
+ # <em> See </em> http://wiki.developers.facebook.com/index.php/Fb:pronoun for complete list of options.
214
+ #
215
+ def fb_pronoun(user, options={})
216
+ options = options.dup
217
+ options.transform_keys!(FB_PRONOUN_OPTION_KEYS_TO_TRANSFORM)
218
+ options.assert_valid_keys(FB_PRONOUN_VALID_OPTION_KEYS)
219
+ options.merge!(:uid => cast_to_facebook_id(user))
220
+ tag("fb:pronoun", stringify_vals(options))
221
+ end
222
+
223
+ FB_PRONOUN_OPTION_KEYS_TO_TRANSFORM = {:use_you => :useyou, :use_they => :usethey}
224
+ FB_PRONOUN_VALID_OPTION_KEYS = [:useyou, :possessive, :reflexive, :objective,
225
+ :usethey, :capitalize]
226
+
227
+ # Render an fb:ref tag.
228
+ # Options must contain either url or handle.
229
+ # * <em> url </em> The URL from which to fetch the FBML. You may need to call fbml.refreshRefUrl to refresh cache
230
+ # * <em> handle </em> The string previously set by fbml.setRefHandle that identifies the FBML
231
+ # <em> See </em> http://wiki.developers.facebook.com/index.php/Fb:ref for complete description
232
+ def fb_ref(options)
233
+ options.assert_valid_keys(FB_REF_VALID_OPTION_KEYS)
234
+ validate_fb_ref_has_either_url_or_handle(options)
235
+ validate_fb_ref_does_not_have_both_url_and_handle(options)
236
+ tag("fb:ref", stringify_vals(options))
237
+ end
238
+
239
+ def validate_fb_ref_has_either_url_or_handle(options)
240
+ unless options.has_key?(:url) || options.has_key?(:handle)
241
+ raise ArgumentError, "fb_ref requires :url or :handle"
242
+ end
243
+ end
244
+
245
+ def validate_fb_ref_does_not_have_both_url_and_handle(options)
246
+ if options.has_key?(:url) && options.has_key?(:handle)
247
+ raise ArgumentError, "fb_ref may not have both :url and :handle"
248
+ end
249
+ end
250
+
251
+ FB_REF_VALID_OPTION_KEYS = [:url, :handle]
252
+
253
+ # Render an <fb:profile-pic> for the specified user.
254
+ #
255
+ # You can optionally specify the size using the :size=> option.
256
+ #
257
+ # Valid sizes are :thumb, :small, :normal and :square
258
+ def fb_profile_pic(user, options={})
259
+ options = options.dup
260
+ validate_fb_profile_pic_size(options)
261
+ options.merge!(:uid => cast_to_facebook_id(user))
262
+ tag("fb:profile-pic", stringify_vals(options))
263
+ end
264
+
265
+ # Render an fb:photo tag.
266
+ # photo is either a Facebooker::Photo or an id of a Facebook photo or an object that responds to photo_id.
267
+ # <em> See: </em> http://wiki.developers.facebook.com/index.php/Fb:photo for complete list of options.
268
+ def fb_photo(photo, options={})
269
+ options = options.dup
270
+ options.assert_valid_keys(FB_PHOTO_VALID_OPTION_KEYS)
271
+ options.merge!(:pid => cast_to_photo_id(photo))
272
+ validate_fb_photo_size(options)
273
+ validate_fb_photo_align_value(options)
274
+ tag("fb:photo", stringify_vals(options))
275
+ end
276
+
277
+ FB_PHOTO_VALID_OPTION_KEYS = [:uid, :size, :align]
278
+
279
+ def cast_to_photo_id(object)
280
+ object.respond_to?(:photo_id) ? object.photo_id : object
281
+ end
282
+
283
+ VALID_FB_SHARED_PHOTO_SIZES = [:thumb, :small, :normal, :square]
284
+ VALID_FB_PHOTO_SIZES = VALID_FB_SHARED_PHOTO_SIZES
285
+ VALID_FB_PROFILE_PIC_SIZES = VALID_FB_SHARED_PHOTO_SIZES
286
+ VALID_PERMISSIONS=[:email, :offline_access, :status_update, :photo_upload, :create_listing, :create_event, :rsvp_event, :sms]
287
+
288
+ # Render an fb:tabs tag containing some number of fb:tab_item tags.
289
+ # Example:
290
+ # <% fb_tabs do %>
291
+ # <%= fb_tab_item("Home", "home") %>
292
+ # <%= fb_tab_item("Office", "office") %>
293
+ # <% end %>
294
+ def fb_tabs(&block)
295
+ content = capture(&block)
296
+ concat(content_tag("fb:tabs", content), block.binding)
297
+ end
298
+
299
+ # Render an fb:tab_item tag.
300
+ # Use this in conjunction with fb_tabs
301
+ # Options can contains :selected => true to indicate that a tab is the current tab.
302
+ # <em> See: </em> http://wiki.developers.facebook.com/index.php/Fb:tab-item for complete list of options
303
+ def fb_tab_item(title, url, options={})
304
+ options= options.dup
305
+ options.assert_valid_keys(FB_TAB_ITEM_VALID_OPTION_KEYS)
306
+ options.merge!(:title => title, :href => url)
307
+ validate_fb_tab_item_align_value(options)
308
+ tag("fb:tab-item", stringify_vals(options))
309
+ end
310
+
311
+ FB_TAB_ITEM_VALID_OPTION_KEYS = [:align, :selected]
312
+
313
+ def validate_fb_tab_item_align_value(options)
314
+ if options.has_key?(:align) && !VALID_FB_TAB_ITEM_ALIGN_VALUES.include?(options[:align].to_sym)
315
+ raise(ArgumentError, "Unkown value for align: #{options[:align]}")
316
+ end
317
+ end
318
+
319
+ def validate_fb_photo_align_value(options)
320
+ if options.has_key?(:align) && !VALID_FB_PHOTO_ALIGN_VALUES.include?(options[:align].to_sym)
321
+ raise(ArgumentError, "Unkown value for align: #{options[:align]}")
322
+ end
323
+ end
324
+
325
+ VALID_FB_SHARED_ALIGN_VALUES = [:left, :right]
326
+ VALID_FB_PHOTO_ALIGN_VALUES = VALID_FB_SHARED_ALIGN_VALUES
327
+ VALID_FB_TAB_ITEM_ALIGN_VALUES = VALID_FB_SHARED_ALIGN_VALUES
328
+
329
+
330
+ # Create a Facebook wall. It can contain fb_wall_posts
331
+ #
332
+ # For Example:
333
+ # <% fb_wall do %>
334
+ # <%= fb_wall_post(@user,"This is my message") %>
335
+ # <%= fb_wall_post(@otheruser,"This is another message") %>
336
+ # <% end %>
337
+ def fb_wall(&proc)
338
+ content = capture(&proc)
339
+ concat(content_tag("fb:wall",content,{}),proc.binding)
340
+ end
341
+
342
+ # Render an <fb:wallpost> tag
343
+ # TODO: Optionally takes a time parameter t = int The current time, which is displayed in epoch seconds.
344
+ def fb_wallpost(user,message)
345
+ content_tag("fb:wallpost",message,:uid=>cast_to_facebook_id(user))
346
+ end
347
+ alias_method :fb_wall_post, :fb_wallpost
348
+
349
+ # Render an <fb:error> tag
350
+ # If message and text are present then this will render fb:error and fb:message tag
351
+ # TODO: Optionally takes a decoration tag with value of 'no_padding' or 'shorten'
352
+ def fb_error(message, text=nil)
353
+ fb_status_msg("error", message, text)
354
+ end
355
+
356
+ # Render an <fb:explanation> tag
357
+ # If message and text are present then this will render fb:error and fb:message tag
358
+ # TODO: Optionally takes a decoration tag with value of 'no_padding' or 'shorten'
359
+ def fb_explanation(message, text=nil)
360
+ fb_status_msg("explanation", message, text)
361
+ end
362
+
363
+ # Render an <fb:success> tag
364
+ # If message and text are present then this will render fb:error and fb:message tag
365
+ # TODO: Optionally takes a decoration tag with value of 'no_padding' or 'shorten'
366
+ def fb_success(message, text=nil)
367
+ fb_status_msg("success", message, text)
368
+ end
369
+
370
+ # Render flash values as <fb:message> and <fb:error> tags
371
+ #
372
+ # values in flash[:notice] will be rendered as an <fb:message>
373
+ #
374
+ # values in flash[:error] will be rendered as an <fb:error>
375
+ # TODO: Allow flash[:info] to render fb_explanation
376
+ def facebook_messages
377
+ message=""
378
+ unless flash[:notice].blank?
379
+ message += fb_success(flash[:notice])
380
+ end
381
+ unless flash[:error].blank?
382
+ message += fb_error(flash[:error])
383
+ end
384
+ message
385
+ end
386
+
387
+ # Create a dashboard. It can contain fb_action, fb_help, and fb_create_button
388
+ #
389
+ # For Example:
390
+ # <% fb_dashboard do %>
391
+ # <%= APP_NAME %>
392
+ # <%= fb_action 'My Matches', search_path %>
393
+ # <%= fb_help 'Feedback', "http://www.facebook.com/apps/application.php?id=6236036681" %>
394
+ # <%= fb_create_button 'Invite Friends', main_path %>
395
+ # <% end %>
396
+ def fb_dashboard(&proc)
397
+ if block_given?
398
+ content = capture(&proc)
399
+ concat(content_tag("fb:dashboard",content,{}),proc.binding)
400
+ else
401
+ content_tag("fb:dashboard",content,{})
402
+ end
403
+ end
404
+
405
+ # Content for the wide profile box goes in this tag
406
+ def fb_wide(&proc)
407
+ content = capture(&proc)
408
+ concat(content_tag("fb:wide", content, {}), proc.binding)
409
+ end
410
+
411
+ # Content for the narrow profile box goes in this tag
412
+ def fb_narrow(&proc)
413
+ content = capture(&proc)
414
+ concat(content_tag("fb:narrow", content, {}), proc.binding)
415
+ end
416
+
417
+ # Renders an action using the <fb:action> tag
418
+ def fb_action(name, url)
419
+ "<fb:action href=\"#{url_for(url)}\">#{name}</fb:action>"
420
+ end
421
+
422
+ # Render a <fb:help> tag
423
+ # For use inside <fb:dashboard>
424
+ def fb_help(name, url)
425
+ "<fb:help href=\"#{url_for(url)}\">#{name}</fb:help>"
426
+ end
427
+
428
+ # Render a <fb:create-button> tag
429
+ # For use inside <fb:dashboard>
430
+ def fb_create_button(name, url)
431
+ "<fb:create-button href=\"#{url_for(url)}\">#{name}</fb:create-button>"
432
+ end
433
+
434
+ # Create a comment area
435
+ # All the data for this content area is stored on the facebook servers.
436
+ # <em>See:</em> http://wiki.developers.facebook.com/index.php/Fb:comments for full details
437
+ def fb_comments(xid,canpost=true,candelete=false,numposts=5,options={})
438
+ options = options.dup
439
+ title = (title = options.delete(:title)) ? fb_title(title) : nil
440
+ content_tag "fb:comments",title,stringify_vals(options.merge(:xid=>xid,:canpost=>canpost.to_s,:candelete=>candelete.to_s,:numposts=>numposts))
441
+ end
442
+
443
+ # Adds a title to the title bar
444
+ #
445
+ # Facebook | App Name | This is the canvas page window title
446
+ #
447
+ # +title+: This is the canvas page window
448
+ def fb_title(title)
449
+ "<fb:title>#{title}</fb:title>"
450
+ end
451
+
452
+ # Create a Google Analytics tag
453
+ #
454
+ # +uacct+: Your Urchin/Google Analytics account ID.
455
+ def fb_google_analytics(uacct, options={})
456
+ options = options.dup
457
+ tag "fb:google-analytics", stringify_vals(options.merge(:uacct => uacct))
458
+ end
459
+
460
+ # Render if-is-app-user tag
461
+ # This tag renders the enclosing content only if the user specified has accepted the terms of service for the application.
462
+ # Use fb_if_user_has_added_app to determine wether the user has added the app.
463
+ # Example:
464
+ # <% fb_if_is_app_user(@facebook_user) do %>
465
+ # Thanks for accepting our terms of service!
466
+ # <% fb_else do %>
467
+ # Hey you haven't agreed to our terms. <%= link_to("Please accept our terms of service.", :action => "terms_of_service") %>
468
+ # <% end %>
469
+ #<% end %>
470
+ def fb_if_is_app_user(user=nil,options={},&proc)
471
+ content = capture(&proc)
472
+ options = options.dup
473
+ options.merge!(:uid=>cast_to_facebook_id(user)) if user
474
+ concat(content_tag("fb:if-is-app-user",content,stringify_vals(options)),proc.binding)
475
+ end
476
+
477
+ # Render if-user-has-added-app tag
478
+ # This tag renders the enclosing content only if the user specified has installed the application
479
+ #
480
+ # Example:
481
+ # <% fb_if_user_has_added_app(@facebook_user) do %>
482
+ # Hey you are an app user!
483
+ # <% fb_else do %>
484
+ # Hey you aren't an app user. <%= link_to("Add App and see the other side.", :action => "added_app") %>
485
+ # <% end %>
486
+ #<% end %>
487
+ def fb_if_user_has_added_app(user,options={},&proc)
488
+ content = capture(&proc)
489
+ options = options.dup
490
+ concat(content_tag("fb:if-user-has-added-app",content,stringify_vals(options.merge(:uid=>cast_to_facebook_id(user)))),proc.binding)
491
+ end
492
+
493
+ # Render fb:if-is-user tag
494
+ # This tag only renders enclosing content if the user is one of those specified
495
+ # user can be a single user or an Array of users
496
+ # Example:
497
+ # <% fb_if_is_user(@check_user) do %>
498
+ # <%= fb_name(@facebook_user) %> are one of the users. <%= link_to("Check the other side", :action => "friend") %>
499
+ # <% fb_else do %>
500
+ # <%= fb_name(@facebook_user) %> are not one of the users <%= fb_name(@check_user) %>
501
+ # <%= link_to("Check the other side", :action => "you") %>
502
+ # <% end %>
503
+ # <% end %>
504
+ def fb_if_is_user(user,&proc)
505
+ content = capture(&proc)
506
+ user = [user] unless user.is_a? Array
507
+ user_list=user.map{|u| cast_to_facebook_id(u)}.join(",")
508
+ concat(content_tag("fb:if-is-user",content,{:uid=>user_list}),proc.binding)
509
+ end
510
+
511
+ # Render fb:else tag
512
+ # Must be used within if block such as fb_if_is_user or fb_if_is_app_user . See example in fb_if_is_app_user
513
+ def fb_else(&proc)
514
+ content = capture(&proc)
515
+ concat(content_tag("fb:else",content),proc.binding)
516
+ end
517
+
518
+ #
519
+ # Return the URL for the about page of the application
520
+ def fb_about_url
521
+ "http://#{Facebooker.www_server_base_url}/apps/application.php?api_key=#{Facebooker.api_key}"
522
+ end
523
+
524
+ #
525
+ # Embed a discussion board named xid on the current page
526
+ #
527
+ def fb_board(xid,options={})
528
+ options = options.dup
529
+ tag("fb:board",stringify_vals(options.merge(:xid=>xid)))
530
+ end
531
+
532
+ def fb_add_profile_section
533
+ tag "fb:add-section-button",:section=>"profile"
534
+ end
535
+
536
+ def fb_add_info_section
537
+ tag "fb:add-section-button",:section=>"info"
538
+ end
539
+
540
+ def fb_prompt_permission(permission,message,callback=nil)
541
+ raise(ArgumentError, "Unknown value for permission: #{permission}") unless VALID_PERMISSIONS.include?(permission.to_sym)
542
+ args={:perms=>permission}
543
+ args[:next_fbjs]=callback unless callback.nil?
544
+ content_tag("fb:prompt-permission",message,args)
545
+ end
546
+
547
+ protected
548
+
549
+ def cast_to_facebook_id(object)
550
+ Facebooker::User.cast_to_facebook_id(object)
551
+ end
552
+
553
+ def validate_fb_profile_pic_size(options)
554
+ if options.has_key?(:size) && !VALID_FB_PROFILE_PIC_SIZES.include?(options[:size].to_sym)
555
+ raise(ArgumentError, "Unkown value for size: #{options[:size]}")
556
+ end
557
+ end
558
+
559
+ def validate_fb_photo_size(options)
560
+ if options.has_key?(:size) && !VALID_FB_PHOTO_SIZES.include?(options[:size].to_sym)
561
+ raise(ArgumentError, "Unkown value for size: #{options[:size]}")
562
+ end
563
+ end
564
+
565
+ private
566
+ def stringify_vals(hash)
567
+ result={}
568
+ hash.each do |key,value|
569
+ result[key]=value.to_s
570
+ end
571
+ result
572
+ end
573
+
574
+ def fb_status_msg(type, message, text)
575
+ if text.blank?
576
+ tag("fb:#{type}", :message => message)
577
+ else
578
+ content_tag("fb:#{type}", content_tag("fb:message", message) + text)
579
+ end
580
+ end
581
+
582
+ def token_tag
583
+ unless protect_against_forgery?
584
+ ''
585
+ else
586
+ tag(:input, :type => "hidden", :name => request_forgery_protection_token.to_s, :value => form_authenticity_token)
587
+ end
588
+ end
589
+ end
590
+ end
591
+ end
592
+
593
+ class Hash
594
+ def transform_keys!(transformation_hash)
595
+ transformation_hash.each_pair{|key, value| transform_key!(key, value)}
596
+ end
597
+
598
+
599
+ def transform_key!(old_key, new_key)
600
+ swapkey!(new_key, old_key)
601
+ end
602
+
603
+ ### This method is lifted from Ruby Facets core
604
+ def swapkey!( newkey, oldkey )
605
+ self[newkey] = self.delete(oldkey) if self.has_key?(oldkey)
606
+ self
607
+ end
608
+
609
+ # We can allow css attributes.
610
+ FB_ALWAYS_VALID_OPTION_KEYS = [:class, :style]
611
+ def assert_valid_keys(*valid_keys)
612
+ unknown_keys = keys - [valid_keys + FB_ALWAYS_VALID_OPTION_KEYS].flatten
613
+ raise(ArgumentError, "Unknown key(s): #{unknown_keys.join(", ")}") unless unknown_keys.empty?
614
+ end
615
+ end