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,10 @@
1
+ module Facebooker
2
+ class InfoItem
3
+ include Model
4
+ attr_accessor :label, :image, :description, :link, :sublabel
5
+
6
+ def to_json
7
+ {:label => label, :image => image, :description => description, :link => link, :sublabel => sublabel}.to_json
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,10 @@
1
+ module Facebooker
2
+ class InfoSection
3
+ include Model
4
+ attr_accessor :field, :items
5
+
6
+ def to_json
7
+ {:field => field, :items => items}.to_json
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,8 @@
1
+ module Facebooker
2
+ ##
3
+ # Representation of Location used in all places where a Location is specified.
4
+ class Location
5
+ include Model
6
+ attr_accessor :city, :zip, :country, :state
7
+ end
8
+ end
@@ -0,0 +1,17 @@
1
+ module Facebooker
2
+ class Notifications
3
+ include Model
4
+ attr_accessor :messages, :group_invites, :pokes, :friend_requests, :event_invites, :shares
5
+
6
+ [:Messages, :Pokes, :Shares].each do |notification_type|
7
+ const_set(notification_type, Class.new do
8
+ include Model
9
+ attr_accessor :unread, :most_recent
10
+ end)
11
+ attribute_name = "#{notification_type.to_s.downcase}"
12
+ define_method("#{attribute_name}=") do |value|
13
+ instance_variable_set("@#{attribute_name}", value.kind_of?(Hash) ? Notifications.const_get(notification_type).from_hash(value) : value)
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,44 @@
1
+ require 'facebooker/model'
2
+ module Facebooker
3
+ class Page
4
+
5
+ def initialize(*args)
6
+ if args.size == 1 and (args.first.is_a?(Integer) or args.first.is_a?(String))
7
+ self.page_id=args.first
8
+ else
9
+ super
10
+ end
11
+ end
12
+
13
+ class Genre
14
+ include Model
15
+ FIELDS = [ :dance, :party, :relax, :talk, :think, :workout, :sing, :intimate, :raunchy, :headphones ]
16
+ attr_accessor(*FIELDS)
17
+
18
+ def initialize(*args)
19
+ super
20
+
21
+ # convert '1'/'0' to true/false
22
+ FIELDS.each do |field|
23
+ self.send("#{field}=", self.send(field) == '1')
24
+ end
25
+ end
26
+ end
27
+
28
+ include Model
29
+ attr_accessor :page_id, :name, :pic_small, :pic_big, :pic_square, :pic_large, :page_url, :type, :website, :location, :hours, :band_members, :bio, :hometown, :record_label, :influences, :has_added_app, :founded, :company_overview, :mission, :products, :release_date, :starring, :written_by, :directed_by, :produced_by, :studio, :awards, :plot_outline, :network, :season, :schedule
30
+ attr_reader :genre
31
+
32
+ def genre=(value)
33
+ @genre = value.kind_of?(Hash) ? Genre.from_hash(value) : value
34
+ end
35
+
36
+ def user_is_admin?(user)
37
+ Session.current.post('facebook.pages.isAdmin', :page_id=>self.page_id, :uid=>Facebooker::User.cast_to_facebook_id(user))
38
+ end
39
+
40
+ def user_is_fan?(user)
41
+ Session.current.post('facebook.pages.isFan', :page_id=>self.page_id, :uid=>Facebooker::User.cast_to_facebook_id(user))
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,19 @@
1
+ require 'facebooker/model'
2
+ module Facebooker
3
+ class Photo
4
+ include Model
5
+ attr_accessor :aid, :owner, :title,
6
+ :link, :caption, :created,
7
+ :src, :src_big, :src_small,
8
+ :story_fbid
9
+
10
+ id_is :pid
11
+
12
+ #override the generated method for id_is to use a string
13
+ def pid=(val)
14
+ @pid = val
15
+ end
16
+
17
+ alias :id= :pid=
18
+ end
19
+ end
@@ -0,0 +1,12 @@
1
+ require 'facebooker/model'
2
+ module Facebooker
3
+ class Tag
4
+ include Model
5
+ attr_accessor :pid, :subject, :xcoord, :ycoord
6
+
7
+ def coordinates
8
+ [xcoord, ycoord]
9
+ end
10
+
11
+ end
12
+ end
@@ -0,0 +1,516 @@
1
+ require 'facebooker/model'
2
+ require 'facebooker/models/affiliation'
3
+ require 'facebooker/models/work_info'
4
+ module Facebooker
5
+ #
6
+ # Holds attributes and behavior for a Facebook User
7
+ class User
8
+ include Model
9
+ class Status
10
+ include Model
11
+ attr_accessor :message, :time, :status_id
12
+ end
13
+ FIELDS = [:status, :political, :pic_small, :name, :quotes, :is_app_user, :tv, :profile_update_time, :meeting_sex, :hs_info, :timezone, :relationship_status, :hometown_location, :about_me, :wall_count, :significant_other_id, :pic_big, :music, :work_history, :sex, :religion, :notes_count, :activities, :pic_square, :movies, :has_added_app, :education_history, :birthday, :birthday_date, :first_name, :meeting_for, :last_name, :interests, :current_location, :pic, :books, :affiliations, :locale, :profile_url, :proxied_email, :email_hashes, :allowed_restrictions, :pic_with_logo, :pic_big_with_logo, :pic_small_with_logo, :pic_square_with_logo]
14
+ STANDARD_FIELDS = [:uid, :first_name, :last_name, :name, :timezone, :birthday, :sex, :affiliations, :locale, :profile_url, :pic_square]
15
+ populating_attr_accessor(*FIELDS)
16
+ attr_reader :affiliations
17
+ populating_hash_settable_accessor :current_location, Location
18
+ populating_hash_settable_accessor :hometown_location, Location
19
+ populating_hash_settable_accessor :hs_info, EducationInfo::HighschoolInfo
20
+ populating_hash_settable_list_accessor :affiliations, Affiliation
21
+ populating_hash_settable_list_accessor :education_history, EducationInfo
22
+ populating_hash_settable_list_accessor :work_history, WorkInfo
23
+
24
+ populating_attr_reader :status
25
+
26
+ # Can pass in these two forms:
27
+ # id, session, (optional) attribute_hash
28
+ # attribute_hash
29
+ def initialize(*args)
30
+ @friends = nil
31
+ @current_location = nil
32
+ @pic = nil
33
+ @hometown_location = nil
34
+ @populated = false
35
+ @session = nil
36
+ @id = nil
37
+ if (args.first.kind_of?(String) || args.first.kind_of?(Integer)) && args.size==1
38
+ self.uid = args.shift
39
+ @session = Session.current
40
+ elsif (args.first.kind_of?(String) || args.first.kind_of?(Integer)) && args[1].kind_of?(Session)
41
+ self.uid = args.shift
42
+ @session = args.shift
43
+ end
44
+ if args.last.kind_of?(Hash)
45
+ populate_from_hash!(args.pop)
46
+ end
47
+ end
48
+
49
+ id_is :uid
50
+ alias :facebook_id :id
51
+
52
+ # Returns a user's events, params correspond to API call parameters (except UID):
53
+ # http://wiki.developers.facebook.com/index.php/Events.get
54
+ # E.g:
55
+ # @user.events(:start_time => Time.now, :end_time => 1.month.from_now)
56
+ # # => Returns events betwen now and a month from now
57
+ def events(params={})
58
+ @events ||= {}
59
+ [:start_time,:end_time].compact.each do |key|
60
+ params[key] = params[key].to_i
61
+ end
62
+ # puts @events[params.to_s].nil?
63
+ @events[params.to_s] ||= @session.post('facebook.events.get', {:uid => self.id}.merge(params)).map do |event|
64
+ Event.from_hash(event)
65
+ end
66
+ end
67
+
68
+ #
69
+ # Set the list of friends, given an array of User objects. If the list has been retrieved previously, will not set
70
+ def friends=(list_of_friends,flid=nil)
71
+ @friends_hash ||= {}
72
+ flid=cast_to_friend_list_id(flid)
73
+ #use __blank instead of nil so that this is cached
74
+ cache_key = flid||"__blank"
75
+
76
+ @friends_hash[cache_key] ||= list_of_friends
77
+ end
78
+
79
+ def cast_to_friend_list_id(flid)
80
+ case flid
81
+ when String
82
+ list=friend_lists.detect {|f| f.name==flid}
83
+ raise Facebooker::Session::InvalidFriendList unless list
84
+ list.flid
85
+ when FriendList
86
+ flid.flid
87
+ else
88
+ flid
89
+ end
90
+ end
91
+ ##
92
+ # Retrieve friends
93
+ def friends(flid = nil)
94
+ @friends_hash ||= {}
95
+ flid=cast_to_friend_list_id(flid)
96
+
97
+ #use __blank instead of nil so that this is cached
98
+ cache_key = flid||"__blank"
99
+ options = {:uid=>self.id}
100
+ options[:flid] = flid unless flid.nil?
101
+ @friends_hash[cache_key] ||= @session.post('facebook.friends.get', options,false).map do |uid|
102
+ User.new(uid, @session)
103
+ end
104
+ @friends_hash[cache_key]
105
+ end
106
+
107
+ ###
108
+ # Publish a post into the stream on the user's Wall and News Feed. This
109
+ # post also appears in the user's friend's streams. The +publish_stream+
110
+ # extended permission must be granted in order to use this method.
111
+ #
112
+ # See: http://wiki.developers.facebook.com/index.php/Stream.publish
113
+ #
114
+ # +target+ can be the current user or some other user.
115
+ #
116
+ # Example:
117
+ # # Publish a message to my own wall:
118
+ # me.publish_to(me, :message => 'hello world')
119
+ #
120
+ # # Publish to a friend's wall with an action link:
121
+ # me.publish_to(my_friend, :message => 'how are you?', :action_links => [
122
+ # :text => 'my website',
123
+ # :href => 'http://tenderlovemaking.com/'
124
+ # ])
125
+ def publish_to target, options = {}
126
+ @session.post('facebook.stream.publish',
127
+ :uid => self.id,
128
+ :target_id => target.id,
129
+ :message => options[:message],
130
+ :attachment => Facebooker.json_encode(options[:attachment]),
131
+ :action_links => Facebooker.json_encode(options[:action_links])
132
+ )
133
+ end
134
+
135
+
136
+ ###
137
+ # Publish a comment on a post
138
+ #
139
+ # See: http://wiki.developers.facebook.com/index.php/Stream.addComment
140
+ #
141
+ # +post_id+ the post_id for the post that is being commented on
142
+ # +comment+ the text of the comment
143
+ def comment_on(post_id, comment)
144
+ @session.post('facebook.stream.addComment', {:post_id=>post_id, :comment=>comment})
145
+ end
146
+
147
+
148
+ def friend_lists
149
+ @friend_lists ||= @session.post('facebook.friends.getLists').map do |hash|
150
+ friend_list = FriendList.from_hash(hash)
151
+ friend_list.session = session
152
+ friend_list
153
+ end
154
+ end
155
+ ###
156
+ # Retrieve friends with user info populated
157
+ # Subsequent calls will be retrieved from memory.
158
+ # Optional: list of fields to retrieve as symbols
159
+ def friends!(*fields)
160
+ @friends ||= session.post('facebook.users.getInfo', :fields => collect(fields), :uids => friends.map{|f| f.id}.join(',')).map do |hash|
161
+ User.new(hash['uid'], session, hash)
162
+ end
163
+ end
164
+
165
+ ###
166
+ # Retrieve profile data for logged in user
167
+ # Optional: list of fields to retrieve as symbols
168
+ def populate(*fields)
169
+ session.post('facebook.users.getInfo', :fields => collect(fields), :uids => id) do |response|
170
+ populate_from_hash!(response.first)
171
+ end
172
+ end
173
+
174
+ def friends_with?(user_or_id)
175
+ friends.map{|f| f.to_i}.include?(user_or_id.to_i)
176
+ end
177
+
178
+ def friends_with_this_app
179
+ @friends_with_this_app ||= session.post('facebook.friends.getAppUsers').map do |uid|
180
+ User.new(uid, session)
181
+ end
182
+ end
183
+
184
+ def groups(gids = [])
185
+ args = gids.empty? ? {} : {:gids => gids}
186
+ @groups ||= session.post('facebook.groups.get', args).map do |hash|
187
+ group = Group.from_hash(hash)
188
+ group.session = session
189
+ group
190
+ end
191
+ end
192
+
193
+ def notifications
194
+ @notifications ||= Notifications.from_hash(session.post('facebook.notifications.get'))
195
+ end
196
+
197
+ def publish_story(story)
198
+ publish(story)
199
+ end
200
+
201
+ def publish_action(action)
202
+ publish(action)
203
+ end
204
+
205
+ def publish_templatized_action(action)
206
+ publish(action)
207
+ end
208
+
209
+ def albums
210
+ @albums ||= session.post('facebook.photos.getAlbums', :uid => self.id) do |response|
211
+ response.map do |hash|
212
+ Album.from_hash(hash)
213
+ end
214
+ end
215
+ end
216
+
217
+ def create_album(params)
218
+ @album = session.post('facebook.photos.createAlbum', params) {|response| Album.from_hash(response)}
219
+ end
220
+
221
+ def profile_photos
222
+ session.get_photos(nil, nil, profile_pic_album_id)
223
+ end
224
+
225
+ # Upload a photo to the user's profile.
226
+ #
227
+ # In your view, create a multipart form that posts directly to your application (not through canvas):
228
+ #
229
+ # <% form_tag photos_url(:canvas => false), :html => {:multipart => true, :promptpermission => 'photo_upload'} do %>
230
+ # Photo: <%= file_field_tag 'photo' %>
231
+ # Caption: <%= text_area_tag 'caption' %>
232
+ # <%= submit_tag 'Upload Photo', :class => 'inputsubmit' %>
233
+ # <% end %>
234
+ #
235
+ # And in your controller:
236
+ #
237
+ # class PhotosController < ApplicationController
238
+ # def create
239
+ # file = Net::HTTP::MultipartPostFile.new(
240
+ # params[:photo].original_filename,
241
+ # params[:photo].content_type,
242
+ # params[:photo].read
243
+ # )
244
+ #
245
+ # @photo = facebook_session.user.upload_photo(file, :caption => params[:caption])
246
+ # redirect_to photos_url(:canvas => true)
247
+ # end
248
+ # end
249
+ #
250
+ # Options correspond to http://wiki.developers.facebook.com/index.php/Photos.upload
251
+ def upload_photo(multipart_post_file, options = {})
252
+ Photo.from_hash(session.post_file('facebook.photos.upload',
253
+ options.merge(nil => multipart_post_file)))
254
+ end
255
+
256
+ # Upload a video to the user's profile.
257
+ #
258
+ # In your view, create a multipart form that posts directly to your application (not through canvas):
259
+ #
260
+ # <% form_tag videos_url(:canvas => false), :html => {:multipart => true, :promptpermission => 'video_upload'} do %>
261
+ # Video: <%= file_field_tag 'video' %>
262
+ # Title: <%= text_area_tag 'title' %>
263
+ # Description: <%= text_area_tag 'description' %>
264
+ # <%= submit_tag 'Upload Video', :class => 'inputsubmit' %>
265
+ # <% end %>
266
+ #
267
+ # And in your controller:
268
+ #
269
+ # class VideosController < ApplicationController
270
+ # def create
271
+ # file = Net::HTTP::MultipartPostFile.new(
272
+ # params[:photo].original_filename,
273
+ # params[:photo].content_type,
274
+ # params[:photo].read
275
+ # )
276
+ #
277
+ # @video = facebook_session.user.upload_video(file, :description => params[:description])
278
+ # redirect_to videos_url(:canvas => true)
279
+ # end
280
+ # end
281
+ #
282
+ # Options correspond to http://wiki.developers.facebook.com/index.php/Video.upload
283
+ def upload_video(multipart_post_file, options = {})
284
+ Video.from_hash(session.post_file('facebook.video.upload',
285
+ options.merge(nil => multipart_post_file, :base => Facebooker.video_server_base)))
286
+ end
287
+
288
+ def profile_fbml
289
+ session.post('facebook.profile.getFBML', :uid => id)
290
+ end
291
+
292
+ ##
293
+ # Set the profile FBML for this user
294
+ #
295
+ # This does not set profile actions, that should be done with profile_action=
296
+ def profile_fbml=(markup)
297
+ set_profile_fbml(markup, nil, nil, nil)
298
+ end
299
+
300
+ ##
301
+ # Set the mobile profile FBML
302
+ def mobile_fbml=(markup)
303
+ set_profile_fbml(nil, markup, nil,nil)
304
+ end
305
+
306
+ def profile_action=(markup)
307
+ set_profile_fbml(nil, nil, markup,nil)
308
+ end
309
+
310
+ def profile_main=(markup)
311
+ set_profile_fbml(nil,nil,nil,markup)
312
+ end
313
+
314
+ def set_profile_fbml(profile_fbml, mobile_fbml, profile_action_fbml, profile_main = nil)
315
+ parameters = {:uid => id}
316
+ parameters[:profile] = profile_fbml if profile_fbml
317
+ parameters[:profile_action] = profile_action_fbml if profile_action_fbml
318
+ parameters[:mobile_profile] = mobile_fbml if mobile_fbml
319
+ parameters[:profile_main] = profile_main if profile_main
320
+ session.post('facebook.profile.setFBML', parameters,false)
321
+ end
322
+
323
+ ## ** NEW PROFILE DESIGN ***
324
+ # Set a info section for this user
325
+ #
326
+ # Note: using set_profile_info as I feel using user.set_info could be confused with the user.getInfo facebook method.
327
+ # Also, I feel it fits in line with user.set_profile_fbml.
328
+ def set_profile_info(title, info_fields, format = :text)
329
+ session.post('facebook.profile.setInfo', :title => title, :uid => id,
330
+ :type => format.to_s == "text" ? 1 : 5, :info_fields => info_fields.to_json)
331
+ end
332
+
333
+ def get_profile_info
334
+ session.post('facebook.profile.getInfo', :uid => id)
335
+ end
336
+
337
+ ##
338
+ # This DOES NOT set the status of a user on Facebook
339
+ # Use the set_status method instead
340
+ def status=(message)
341
+ case message
342
+ when String,Status
343
+ @status = message
344
+ when Hash
345
+ @status = Status.from_hash(message)
346
+ end
347
+ end
348
+
349
+ ##
350
+ # Set the status for a user
351
+ # DOES NOT prepend "is" to the message
352
+ #
353
+ # requires extended permission.
354
+ def set_status(message)
355
+ self.status=message
356
+ session.post('facebook.users.setStatus',{:status=>message,:status_includes_verb=>1,:uid => uid}, false) do |ret|
357
+ ret
358
+ end
359
+ end
360
+
361
+ ##
362
+ # Checks to see if the user has enabled the given extended permission
363
+ def has_permission?(ext_perm) # ext_perm = email, offline_access, status_update, photo_upload, create_listing, create_event, rsvp_event, sms
364
+ session.post('facebook.users.hasAppPermission',:ext_perm=>ext_perm) == "1"
365
+ end
366
+
367
+ ##
368
+ # Convenience method to check multiple permissions at once
369
+ def has_permissions?(ext_perms)
370
+ ext_perms.all?{|p| has_permission?(p)}
371
+ end
372
+
373
+ ##
374
+ # Convenience method to send email to the current user
375
+ def send_email(subject, text=nil, fbml=nil)
376
+ session.send_email([id], subject, text, fbml)
377
+ end
378
+
379
+ ##
380
+ # Convenience method to set cookie for the current user
381
+ def set_cookie(name, value, expires=nil, path=nil)
382
+ session.data.set_cookie(id, name, value, expires, path)
383
+ end
384
+
385
+ ##
386
+ # Convenience method to get cookies for the current user
387
+ def get_cookies(name=nil)
388
+ session.data.get_cookies(id, name)
389
+ end
390
+
391
+ ##
392
+ # Returns the user's id as an integer
393
+ def to_i
394
+ id
395
+ end
396
+
397
+ def to_s
398
+ id.to_s
399
+ end
400
+
401
+ ##
402
+ # Two Facebooker::User objects should be considered equal if their Facebook ids are equal
403
+ def ==(other_user)
404
+ other_user.is_a?(User) && id == other_user.id
405
+ end
406
+
407
+
408
+ # register a user with Facebook
409
+ # users should be a hast with at least an :email field
410
+ # you can optionally provide an :account_id field as well
411
+
412
+ def self.register(users)
413
+ user_map={}
414
+ users=users.map do |h|
415
+ returning h.dup do |d|
416
+ if email=d.delete(:email)
417
+ hash = hash_email(email)
418
+ user_map[hash]=h
419
+ d[:email_hash]=hash
420
+ end
421
+ end
422
+ end
423
+ Facebooker::Session.create.post("facebook.connect.registerUsers",:accounts=>users.to_json) do |ret|
424
+ ret.each do |hash|
425
+ user_map.delete(hash)
426
+ end
427
+ unless user_map.empty?
428
+ e=Facebooker::Session::UserRegistrationFailed.new
429
+ e.failed_users = user_map.values
430
+ raise e
431
+ end
432
+ ret
433
+ end
434
+ end
435
+
436
+ # Get a count of unconnected friends
437
+ def getUnconnectedFriendsCount
438
+ session.post("facebook.connect.getUnconnectedFriendsCount")
439
+ end
440
+
441
+
442
+ # Unregister an array of email hashes
443
+ def self.unregister(email_hashes)
444
+ Facebooker::Session.create.post("facebook.connect.unregisterUsers",:email_hashes=>email_hashes.to_json) do |ret|
445
+ ret.each do |hash|
446
+ email_hashes.delete(hash)
447
+ end
448
+ unless email_hashes.empty?
449
+ e=Facebooker::Session::UserUnRegistrationFailed.new
450
+ e.failed_users = email_hashes
451
+ raise e
452
+ end
453
+ ret
454
+ end
455
+ end
456
+
457
+ # unregister an array of email addresses
458
+ def self.unregister_emails(emails)
459
+ emails_hash = {}
460
+ emails.each {|e| emails_hash[hash_email(e)] = e}
461
+ begin
462
+ unregister(emails_hash.keys).collect {|r| emails_hash[r]}
463
+ rescue
464
+ # re-raise with emails instead of hashes.
465
+ e = Facebooker::Session::UserUnRegistrationFailed.new
466
+ e.failed_users = $!.failed_users.collect { |f| emails_hash[f] }
467
+ raise e
468
+ end
469
+ end
470
+
471
+ def self.hash_email(email)
472
+ email = email.downcase.strip
473
+ crc=Zlib.crc32(email)
474
+ md5=Digest::MD5.hexdigest(email)
475
+ "#{crc}_#{md5}"
476
+ end
477
+
478
+ def self.cast_to_facebook_id(object)
479
+ if object.respond_to?(:facebook_id)
480
+ object.facebook_id
481
+ else
482
+ object
483
+ end
484
+ end
485
+
486
+ def self.user_fields(fields = [])
487
+ valid_fields(fields)
488
+ end
489
+
490
+ def self.standard_fields(fields = [])
491
+ valid_fields(fields,STANDARD_FIELDS)
492
+ end
493
+
494
+ private
495
+ def publish(feed_story_or_action)
496
+ session.post(Facebooker::Feed::METHODS[feed_story_or_action.class.name.split(/::/).last], feed_story_or_action.to_params) == "1" ? true : false
497
+ end
498
+
499
+ def self.valid_fields(fields, allowable=FIELDS)
500
+ allowable.reject{|field_name| !fields.empty? && !fields.include?(field_name)}.join(',')
501
+ end
502
+
503
+ def collect(fields, allowable=FIELDS)
504
+ allowable.reject{|field_name| !fields.empty? && !fields.include?(field_name)}.join(',')
505
+ end
506
+
507
+ def profile_pic_album_id
508
+ merge_aid(-3, id)
509
+ end
510
+
511
+ def merge_aid(aid, uid)
512
+ (uid << 32) + (aid & 0xFFFFFFFF)
513
+ end
514
+
515
+ end
516
+ end