zerobearing2-facebooker 1.0.46

Sign up to get free protection for your applications and to get access to all the features.
Files changed (143) hide show
  1. data/.autotest +15 -0
  2. data/CHANGELOG.rdoc +24 -0
  3. data/COPYING.rdoc +19 -0
  4. data/Manifest.txt +139 -0
  5. data/README.rdoc +113 -0
  6. data/Rakefile +85 -0
  7. data/TODO.rdoc +4 -0
  8. data/examples/desktop_login.rb +14 -0
  9. data/facebooker.gemspec +42 -0
  10. data/generators/facebook/facebook_generator.rb +14 -0
  11. data/generators/facebook/templates/config/facebooker.yml +49 -0
  12. data/generators/facebook/templates/public/javascripts/facebooker.js +93 -0
  13. data/generators/facebook_controller/USAGE +33 -0
  14. data/generators/facebook_controller/facebook_controller_generator.rb +40 -0
  15. data/generators/facebook_controller/templates/controller.rb +7 -0
  16. data/generators/facebook_controller/templates/functional_test.rb +11 -0
  17. data/generators/facebook_controller/templates/helper.rb +2 -0
  18. data/generators/facebook_controller/templates/view.fbml.erb +2 -0
  19. data/generators/facebook_controller/templates/view.html.erb +2 -0
  20. data/generators/facebook_publisher/facebook_publisher_generator.rb +14 -0
  21. data/generators/facebook_publisher/templates/create_facebook_templates.rb +15 -0
  22. data/generators/facebook_publisher/templates/publisher.rb +3 -0
  23. data/generators/facebook_scaffold/USAGE +27 -0
  24. data/generators/facebook_scaffold/facebook_scaffold_generator.rb +118 -0
  25. data/generators/facebook_scaffold/templates/controller.rb +93 -0
  26. data/generators/facebook_scaffold/templates/facebook_style.css +2579 -0
  27. data/generators/facebook_scaffold/templates/functional_test.rb +89 -0
  28. data/generators/facebook_scaffold/templates/helper.rb +2 -0
  29. data/generators/facebook_scaffold/templates/layout.fbml.erb +6 -0
  30. data/generators/facebook_scaffold/templates/layout.html.erb +17 -0
  31. data/generators/facebook_scaffold/templates/style.css +74 -0
  32. data/generators/facebook_scaffold/templates/view_edit.fbml.erb +13 -0
  33. data/generators/facebook_scaffold/templates/view_edit.html.erb +18 -0
  34. data/generators/facebook_scaffold/templates/view_index.fbml.erb +24 -0
  35. data/generators/facebook_scaffold/templates/view_index.html.erb +24 -0
  36. data/generators/facebook_scaffold/templates/view_new.fbml.erb +12 -0
  37. data/generators/facebook_scaffold/templates/view_new.html.erb +17 -0
  38. data/generators/facebook_scaffold/templates/view_show.fbml.erb +10 -0
  39. data/generators/facebook_scaffold/templates/view_show.html.erb +10 -0
  40. data/generators/publisher/publisher_generator.rb +14 -0
  41. data/generators/xd_receiver/templates/xd_receiver.html +10 -0
  42. data/generators/xd_receiver/xd_receiver_generator.rb +10 -0
  43. data/init.rb +26 -0
  44. data/install.rb +12 -0
  45. data/lib/facebooker.rb +233 -0
  46. data/lib/facebooker/adapters/adapter_base.rb +91 -0
  47. data/lib/facebooker/adapters/bebo_adapter.rb +77 -0
  48. data/lib/facebooker/adapters/facebook_adapter.rb +52 -0
  49. data/lib/facebooker/admin.rb +42 -0
  50. data/lib/facebooker/batch_request.rb +45 -0
  51. data/lib/facebooker/data.rb +57 -0
  52. data/lib/facebooker/feed.rb +78 -0
  53. data/lib/facebooker/logging.rb +44 -0
  54. data/lib/facebooker/mobile.rb +20 -0
  55. data/lib/facebooker/mock/service.rb +50 -0
  56. data/lib/facebooker/mock/session.rb +18 -0
  57. data/lib/facebooker/model.rb +139 -0
  58. data/lib/facebooker/models/affiliation.rb +10 -0
  59. data/lib/facebooker/models/album.rb +11 -0
  60. data/lib/facebooker/models/applicationproperties.rb +39 -0
  61. data/lib/facebooker/models/applicationrestrictions.rb +10 -0
  62. data/lib/facebooker/models/cookie.rb +10 -0
  63. data/lib/facebooker/models/education_info.rb +11 -0
  64. data/lib/facebooker/models/event.rb +28 -0
  65. data/lib/facebooker/models/friend_list.rb +16 -0
  66. data/lib/facebooker/models/group.rb +36 -0
  67. data/lib/facebooker/models/info_item.rb +10 -0
  68. data/lib/facebooker/models/info_section.rb +10 -0
  69. data/lib/facebooker/models/location.rb +8 -0
  70. data/lib/facebooker/models/notifications.rb +17 -0
  71. data/lib/facebooker/models/page.rb +44 -0
  72. data/lib/facebooker/models/photo.rb +19 -0
  73. data/lib/facebooker/models/tag.rb +12 -0
  74. data/lib/facebooker/models/user.rb +516 -0
  75. data/lib/facebooker/models/video.rb +9 -0
  76. data/lib/facebooker/models/work_info.rb +10 -0
  77. data/lib/facebooker/parser.rb +675 -0
  78. data/lib/facebooker/rails/backwards_compatible_param_checks.rb +31 -0
  79. data/lib/facebooker/rails/controller.rb +355 -0
  80. data/lib/facebooker/rails/cucumber.rb +28 -0
  81. data/lib/facebooker/rails/cucumber/world.rb +40 -0
  82. data/lib/facebooker/rails/extensions/action_controller.rb +48 -0
  83. data/lib/facebooker/rails/extensions/rack_setup.rb +6 -0
  84. data/lib/facebooker/rails/extensions/routing.rb +15 -0
  85. data/lib/facebooker/rails/facebook_form_builder.rb +112 -0
  86. data/lib/facebooker/rails/facebook_pretty_errors.rb +22 -0
  87. data/lib/facebooker/rails/facebook_request_fix.rb +28 -0
  88. data/lib/facebooker/rails/facebook_request_fix_2-3.rb +31 -0
  89. data/lib/facebooker/rails/facebook_session_handling.rb +68 -0
  90. data/lib/facebooker/rails/facebook_url_helper.rb +192 -0
  91. data/lib/facebooker/rails/facebook_url_rewriting.rb +60 -0
  92. data/lib/facebooker/rails/helpers.rb +814 -0
  93. data/lib/facebooker/rails/helpers/fb_connect.rb +119 -0
  94. data/lib/facebooker/rails/integration_session.rb +38 -0
  95. data/lib/facebooker/rails/profile_publisher_extensions.rb +42 -0
  96. data/lib/facebooker/rails/publisher.rb +550 -0
  97. data/lib/facebooker/rails/routing.rb +49 -0
  98. data/lib/facebooker/rails/test_helpers.rb +68 -0
  99. data/lib/facebooker/rails/utilities.rb +22 -0
  100. data/lib/facebooker/server_cache.rb +24 -0
  101. data/lib/facebooker/service.rb +102 -0
  102. data/lib/facebooker/service/base_service.rb +18 -0
  103. data/lib/facebooker/service/curl_service.rb +44 -0
  104. data/lib/facebooker/service/net_http_service.rb +12 -0
  105. data/lib/facebooker/service/typhoeus_multi_service.rb +27 -0
  106. data/lib/facebooker/service/typhoeus_service.rb +17 -0
  107. data/lib/facebooker/session.rb +651 -0
  108. data/lib/facebooker/version.rb +9 -0
  109. data/lib/net/http_multipart_post.rb +123 -0
  110. data/lib/rack/facebook.rb +89 -0
  111. data/lib/tasks/facebooker.rake +18 -0
  112. data/lib/tasks/tunnel.rake +46 -0
  113. data/rails/init.rb +1 -0
  114. data/setup.rb +1585 -0
  115. data/templates/layout.erb +24 -0
  116. data/test/facebooker/adapters_test.rb +191 -0
  117. data/test/facebooker/admin_test.rb +102 -0
  118. data/test/facebooker/batch_request_test.rb +83 -0
  119. data/test/facebooker/data_test.rb +86 -0
  120. data/test/facebooker/logging_test.rb +43 -0
  121. data/test/facebooker/mobile_test.rb +45 -0
  122. data/test/facebooker/model_test.rb +133 -0
  123. data/test/facebooker/models/event_test.rb +15 -0
  124. data/test/facebooker/models/page_test.rb +46 -0
  125. data/test/facebooker/models/photo_test.rb +16 -0
  126. data/test/facebooker/models/user_test.rb +366 -0
  127. data/test/facebooker/rails/facebook_request_fix_2-3_test.rb +25 -0
  128. data/test/facebooker/rails/facebook_url_rewriting_test.rb +39 -0
  129. data/test/facebooker/rails/integration_session_test.rb +10 -0
  130. data/test/facebooker/rails/publisher_test.rb +482 -0
  131. data/test/facebooker/rails_integration_test.rb +1459 -0
  132. data/test/facebooker/server_cache_test.rb +44 -0
  133. data/test/facebooker/service_test.rb +58 -0
  134. data/test/facebooker/session_test.rb +669 -0
  135. data/test/facebooker_test.rb +951 -0
  136. data/test/fixtures/multipart_post_body_with_only_parameters.txt +33 -0
  137. data/test/fixtures/multipart_post_body_with_single_file.txt +38 -0
  138. data/test/fixtures/multipart_post_body_with_single_file_that_has_nil_key.txt +38 -0
  139. data/test/net/http_multipart_post_test.rb +52 -0
  140. data/test/rack/facebook_test.rb +73 -0
  141. data/test/rails_test_helper.rb +27 -0
  142. data/test/test_helper.rb +74 -0
  143. metadata +244 -0
@@ -0,0 +1,60 @@
1
+ module ::ActionController
2
+ if Rails.version < '2.3'
3
+ class AbstractRequest
4
+ def relative_url_root
5
+ Facebooker.path_prefix
6
+ end
7
+ end
8
+ else
9
+ class Request
10
+ def relative_url_root
11
+ Facebooker.path_prefix
12
+ end
13
+ end
14
+ end
15
+
16
+ class Base
17
+ class << self
18
+ alias :old_relative_url_root :relative_url_root
19
+ def relative_url_root
20
+ Facebooker.path_prefix
21
+ end
22
+ end
23
+ end
24
+
25
+ class UrlRewriter
26
+ include Facebooker::Rails::BackwardsCompatibleParamChecks
27
+
28
+ RESERVED_OPTIONS << :canvas
29
+
30
+ def link_to_new_canvas?
31
+ one_or_true @request.parameters["fb_sig_in_new_facebook"]
32
+ end
33
+
34
+ def link_to_canvas?(params, options)
35
+ option_override = options[:canvas]
36
+ return false if option_override == false # important to check for false. nil should use default behavior
37
+ option_override || (can_safely_access_request_parameters? && (one_or_true(@request.parameters["fb_sig_in_canvas"]) || one_or_true(@request.parameters[:fb_sig_in_canvas]) ))
38
+ end
39
+
40
+ #rails blindly tries to merge things that may be nil into the parameters. Make sure this won't break
41
+ def can_safely_access_request_parameters?
42
+ @request.request_parameters
43
+ end
44
+
45
+ def rewrite_url_with_facebooker(*args)
46
+ options = args.first.is_a?(Hash) ? args.first : args.last
47
+ is_link_to_canvas = link_to_canvas?(@request.request_parameters, options)
48
+ if is_link_to_canvas && !options.has_key?(:host)
49
+ options[:host] = Facebooker.canvas_server_base
50
+ end
51
+ options.delete(:canvas)
52
+ Facebooker.request_for_canvas(is_link_to_canvas) do
53
+ rewrite_url_without_facebooker(*args)
54
+ end
55
+ end
56
+
57
+ alias_method_chain :rewrite_url, :facebooker
58
+
59
+ end
60
+ end
@@ -0,0 +1,814 @@
1
+ require 'action_pack'
2
+
3
+ module Facebooker
4
+ module Rails
5
+
6
+ # Facebook specific helpers for creating FBML
7
+ #
8
+ # All helpers that take a user as a parameter will get the Facebook UID from the facebook_id attribute if it exists.
9
+ # It will use to_s if the facebook_id attribute is not present.
10
+ #
11
+ module Helpers
12
+
13
+ include Facebooker::Rails::Helpers::FbConnect
14
+
15
+ # Create an fb:dialog
16
+ # id must be a unique name e.g. "my_dialog"
17
+ # cancel_button is true or false
18
+ def fb_dialog( id, cancel_button, &block )
19
+ content = capture(&block)
20
+ if ignore_binding?
21
+ concat( content_tag("fb:dialog", content, {:id => id, :cancel_button => cancel_button}) )
22
+ else
23
+ concat( content_tag("fb:dialog", content, {:id => id, :cancel_button => cancel_button}), block.binding )
24
+ end
25
+ end
26
+
27
+ def fb_iframe(src, options = {})
28
+ content_tag "fb:iframe", '', options.merge({ :src => src })
29
+ end
30
+
31
+ def fb_swf(src, options = {})
32
+ tag "fb:swf", options.merge(:swfsrc => src)
33
+ end
34
+
35
+ def fb_dialog_title( title )
36
+ content_tag "fb:dialog-title", title
37
+ end
38
+
39
+ def fb_dialog_content( &block )
40
+ content = capture(&block)
41
+ if ignore_binding?
42
+ concat( content_tag("fb:dialog-content", content) )
43
+ else
44
+ concat( content_tag("fb:dialog-content", content), block.binding )
45
+ end
46
+ end
47
+
48
+ def fb_dialog_button( type, value, options={} )
49
+ options.assert_valid_keys FB_DIALOG_BUTTON_VALID_OPTION_KEYS
50
+ options.merge! :type => type, :value => value
51
+ tag "fb:dialog-button", options
52
+ end
53
+
54
+ FB_DIALOG_BUTTON_VALID_OPTION_KEYS = [:close_dialog, :href, :form_id, :clickrewriteurl, :clickrewriteid, :clickrewriteform]
55
+
56
+ def fb_show_feed_dialog(action, user_message = "", prompt = "", callback = nil)
57
+ update_page do |page|
58
+ page.call "Facebook.showFeedDialog",action.template_id,action.data,action.body_general,action.target_ids,callback,prompt,user_message
59
+ end
60
+ end
61
+
62
+
63
+ # Create an fb:request-form without a selector
64
+ #
65
+ # The block passed to this tag is used as the content of the form
66
+ #
67
+ # The message param is the name sent to content_for that specifies the body of the message
68
+ #
69
+ # For example,
70
+ #
71
+ # <% content_for("invite_message") do %>
72
+ # This gets sent in the invite. <%= fb_req_choice("with a button!",new_poke_path) %>
73
+ # <% end %>
74
+ # <% fb_request_form("Poke","invite_message",create_poke_path) do %>
75
+ # Send a poke to: <%= fb_friend_selector %> <br />
76
+ # <%= fb_request_form_submit %>
77
+ # <% end %>
78
+ def fb_request_form(type,message_param,url,options={},&block)
79
+ content = capture(&block)
80
+ message = @template.instance_variable_get("@content_for_#{message_param}")
81
+ if ignore_binding?
82
+ concat(content_tag("fb:request-form", content + token_tag,
83
+ {:action=>url,:method=>"post",:invite=>true,:type=>type,:content=>message}.merge(options)))
84
+ else
85
+ concat(content_tag("fb:request-form", content + token_tag,
86
+ {:action=>url,:method=>"post",:invite=>true,:type=>type,:content=>message}.merge(options)),
87
+ block.binding)
88
+ end
89
+ end
90
+
91
+ # Create a submit button for an <fb:request-form>
92
+ # If the request is for an individual user you can optionally
93
+ # Provide the user and a label for the request button.
94
+ # For example
95
+ # <% content_for("invite_user") do %>
96
+ # This gets sent in the invite. <%= fb_req_choice("Come join us!",new_invite_path) %>
97
+ # <% end %>
98
+ # <% fb_request_form("Invite","invite_user",create_invite_path) do %>
99
+ # Invite <%= fb_name(@facebook_user.friends.first.id)%> to the party <br />
100
+ # <%= fb_request_form_submit(:uid => @facebook_user.friends.first.id, :label => "Invite %n") %>
101
+ # <% end %>
102
+ # <em>See:</em> http://wiki.developers.facebook.com/index.php/Fb:request-form-submit for options
103
+ def fb_request_form_submit(options={})
104
+ tag("fb:request-form-submit",stringify_vals(options))
105
+ end
106
+
107
+
108
+ # Create an fb:request-form with an fb_multi_friend_selector inside
109
+ #
110
+ # The content of the block are used as the message on the form,
111
+ #
112
+ # For example:
113
+ # <% fb_multi_friend_request("Poke","Choose some friends to Poke",create_poke_path) do %>
114
+ # If you select some friends, they will see this message.
115
+ # <%= fb_req_choice("They will get this button, too",new_poke_path) %>
116
+ # <% end %>
117
+ def fb_multi_friend_request(type,friend_selector_message,url,&block)
118
+ content = capture(&block)
119
+ if ignore_binding?
120
+ concat(content_tag("fb:request-form",
121
+ fb_multi_friend_selector(friend_selector_message) + token_tag,
122
+ {:action=>url,:method=>"post",:invite=>true,:type=>type,:content=>content}
123
+ ))
124
+ else
125
+ concat(content_tag("fb:request-form",
126
+ fb_multi_friend_selector(friend_selector_message) + token_tag,
127
+ {:action=>url,:method=>"post",:invite=>true,:type=>type,:content=>content}
128
+ ),
129
+ block.binding)
130
+ end
131
+ end
132
+
133
+ # Render an <fb:friend-selector> element
134
+ # <em>See:</em> http://wiki.developers.facebook.com/index.php/Fb:friend-selector for options
135
+ #
136
+ def fb_friend_selector(options={})
137
+ tag("fb:friend-selector",stringify_vals(options))
138
+ end
139
+
140
+ # Render an <fb:multi-friend-input> element
141
+ # <em> See: </em> http://wiki.developers.facebook.com/index.php/Fb:multi-friend-input for options
142
+ def fb_multi_friend_input(options={})
143
+ tag "fb:multi-friend-input",stringify_vals(options)
144
+ end
145
+
146
+ # Render an <fb:multi-friend-selector> with the passed in welcome message
147
+ # Full version shows all profile pics for friends.
148
+ # <em> See: </em> http://wiki.developers.facebook.com/index.php/Fb:multi-friend-selector for options
149
+ # <em> Note: </em> I don't think the block is used here.
150
+ def fb_multi_friend_selector(message,options={},&block)
151
+ options = options.dup
152
+ tag("fb:multi-friend-selector",stringify_vals({:showborder=>false,:actiontext=>message,:max=>20}.merge(options)))
153
+ end
154
+
155
+ # Render a condensed <fb:multi-friend-selector> with the passed in welcome message
156
+ # Condensed version show checkboxes for each friend.
157
+ # <em> See: </em> http://wiki.developers.facebook.com/index.php/Fb:multi-friend-selector_%28condensed%29 for options
158
+ # <em> Note: </em> I don't think the block is used here.
159
+ def fb_multi_friend_selector_condensed(options={},&block)
160
+ options = options.dup
161
+ tag("fb:multi-friend-selector",stringify_vals(options.merge(:condensed=>true)))
162
+ end
163
+
164
+ # Render a button in a request using the <fb:req-choice> tag
165
+ # url must be an absolute url
166
+ # This should be used inside the block of an fb_multi_friend_request
167
+ def fb_req_choice(label,url)
168
+ tag "fb:req-choice",:label=>label,:url=>url
169
+ end
170
+
171
+ # Create a facebook form using <fb:editor>
172
+ #
173
+ # It yields a form builder that will convert the standard rails form helpers
174
+ # into the facebook specific version.
175
+ #
176
+ # Example:
177
+ # <% facebook_form_for(:poke,@poke,:url => create_poke_path) do |f| %>
178
+ # <%= f.text_field :message, :label=>"message" %>
179
+ # <%= f.buttons "Save Poke" %>
180
+ # <% end %>
181
+ #
182
+ # will generate
183
+ #
184
+ # <fb:editor action="/pokes/create">
185
+ # <fb:editor-text name="poke[message]" id="poke_message" value="" label="message" />
186
+ # <fb:editor-buttonset>
187
+ # <fb:editor-button label="Save Poke"
188
+ # </fb:editor-buttonset>
189
+ # </fb:editor>
190
+ def facebook_form_for( record_or_name_or_array,*args, &proc)
191
+
192
+ raise ArgumentError, "Missing block" unless block_given?
193
+ options = args.last.is_a?(Hash) ? args.pop : {}
194
+
195
+ case record_or_name_or_array
196
+ when String, Symbol
197
+ object_name = record_or_name_or_array
198
+ when Array
199
+ object = record_or_name_or_array.last
200
+ object_name = ActionController::RecordIdentifier.singular_class_name(object)
201
+ apply_form_for_options!(record_or_name_or_array, options)
202
+ args.unshift object
203
+ else
204
+ object = record_or_name_or_array
205
+ object_name = ActionController::RecordIdentifier.singular_class_name(object)
206
+ apply_form_for_options!([object], options)
207
+ args.unshift object
208
+ end
209
+ method = (options[:html]||{})[:method]
210
+ options[:builder] ||= Facebooker::Rails::FacebookFormBuilder
211
+ editor_options={}
212
+
213
+ action=options.delete(:url)
214
+ editor_options[:action]= action unless action.blank?
215
+ width=options.delete(:width)
216
+ editor_options[:width]=width unless width.blank?
217
+ width=options.delete(:labelwidth)
218
+ editor_options[:labelwidth]=width unless width.blank?
219
+
220
+ if ignore_binding?
221
+ concat(tag("fb:editor",editor_options,true))
222
+ concat(tag(:input,{:type=>"hidden",:name=>:_method, :value=>method},false)) unless method.blank?
223
+ concat(token_tag)
224
+ fields_for( object_name,*(args << options), &proc)
225
+ concat("</fb:editor>")
226
+ else
227
+ concat(tag("fb:editor",editor_options,true) , proc.binding)
228
+ concat(tag(:input,{:type=>"hidden",:name=>:_method, :value=>method},false), proc.binding) unless method.blank?
229
+ concat(token_tag, proc.binding)
230
+ fields_for( object_name,*(args << options), &proc)
231
+ concat("</fb:editor>",proc.binding)
232
+ end
233
+ end
234
+
235
+ # Render an fb:application-name tag
236
+ #
237
+ # This renders the current application name via fbml. See
238
+ # http://wiki.developers.facebook.com/index.php/Fb:application-name
239
+ # for a full description.
240
+ #
241
+ def fb_application_name(options={})
242
+ tag "fb:application-name", stringify_vals(options)
243
+ end
244
+
245
+ # Render an fb:name tag for the given user
246
+ # This renders the name of the user specified. You can use this tag as both subject and object of
247
+ # a sentence. <em> See </em> http://wiki.developers.facebook.com/index.php/Fb:name for full description.
248
+ # Use this tag on FBML pages instead of retrieving the user's info and rendering the name explicitly.
249
+ #
250
+ def fb_name(user, options={})
251
+ options = options.dup
252
+ options.transform_keys!(FB_NAME_OPTION_KEYS_TO_TRANSFORM)
253
+ options.assert_valid_keys(FB_NAME_VALID_OPTION_KEYS)
254
+ options.merge!(:uid => cast_to_facebook_id(user))
255
+ content_tag("fb:name",nil, stringify_vals(options))
256
+ end
257
+
258
+ FB_NAME_OPTION_KEYS_TO_TRANSFORM = {:first_name_only => :firstnameonly,
259
+ :last_name_only => :lastnameonly,
260
+ :show_network => :shownetwork,
261
+ :use_you => :useyou,
262
+ :if_cant_see => :ifcantsee,
263
+ :subject_id => :subjectid}
264
+ FB_NAME_VALID_OPTION_KEYS = [:firstnameonly, :linked, :lastnameonly, :possessive, :reflexive,
265
+ :shownetwork, :useyou, :ifcantsee, :capitalize, :subjectid]
266
+
267
+ # Render an <fb:pronoun> tag for the specified user
268
+ # Options give flexibility for placing in any part of a sentence.
269
+ # <em> See </em> http://wiki.developers.facebook.com/index.php/Fb:pronoun for complete list of options.
270
+ #
271
+ def fb_pronoun(user, options={})
272
+ options = options.dup
273
+ options.transform_keys!(FB_PRONOUN_OPTION_KEYS_TO_TRANSFORM)
274
+ options.assert_valid_keys(FB_PRONOUN_VALID_OPTION_KEYS)
275
+ options.merge!(:uid => cast_to_facebook_id(user))
276
+ content_tag("fb:pronoun",nil, stringify_vals(options))
277
+ end
278
+
279
+ FB_PRONOUN_OPTION_KEYS_TO_TRANSFORM = {:use_you => :useyou, :use_they => :usethey}
280
+ FB_PRONOUN_VALID_OPTION_KEYS = [:useyou, :possessive, :reflexive, :objective,
281
+ :usethey, :capitalize]
282
+
283
+ # Render an fb:ref tag.
284
+ # Options must contain either url or handle.
285
+ # * <em> url </em> The URL from which to fetch the FBML. You may need to call fbml.refreshRefUrl to refresh cache
286
+ # * <em> handle </em> The string previously set by fbml.setRefHandle that identifies the FBML
287
+ # <em> See </em> http://wiki.developers.facebook.com/index.php/Fb:ref for complete description
288
+ def fb_ref(options)
289
+ options.assert_valid_keys(FB_REF_VALID_OPTION_KEYS)
290
+ validate_fb_ref_has_either_url_or_handle(options)
291
+ validate_fb_ref_does_not_have_both_url_and_handle(options)
292
+ tag("fb:ref", stringify_vals(options))
293
+ end
294
+
295
+ def validate_fb_ref_has_either_url_or_handle(options)
296
+ unless options.has_key?(:url) || options.has_key?(:handle)
297
+ raise ArgumentError, "fb_ref requires :url or :handle"
298
+ end
299
+ end
300
+
301
+ def validate_fb_ref_does_not_have_both_url_and_handle(options)
302
+ if options.has_key?(:url) && options.has_key?(:handle)
303
+ raise ArgumentError, "fb_ref may not have both :url and :handle"
304
+ end
305
+ end
306
+
307
+ FB_REF_VALID_OPTION_KEYS = [:url, :handle]
308
+
309
+ # Render an <fb:profile-pic> for the specified user.
310
+ #
311
+ # You can optionally specify the size using the :size=> option. Valid
312
+ # sizes are :thumb, :small, :normal and :square. Or, you can specify
313
+ # width and height settings instead, just like an img tag.
314
+ #
315
+ # You can optionally specify whether or not to include the facebook icon
316
+ # overlay using the :facebook_logo => true option. Default is false.
317
+ #
318
+ def fb_profile_pic(user, options={})
319
+ options = options.dup
320
+ validate_fb_profile_pic_size(options)
321
+ options.transform_keys!(FB_PROFILE_PIC_OPTION_KEYS_TO_TRANSFORM)
322
+ options.assert_valid_keys(FB_PROFILE_PIC_VALID_OPTION_KEYS)
323
+ options.merge!(:uid => cast_to_facebook_id(user))
324
+ content_tag("fb:profile-pic", nil,stringify_vals(options))
325
+ end
326
+
327
+ FB_PROFILE_PIC_OPTION_KEYS_TO_TRANSFORM = {:facebook_logo => 'facebook-logo'}
328
+ FB_PROFILE_PIC_VALID_OPTION_KEYS = [:size, :linked, 'facebook-logo', :width, :height]
329
+
330
+
331
+ # Render an fb:photo tag.
332
+ # photo is either a Facebooker::Photo or an id of a Facebook photo or an object that responds to photo_id.
333
+ # <em> See: </em> http://wiki.developers.facebook.com/index.php/Fb:photo for complete list of options.
334
+ def fb_photo(photo, options={})
335
+ options = options.dup
336
+ options.assert_valid_keys(FB_PHOTO_VALID_OPTION_KEYS)
337
+ options.merge!(:pid => cast_to_photo_id(photo))
338
+ validate_fb_photo_size(options)
339
+ validate_fb_photo_align_value(options)
340
+ content_tag("fb:photo",nil, stringify_vals(options))
341
+ end
342
+
343
+ FB_PHOTO_VALID_OPTION_KEYS = [:uid, :size, :align]
344
+
345
+ def cast_to_photo_id(object)
346
+ object.respond_to?(:photo_id) ? object.photo_id : object
347
+ end
348
+
349
+ VALID_FB_SHARED_PHOTO_SIZES = [:thumb, :small, :normal, :square]
350
+ VALID_FB_PHOTO_SIZES = VALID_FB_SHARED_PHOTO_SIZES
351
+ VALID_FB_PROFILE_PIC_SIZES = VALID_FB_SHARED_PHOTO_SIZES
352
+ VALID_PERMISSIONS=[:email, :offline_access, :status_update, :photo_upload, :create_listing, :create_event, :rsvp_event, :sms, :video_upload,
353
+ :publish_stream, :read_stream]
354
+
355
+ # Render an fb:tabs tag containing some number of fb:tab_item tags.
356
+ # Example:
357
+ # <% fb_tabs do %>
358
+ # <%= fb_tab_item("Home", "home") %>
359
+ # <%= fb_tab_item("Office", "office") %>
360
+ # <% end %>
361
+ def fb_tabs(&block)
362
+ content = capture(&block)
363
+ if ignore_binding?
364
+ concat(content_tag("fb:tabs", content))
365
+ else
366
+ concat(content_tag("fb:tabs", content), block.binding)
367
+ end
368
+ end
369
+
370
+ # Render an fb:tab_item tag.
371
+ # Use this in conjunction with fb_tabs
372
+ # Options can contains :selected => true to indicate that a tab is the current tab.
373
+ # <em> See: </em> http://wiki.developers.facebook.com/index.php/Fb:tab-item for complete list of options
374
+ def fb_tab_item(title, url, options={})
375
+ options= options.dup
376
+ options.assert_valid_keys(FB_TAB_ITEM_VALID_OPTION_KEYS)
377
+ options.merge!(:title => title, :href => url)
378
+ validate_fb_tab_item_align_value(options)
379
+ tag("fb:tab-item", stringify_vals(options))
380
+ end
381
+
382
+ FB_TAB_ITEM_VALID_OPTION_KEYS = [:align, :selected]
383
+
384
+ def validate_fb_tab_item_align_value(options)
385
+ if options.has_key?(:align) && !VALID_FB_TAB_ITEM_ALIGN_VALUES.include?(options[:align].to_sym)
386
+ raise(ArgumentError, "Unknown value for align: #{options[:align]}")
387
+ end
388
+ end
389
+
390
+ def validate_fb_photo_align_value(options)
391
+ if options.has_key?(:align) && !VALID_FB_PHOTO_ALIGN_VALUES.include?(options[:align].to_sym)
392
+ raise(ArgumentError, "Unknown value for align: #{options[:align]}")
393
+ end
394
+ end
395
+
396
+ VALID_FB_SHARED_ALIGN_VALUES = [:left, :right]
397
+ VALID_FB_PHOTO_ALIGN_VALUES = VALID_FB_SHARED_ALIGN_VALUES
398
+ VALID_FB_TAB_ITEM_ALIGN_VALUES = VALID_FB_SHARED_ALIGN_VALUES
399
+
400
+
401
+ # Create a Facebook wall. It can contain fb_wall_posts
402
+ #
403
+ # For Example:
404
+ # <% fb_wall do %>
405
+ # <%= fb_wall_post(@user,"This is my message") %>
406
+ # <%= fb_wall_post(@otheruser,"This is another message") %>
407
+ # <% end %>
408
+ def fb_wall(&proc)
409
+ content = capture(&proc)
410
+ if ignore_binding?
411
+ concat(content_tag("fb:wall",content,{}))
412
+ else
413
+ concat(content_tag("fb:wall",content,{}),proc.binding)
414
+ end
415
+ end
416
+
417
+ # Render an <fb:wallpost> tag
418
+ # TODO: Optionally takes a time parameter t = int The current time, which is displayed in epoch seconds.
419
+ def fb_wallpost(user,message)
420
+ content_tag("fb:wallpost",message,:uid=>cast_to_facebook_id(user))
421
+ end
422
+ alias_method :fb_wall_post, :fb_wallpost
423
+
424
+ # Render an <fb:error> tag
425
+ # If message and text are present then this will render fb:error and fb:message tag
426
+ # TODO: Optionally takes a decoration tag with value of 'no_padding' or 'shorten'
427
+ def fb_error(message, text=nil)
428
+ fb_status_msg("error", message, text)
429
+ end
430
+
431
+ # Render an <fb:explanation> tag
432
+ # If message and text are present then this will render fb:error and fb:message tag
433
+ # TODO: Optionally takes a decoration tag with value of 'no_padding' or 'shorten'
434
+ def fb_explanation(message, text=nil)
435
+ fb_status_msg("explanation", message, text)
436
+ end
437
+
438
+ # Render an <fb:success> tag
439
+ # If message and text are present then this will render fb:error and fb:message tag
440
+ # TODO: Optionally takes a decoration tag with value of 'no_padding' or 'shorten'
441
+ def fb_success(message, text=nil)
442
+ fb_status_msg("success", message, text)
443
+ end
444
+
445
+ # Render flash values as <fb:message> and <fb:error> tags
446
+ #
447
+ # values in flash[:notice] will be rendered as an <fb:message>
448
+ #
449
+ # values in flash[:error] will be rendered as an <fb:error>
450
+ # TODO: Allow flash[:info] to render fb_explanation
451
+ def facebook_messages
452
+ message=""
453
+ unless flash[:notice].blank?
454
+ message += fb_success(flash[:notice])
455
+ end
456
+ unless flash[:error].blank?
457
+ message += fb_error(flash[:error])
458
+ end
459
+ message
460
+ end
461
+
462
+ # Create a dashboard. It can contain fb_action, fb_help, and fb_create_button
463
+ #
464
+ # For Example:
465
+ # <% fb_dashboard do %>
466
+ # <%= APP_NAME %>
467
+ # <%= fb_action 'My Matches', search_path %>
468
+ # <%= fb_help 'Feedback', "http://www.facebook.com/apps/application.php?id=6236036681" %>
469
+ # <%= fb_create_button 'Invite Friends', main_path %>
470
+ # <% end %>
471
+ def fb_dashboard(&proc)
472
+ if block_given?
473
+ content = capture(&proc)
474
+ if ignore_binding?
475
+ concat(content_tag("fb:dashboard",content,{}))
476
+ else
477
+ concat(content_tag("fb:dashboard",content,{}),proc.binding)
478
+ end
479
+ else
480
+ content_tag("fb:dashboard",content,{})
481
+ end
482
+ end
483
+
484
+ # Content for the wide profile box goes in this tag
485
+ def fb_wide(&proc)
486
+ content = capture(&proc)
487
+ if ignore_binding?
488
+ concat(content_tag("fb:wide", content, {}))
489
+ else
490
+ concat(content_tag("fb:wide", content, {}), proc.binding)
491
+ end
492
+ end
493
+
494
+ # Content for the narrow profile box goes in this tag
495
+ def fb_narrow(&proc)
496
+ content = capture(&proc)
497
+ if ignore_binding?
498
+ concat(content_tag("fb:narrow", content, {}))
499
+ else
500
+ concat(content_tag("fb:narrow", content, {}), proc.binding)
501
+ end
502
+ end
503
+
504
+ # Renders an action using the <fb:action> tag
505
+ def fb_action(name, url)
506
+ "<fb:action href=\"#{url_for(url)}\">#{name}</fb:action>"
507
+ end
508
+
509
+ # Render a <fb:help> tag
510
+ # For use inside <fb:dashboard>
511
+ def fb_help(name, url)
512
+ "<fb:help href=\"#{url_for(url)}\">#{name}</fb:help>"
513
+ end
514
+
515
+ # Render a <fb:create-button> tag
516
+ # For use inside <fb:dashboard>
517
+ def fb_create_button(name, url)
518
+ "<fb:create-button href=\"#{url_for(url)}\">#{name}</fb:create-button>"
519
+ end
520
+
521
+ # Create a comment area
522
+ # All the data for this content area is stored on the facebook servers.
523
+ # <em>See:</em> http://wiki.developers.facebook.com/index.php/Fb:comments for full details
524
+ def fb_comments(xid,canpost=true,candelete=false,numposts=5,options={})
525
+ options = options.dup
526
+ title = (title = options.delete(:title)) ? fb_title(title) : nil
527
+ content_tag "fb:comments",title,stringify_vals(options.merge(:xid=>xid,:canpost=>canpost.to_s,:candelete=>candelete.to_s,:numposts=>numposts))
528
+ end
529
+
530
+ # Adds a title to the title bar
531
+ #
532
+ # Facebook | App Name | This is the canvas page window title
533
+ #
534
+ # +title+: This is the canvas page window
535
+ def fb_title(title)
536
+ "<fb:title>#{title}</fb:title>"
537
+ end
538
+
539
+ # Create a Google Analytics tag
540
+ #
541
+ # +uacct+: Your Urchin/Google Analytics account ID.
542
+ def fb_google_analytics(uacct, options={})
543
+ options = options.dup
544
+ tag "fb:google-analytics", stringify_vals(options.merge(:uacct => uacct))
545
+ end
546
+
547
+ # Render if-is-app-user tag
548
+ # This tag renders the enclosing content only if the user specified has accepted the terms of service for the application.
549
+ # Use fb_if_user_has_added_app to determine wether the user has added the app.
550
+ # Example:
551
+ # <% fb_if_is_app_user(@facebook_user) do %>
552
+ # Thanks for accepting our terms of service!
553
+ # <% fb_else do %>
554
+ # Hey you haven't agreed to our terms. <%= link_to("Please accept our terms of service.", :action => "terms_of_service") %>
555
+ # <% end %>
556
+ #<% end %>
557
+ def fb_if_is_app_user(user=nil,options={},&proc)
558
+ content = capture(&proc)
559
+ options = options.dup
560
+ options.merge!(:uid=>cast_to_facebook_id(user)) if user
561
+ if ignore_binding?
562
+ concat(content_tag("fb:if-is-app-user",content,stringify_vals(options)))
563
+ else
564
+ concat(content_tag("fb:if-is-app-user",content,stringify_vals(options)),proc.binding)
565
+ end
566
+ end
567
+
568
+ # Render if-user-has-added-app tag
569
+ # This tag renders the enclosing content only if the user specified has installed the application
570
+ #
571
+ # Example:
572
+ # <% fb_if_user_has_added_app(@facebook_user) do %>
573
+ # Hey you are an app user!
574
+ # <% fb_else do %>
575
+ # Hey you aren't an app user. <%= link_to("Add App and see the other side.", :action => "added_app") %>
576
+ # <% end %>
577
+ #<% end %>
578
+ def fb_if_user_has_added_app(user,options={},&proc)
579
+ content = capture(&proc)
580
+ options = options.dup
581
+ if ignore_binding?
582
+ concat(content_tag("fb:if-user-has-added-app", content, stringify_vals(options.merge(:uid=>cast_to_facebook_id(user)))))
583
+ else
584
+ concat(content_tag("fb:if-user-has-added-app", content, stringify_vals(options.merge(:uid=>cast_to_facebook_id(user)))),proc.binding)
585
+ end
586
+ end
587
+
588
+ # Render fb:if-is-user tag
589
+ # This tag only renders enclosing content if the user is one of those specified
590
+ # user can be a single user or an Array of users
591
+ # Example:
592
+ # <% fb_if_is_user(@check_user) do %>
593
+ # <%= fb_name(@facebook_user) %> are one of the users. <%= link_to("Check the other side", :action => "friend") %>
594
+ # <% fb_else do %>
595
+ # <%= fb_name(@facebook_user) %> are not one of the users <%= fb_name(@check_user) %>
596
+ # <%= link_to("Check the other side", :action => "you") %>
597
+ # <% end %>
598
+ # <% end %>
599
+ def fb_if_is_user(user,&proc)
600
+ content = capture(&proc)
601
+ user = [user] unless user.is_a? Array
602
+ user_list=user.map{|u| cast_to_facebook_id(u)}.join(",")
603
+ if ignore_binding?
604
+ concat(content_tag("fb:if-is-user",content,{:uid=>user_list}))
605
+ else
606
+ concat(content_tag("fb:if-is-user",content,{:uid=>user_list}),proc.binding)
607
+ end
608
+ end
609
+
610
+ # Render fb:else tag
611
+ # 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
612
+ def fb_else(&proc)
613
+ content = capture(&proc)
614
+ if ignore_binding?
615
+ concat(content_tag("fb:else",content))
616
+ else
617
+ concat(content_tag("fb:else",content),proc.binding)
618
+ end
619
+ end
620
+
621
+ #
622
+ # Return the URL for the about page of the application
623
+ def fb_about_url
624
+ "http://#{Facebooker.www_server_base_url}/apps/application.php?api_key=#{Facebooker.api_key}"
625
+ end
626
+
627
+ #
628
+ # Embed a discussion board named xid on the current page
629
+ # <em>See</em http://wiki.developers.facebook.com/index.php/Fb:board for more details
630
+ # Options are:
631
+ # * canpost
632
+ # * candelete
633
+ # * canmark
634
+ # * cancreatet
635
+ # * numtopics
636
+ # * callbackurl
637
+ # * returnurl
638
+ #
639
+ def fb_board(xid,options={})
640
+ options = options.dup
641
+ title = (title = options.delete(:title)) ? fb_title(title) : nil
642
+ content_tag("fb:board", title, stringify_vals(options.merge(:xid=>xid)))
643
+ end
644
+
645
+ # Renders an 'Add to Profile' button
646
+ # The button allows a user to add condensed profile box to the main profile
647
+ def fb_add_profile_section
648
+ tag "fb:add-section-button",:section=>"profile"
649
+ end
650
+
651
+ # Renders an 'Add to Info' button
652
+ # The button allows a user to add an application info section to her Info tab
653
+ def fb_add_info_section
654
+ tag "fb:add-section-button",:section=>"info"
655
+ end
656
+
657
+ # Renders a link that, when clicked, initiates a dialog requesting the specified extended permission from the user.
658
+ #
659
+ # You can prompt a user with the following permissions:
660
+ # * email
661
+ # * read_stream
662
+ # * publish_stream
663
+ # * offline_access
664
+ # * status_update
665
+ # * photo_upload
666
+ # * create_event
667
+ # * rsvp_event
668
+ # * sms
669
+ # * video_upload
670
+ # * create_note
671
+ # * share_item
672
+ # Example:
673
+ # <%= fb_prompt_permission('email', "Would you like to receive email from our application?" ) %>
674
+ #
675
+ # See http://wiki.developers.facebook.com/index.php/Fb:prompt-permission for
676
+ # more details. Correct as of 7th June 2009.
677
+ #
678
+ def fb_prompt_permission(permission,message,callback=nil)
679
+ raise(ArgumentError, "Unknown value for permission: #{permission}") unless VALID_PERMISSIONS.include?(permission.to_sym)
680
+ args={:perms=>permission}
681
+ args[:next_fbjs]=callback unless callback.nil?
682
+ content_tag("fb:prompt-permission",message,args)
683
+ end
684
+
685
+ # Renders a link to prompt for multiple permissions at once.
686
+ #
687
+ # Example:
688
+ # <%= fb_prompt_permissions(['email','offline_access','status_update'], 'Would you like to grant some permissions?')
689
+ def fb_prompt_permissions(permissions,message,callback=nil)
690
+ permissions.each do |p|
691
+ raise(ArgumentError, "Unknown value for permission: #{p}") unless VALID_PERMISSIONS.include?(p.to_sym)
692
+ end
693
+ args={:perms=>permissions*','}
694
+ args[:next_fbjs]=callback unless callback.nil?
695
+ content_tag("fb:prompt-permission",message,args)
696
+ end
697
+
698
+ # Renders an <fb:eventlink /> tag that displays the event name and links to the event's page.
699
+ def fb_eventlink(eid)
700
+ content_tag "fb:eventlink",nil,:eid=>eid
701
+ end
702
+
703
+ # Renders an <fb:grouplink /> tag that displays the group name and links to the group's page.
704
+ def fb_grouplink(gid)
705
+ content_tag "fb:grouplink",nil,:gid=>gid
706
+ end
707
+
708
+ # Returns the status of the user
709
+ def fb_user_status(user,linked=true)
710
+ content_tag "fb:user-status",nil,stringify_vals(:uid=>cast_to_facebook_id(user), :linked=>linked)
711
+ end
712
+
713
+ # Renders a standard 'Share' button for the specified URL.
714
+ def fb_share_button(url)
715
+ content_tag "fb:share-button",nil,:class=>"url",:href=>url
716
+ end
717
+
718
+ # Renders the FBML on a Facebook server inside an iframe.
719
+ #
720
+ # Meant to be used for a Facebook Connect site or an iframe application
721
+ def fb_serverfbml(options={},&proc)
722
+ inner = capture(&proc)
723
+ concat(content_tag("fb:serverfbml",inner,options),&proc.binding)
724
+ end
725
+
726
+ def fb_container(options={},&proc)
727
+ inner = capture(&proc)
728
+ concat(content_tag("fb:container",inner,options),&proc.binding)
729
+ end
730
+
731
+ # Renders an fb:time element
732
+ #
733
+ # Example:
734
+ # <%= fb_time(Time.now, :tz => 'America/New York', :preposition => true) %>
735
+ #
736
+ # See http://wiki.developers.facebook.com/index.php/Fb:time for
737
+ # more details
738
+ def fb_time(time, options={})
739
+ tag "fb:time",stringify_vals({:t => time.to_i}.merge(options))
740
+ end
741
+
742
+ protected
743
+
744
+ def cast_to_facebook_id(object)
745
+ Facebooker::User.cast_to_facebook_id(object)
746
+ end
747
+
748
+ def validate_fb_profile_pic_size(options)
749
+ if options.has_key?(:size) && !VALID_FB_PROFILE_PIC_SIZES.include?(options[:size].to_sym)
750
+ raise(ArgumentError, "Unknown value for size: #{options[:size]}")
751
+ end
752
+ end
753
+
754
+ def validate_fb_photo_size(options)
755
+ if options.has_key?(:size) && !VALID_FB_PHOTO_SIZES.include?(options[:size].to_sym)
756
+ raise(ArgumentError, "Unknown value for size: #{options[:size]}")
757
+ end
758
+ end
759
+
760
+ private
761
+ def stringify_vals(hash)
762
+ result={}
763
+ hash.each do |key,value|
764
+ result[key]=value.to_s
765
+ end
766
+ result
767
+ end
768
+
769
+ def fb_status_msg(type, message, text)
770
+ if text.blank?
771
+ tag("fb:#{type}", :message => message)
772
+ else
773
+ content_tag("fb:#{type}", content_tag("fb:message", message) + text)
774
+ end
775
+ end
776
+
777
+ def token_tag
778
+ unless protect_against_forgery?
779
+ ''
780
+ else
781
+ tag(:input, :type => "hidden", :name => request_forgery_protection_token.to_s, :value => form_authenticity_token)
782
+ end
783
+ end
784
+
785
+ def ignore_binding?
786
+ ActionPack::VERSION::MAJOR >= 2 && ActionPack::VERSION::MINOR >= 2
787
+ end
788
+ end
789
+ end
790
+ end
791
+
792
+ class Hash
793
+ def transform_keys!(transformation_hash)
794
+ transformation_hash.each_pair{|key, value| transform_key!(key, value)}
795
+ end
796
+
797
+
798
+ def transform_key!(old_key, new_key)
799
+ swapkey!(new_key, old_key)
800
+ end
801
+
802
+ ### This method is lifted from Ruby Facets core
803
+ def swapkey!( newkey, oldkey )
804
+ self[newkey] = self.delete(oldkey) if self.has_key?(oldkey)
805
+ self
806
+ end
807
+
808
+ # We can allow css attributes.
809
+ FB_ALWAYS_VALID_OPTION_KEYS = [:class, :style]
810
+ def assert_valid_keys(*valid_keys)
811
+ unknown_keys = keys - [valid_keys + FB_ALWAYS_VALID_OPTION_KEYS].flatten
812
+ raise(ArgumentError, "Unknown key(s): #{unknown_keys.join(", ")}") unless unknown_keys.empty?
813
+ end
814
+ end