fbooker 1.0.53

Sign up to get free protection for your applications and to get access to all the features.
Files changed (142) hide show
  1. data/.autotest +15 -0
  2. data/CHANGELOG.rdoc +24 -0
  3. data/COPYING.rdoc +19 -0
  4. data/Manifest.txt +144 -0
  5. data/README.rdoc +113 -0
  6. data/Rakefile +94 -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 +332 -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/templates/xd_receiver_ssl.html +10 -0
  43. data/generators/xd_receiver/xd_receiver_generator.rb +10 -0
  44. data/init.rb +26 -0
  45. data/install.rb +12 -0
  46. data/lib/facebooker.rb +252 -0
  47. data/lib/facebooker/adapters/adapter_base.rb +91 -0
  48. data/lib/facebooker/adapters/bebo_adapter.rb +77 -0
  49. data/lib/facebooker/adapters/facebook_adapter.rb +60 -0
  50. data/lib/facebooker/admin.rb +42 -0
  51. data/lib/facebooker/application.rb +13 -0
  52. data/lib/facebooker/batch_request.rb +45 -0
  53. data/lib/facebooker/data.rb +57 -0
  54. data/lib/facebooker/feed.rb +78 -0
  55. data/lib/facebooker/logging.rb +44 -0
  56. data/lib/facebooker/mobile.rb +20 -0
  57. data/lib/facebooker/mock/service.rb +50 -0
  58. data/lib/facebooker/mock/session.rb +18 -0
  59. data/lib/facebooker/model.rb +139 -0
  60. data/lib/facebooker/models/affiliation.rb +10 -0
  61. data/lib/facebooker/models/album.rb +11 -0
  62. data/lib/facebooker/models/applicationproperties.rb +39 -0
  63. data/lib/facebooker/models/applicationrestrictions.rb +10 -0
  64. data/lib/facebooker/models/cookie.rb +10 -0
  65. data/lib/facebooker/models/education_info.rb +11 -0
  66. data/lib/facebooker/models/event.rb +28 -0
  67. data/lib/facebooker/models/family_relative_info.rb +7 -0
  68. data/lib/facebooker/models/friend_list.rb +16 -0
  69. data/lib/facebooker/models/group.rb +36 -0
  70. data/lib/facebooker/models/info_item.rb +10 -0
  71. data/lib/facebooker/models/info_section.rb +10 -0
  72. data/lib/facebooker/models/location.rb +8 -0
  73. data/lib/facebooker/models/notifications.rb +17 -0
  74. data/lib/facebooker/models/page.rb +44 -0
  75. data/lib/facebooker/models/photo.rb +19 -0
  76. data/lib/facebooker/models/tag.rb +12 -0
  77. data/lib/facebooker/models/user.rb +552 -0
  78. data/lib/facebooker/models/video.rb +9 -0
  79. data/lib/facebooker/models/work_info.rb +10 -0
  80. data/lib/facebooker/parser.rb +717 -0
  81. data/lib/facebooker/rails/backwards_compatible_param_checks.rb +31 -0
  82. data/lib/facebooker/rails/controller.rb +353 -0
  83. data/lib/facebooker/rails/cucumber.rb +28 -0
  84. data/lib/facebooker/rails/cucumber/world.rb +40 -0
  85. data/lib/facebooker/rails/extensions/action_controller.rb +48 -0
  86. data/lib/facebooker/rails/extensions/rack_setup.rb +6 -0
  87. data/lib/facebooker/rails/extensions/routing.rb +15 -0
  88. data/lib/facebooker/rails/facebook_form_builder.rb +112 -0
  89. data/lib/facebooker/rails/facebook_pretty_errors.rb +22 -0
  90. data/lib/facebooker/rails/facebook_request_fix.rb +28 -0
  91. data/lib/facebooker/rails/facebook_request_fix_2-3.rb +31 -0
  92. data/lib/facebooker/rails/facebook_session_handling.rb +68 -0
  93. data/lib/facebooker/rails/facebook_url_helper.rb +192 -0
  94. data/lib/facebooker/rails/facebook_url_rewriting.rb +60 -0
  95. data/lib/facebooker/rails/helpers.rb +870 -0
  96. data/lib/facebooker/rails/helpers/fb_connect.rb +122 -0
  97. data/lib/facebooker/rails/integration_session.rb +38 -0
  98. data/lib/facebooker/rails/profile_publisher_extensions.rb +42 -0
  99. data/lib/facebooker/rails/publisher.rb +595 -0
  100. data/lib/facebooker/rails/routing.rb +49 -0
  101. data/lib/facebooker/rails/test_helpers.rb +68 -0
  102. data/lib/facebooker/rails/utilities.rb +22 -0
  103. data/lib/facebooker/server_cache.rb +24 -0
  104. data/lib/facebooker/service.rb +102 -0
  105. data/lib/facebooker/service/base_service.rb +19 -0
  106. data/lib/facebooker/service/curl_service.rb +44 -0
  107. data/lib/facebooker/service/net_http_service.rb +12 -0
  108. data/lib/facebooker/service/typhoeus_multi_service.rb +27 -0
  109. data/lib/facebooker/service/typhoeus_service.rb +17 -0
  110. data/lib/facebooker/session.rb +726 -0
  111. data/lib/facebooker/version.rb +9 -0
  112. data/lib/net/http_multipart_post.rb +123 -0
  113. data/lib/rack/facebook.rb +89 -0
  114. data/lib/tasks/facebooker.rake +18 -0
  115. data/lib/tasks/tunnel.rake +46 -0
  116. data/rails/init.rb +1 -0
  117. data/setup.rb +1585 -0
  118. data/templates/layout.erb +24 -0
  119. data/test/facebooker/adapters_test.rb +191 -0
  120. data/test/facebooker/admin_test.rb +102 -0
  121. data/test/facebooker/application_test.rb +41 -0
  122. data/test/facebooker/batch_request_test.rb +83 -0
  123. data/test/facebooker/data_test.rb +86 -0
  124. data/test/facebooker/logging_test.rb +43 -0
  125. data/test/facebooker/mobile_test.rb +45 -0
  126. data/test/facebooker/model_test.rb +133 -0
  127. data/test/facebooker/models/event_test.rb +15 -0
  128. data/test/facebooker/models/page_test.rb +46 -0
  129. data/test/facebooker/models/photo_test.rb +16 -0
  130. data/test/facebooker/models/user_test.rb +397 -0
  131. data/test/facebooker/rails/facebook_request_fix_2-3_test.rb +25 -0
  132. data/test/facebooker/rails/facebook_url_rewriting_test.rb +76 -0
  133. data/test/facebooker/rails/integration_session_test.rb +13 -0
  134. data/test/facebooker/rails/publisher_test.rb +519 -0
  135. data/test/facebooker/rails_integration_test.rb +1511 -0
  136. data/test/facebooker/server_cache_test.rb +44 -0
  137. data/test/facebooker/service_test.rb +58 -0
  138. data/test/facebooker/session_test.rb +883 -0
  139. data/test/facebooker_test.rb +951 -0
  140. data/test/net/http_multipart_post_test.rb +52 -0
  141. data/test/rack/facebook_test.rb +73 -0
  142. metadata +235 -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, :text, :created
6
+
7
+ def coordinates
8
+ [xcoord, ycoord]
9
+ end
10
+
11
+ end
12
+ end
@@ -0,0 +1,552 @@
1
+ require 'facebooker/model'
2
+ require 'facebooker/models/affiliation'
3
+ require 'facebooker/models/work_info'
4
+ require 'facebooker/models/family_relative_info'
5
+ module Facebooker
6
+ #
7
+ # Holds attributes and behavior for a Facebook User
8
+ class User
9
+ include Model
10
+ class Status
11
+ include Model
12
+ attr_accessor :message, :time, :status_id
13
+ end
14
+ 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, :online_presence, :verified, :profile_blurb, :username, :website, :is_blocked, :family]
15
+ STANDARD_FIELDS = [:uid, :first_name, :last_name, :name, :timezone, :birthday, :sex, :affiliations, :locale, :profile_url, :proxied_email]
16
+ populating_attr_accessor(*FIELDS)
17
+ attr_reader :affiliations
18
+ populating_hash_settable_accessor :current_location, Location
19
+ populating_hash_settable_accessor :hometown_location, Location
20
+ populating_hash_settable_accessor :hs_info, EducationInfo::HighschoolInfo
21
+ populating_hash_settable_list_accessor :affiliations, Affiliation
22
+ populating_hash_settable_list_accessor :education_history, EducationInfo
23
+ populating_hash_settable_list_accessor :work_history, WorkInfo
24
+ populating_hash_settable_list_accessor :family, FamilyRelativeInfo
25
+
26
+ populating_attr_reader :status
27
+
28
+ # Can pass in these two forms:
29
+ # id, session, (optional) attribute_hash
30
+ # attribute_hash
31
+ def initialize(*args)
32
+ @friends = nil
33
+ @current_location = nil
34
+ @pic = nil
35
+ @hometown_location = nil
36
+ @populated = false
37
+ @session = nil
38
+ @id = nil
39
+ if (args.first.kind_of?(String) || args.first.kind_of?(Integer)) && args.size==1
40
+ self.uid = args.shift
41
+ @session = Session.current
42
+ elsif (args.first.kind_of?(String) || args.first.kind_of?(Integer)) && args[1].kind_of?(Session)
43
+ self.uid = args.shift
44
+ @session = args.shift
45
+ end
46
+ if args.last.kind_of?(Hash)
47
+ populate_from_hash!(args.pop)
48
+ end
49
+ end
50
+
51
+ id_is :uid
52
+ alias :facebook_id :id
53
+
54
+ # Returns a user's events, params correspond to API call parameters (except UID):
55
+ # http://wiki.developers.facebook.com/index.php/Events.get
56
+ # E.g:
57
+ # @user.events(:start_time => Time.now, :end_time => 1.month.from_now)
58
+ # # => Returns events betwen now and a month from now
59
+ def events(params={})
60
+ @events ||= {}
61
+ [:start_time,:end_time].compact.each do |key|
62
+ params[key] = params[key].to_i
63
+ end
64
+ # puts @events[params.to_s].nil?
65
+ @events[params.to_s] ||= @session.post('facebook.events.get', {:uid => self.id}.merge(params)).map do |event|
66
+ Event.from_hash(event)
67
+ end
68
+ end
69
+
70
+ # Rsvp to an event with the eid and rsvp_status which can be 'attending', 'unsure', or 'declined'.
71
+ # http://wiki.developers.facebook.com/index.php/Events.rsvp
72
+ # E.g:
73
+ # @user.rsvp_event('100321123', 'attending')
74
+ # # => Returns true if all went well
75
+ def rsvp_event(eid, rsvp_status, options = {})
76
+ result = @session.post('facebook.events.rsvp', options.merge(:eid => eid, :rsvp_status => rsvp_status))
77
+ result == '1' ? true : false
78
+ end
79
+
80
+ #
81
+ # Set the list of friends, given an array of User objects. If the list has been retrieved previously, will not set
82
+ def friends=(list_of_friends,flid=nil)
83
+ @friends_hash ||= {}
84
+ flid=cast_to_friend_list_id(flid)
85
+ #use __blank instead of nil so that this is cached
86
+ cache_key = flid||"__blank"
87
+
88
+ @friends_hash[cache_key] ||= list_of_friends
89
+ end
90
+
91
+ def cast_to_friend_list_id(flid)
92
+ case flid
93
+ when String
94
+ list=friend_lists.detect {|f| f.name==flid}
95
+ raise Facebooker::Session::InvalidFriendList unless list
96
+ list.flid
97
+ when FriendList
98
+ flid.flid
99
+ else
100
+ flid
101
+ end
102
+ end
103
+ ##
104
+ # Retrieve friends
105
+ def friends(flid = nil)
106
+ @friends_hash ||= {}
107
+ flid=cast_to_friend_list_id(flid)
108
+
109
+ #use __blank instead of nil so that this is cached
110
+ cache_key = flid||"__blank"
111
+ options = {:uid=>self.id}
112
+ options[:flid] = flid unless flid.nil?
113
+ @friends_hash[cache_key] ||= @session.post('facebook.friends.get', options,false).map do |uid|
114
+ User.new(uid, @session)
115
+ end
116
+ @friends_hash[cache_key]
117
+ end
118
+
119
+ def friend_ids
120
+ options = {:uid => self.id}
121
+ @session.post('facebook.friends.get', options, false)
122
+ end
123
+
124
+ ###
125
+ # Publish a post into the stream on the user's Wall and News Feed. This
126
+ # post also appears in the user's friend's streams. The +publish_stream+
127
+ # extended permission must be granted in order to use this method.
128
+ #
129
+ # See: http://wiki.developers.facebook.com/index.php/Stream.publish
130
+ #
131
+ # +target+ can be the current user or some other user.
132
+ #
133
+ # Example:
134
+ # # Publish a message to my own wall:
135
+ # me.publish_to(me, :message => 'hello world')
136
+ #
137
+ # # Publish to a friend's wall with an action link:
138
+ # me.publish_to(my_friend, :message => 'how are you?', :action_links => [
139
+ # :text => 'my website',
140
+ # :href => 'http://tenderlovemaking.com/'
141
+ # ])
142
+ def publish_to(target, options = {})
143
+ @session.post('facebook.stream.publish', prepare_publish_to_options(target, options), false)
144
+ end
145
+
146
+ # Prepares options for the stream.publish
147
+ def prepare_publish_to_options(target, options)
148
+ opts = {:uid => self.id,
149
+ :target_id => target.id,
150
+ :message => options[:message]}
151
+
152
+ if(attachment = options[:attachment] && Facebooker.json_encode(options[:attachment]))
153
+ opts[:attachment] = attachment
154
+ end
155
+ if (links = options[:action_links] && Facebooker.json_encode(options[:action_links]))
156
+ opts[:action_links] = links
157
+ end
158
+ opts
159
+ end
160
+
161
+ ###
162
+ # Publish a comment on a post
163
+ #
164
+ # See: http://wiki.developers.facebook.com/index.php/Stream.addComment
165
+ #
166
+ # +post_id+ the post_id for the post that is being commented on
167
+ # +comment+ the text of the comment
168
+ def comment_on(post_id, comment)
169
+ @session.post('facebook.stream.addComment', {:post_id=>post_id, :comment=>comment})
170
+ end
171
+
172
+
173
+ def friend_lists
174
+ @friend_lists ||= @session.post('facebook.friends.getLists').map do |hash|
175
+ friend_list = FriendList.from_hash(hash)
176
+ friend_list.session = session
177
+ friend_list
178
+ end
179
+ end
180
+ ###
181
+ # Retrieve friends with user info populated
182
+ # Subsequent calls will be retrieved from memory.
183
+ # Optional: list of fields to retrieve as symbols
184
+ def friends!(*fields)
185
+ @friends ||= session.post('facebook.users.getInfo', :fields => collect(fields), :uids => friends.map{|f| f.id}.join(',')).map do |hash|
186
+ User.new(hash['uid'], session, hash)
187
+ end
188
+ end
189
+
190
+ ###
191
+ # Retrieve profile data for logged in user
192
+ # Optional: list of fields to retrieve as symbols
193
+ def populate(*fields)
194
+ session.post('facebook.users.getInfo', :fields => collect(fields), :uids => id) do |response|
195
+ populate_from_hash!(response.first)
196
+ end
197
+ end
198
+
199
+ def friends_with?(user_or_id)
200
+ friends.map{|f| f.to_i}.include?(user_or_id.to_i)
201
+ end
202
+
203
+ def friends_with_this_app
204
+ @friends_with_this_app ||= friend_ids_with_this_app.map do |uid|
205
+ User.new(uid, session)
206
+ end
207
+ end
208
+
209
+ def friend_ids_with_this_app
210
+ @friend_ids_with_this_app ||= session.post('facebook.friends.getAppUsers')
211
+ end
212
+
213
+ def groups(gids = [])
214
+ args = gids.empty? ? {} : {:gids => gids}
215
+ @groups ||= session.post('facebook.groups.get', args).map do |hash|
216
+ group = Group.from_hash(hash)
217
+ group.session = session
218
+ group
219
+ end
220
+ end
221
+
222
+ def notifications
223
+ @notifications ||= Notifications.from_hash(session.post('facebook.notifications.get'))
224
+ end
225
+
226
+ def publish_story(story)
227
+ publish(story)
228
+ end
229
+
230
+ def publish_action(action)
231
+ publish(action)
232
+ end
233
+
234
+ def publish_templatized_action(action)
235
+ publish(action)
236
+ end
237
+
238
+ def albums
239
+ @albums ||= session.post('facebook.photos.getAlbums', :uid => self.id) do |response|
240
+ response.map do |hash|
241
+ Album.from_hash(hash)
242
+ end
243
+ end
244
+ end
245
+
246
+ def create_album(params)
247
+ @album = session.post('facebook.photos.createAlbum', params) {|response| Album.from_hash(response)}
248
+ end
249
+
250
+ def profile_photos
251
+ session.get_photos(nil, nil, profile_pic_album_id)
252
+ end
253
+
254
+ # Upload a photo to the user's profile.
255
+ #
256
+ # In your view, create a multipart form that posts directly to your application (not through canvas):
257
+ #
258
+ # <% form_tag photos_url(:canvas => false), :html => {:multipart => true, :promptpermission => 'photo_upload'} do %>
259
+ # Photo: <%= file_field_tag 'photo' %>
260
+ # Caption: <%= text_area_tag 'caption' %>
261
+ # <%= submit_tag 'Upload Photo', :class => 'inputsubmit' %>
262
+ # <% end %>
263
+ #
264
+ # And in your controller:
265
+ #
266
+ # class PhotosController < ApplicationController
267
+ # def create
268
+ # file = Net::HTTP::MultipartPostFile.new(
269
+ # params[:photo].original_filename,
270
+ # params[:photo].content_type,
271
+ # params[:photo].read
272
+ # )
273
+ #
274
+ # @photo = facebook_session.user.upload_photo(file, :caption => params[:caption])
275
+ # redirect_to photos_url(:canvas => true)
276
+ # end
277
+ # end
278
+ #
279
+ # Options correspond to http://wiki.developers.facebook.com/index.php/Photos.upload
280
+ def upload_photo(multipart_post_file, options = {})
281
+ Photo.from_hash(session.post_file('facebook.photos.upload',
282
+ options.merge(nil => multipart_post_file)))
283
+ end
284
+
285
+ # Upload a video to the user's profile.
286
+ #
287
+ # In your view, create a multipart form that posts directly to your application (not through canvas):
288
+ #
289
+ # <% form_tag videos_url(:canvas => false), :html => {:multipart => true, :promptpermission => 'video_upload'} do %>
290
+ # Video: <%= file_field_tag 'video' %>
291
+ # Title: <%= text_area_tag 'title' %>
292
+ # Description: <%= text_area_tag 'description' %>
293
+ # <%= submit_tag 'Upload Video', :class => 'inputsubmit' %>
294
+ # <% end %>
295
+ #
296
+ # And in your controller:
297
+ #
298
+ # class VideosController < ApplicationController
299
+ # def create
300
+ # file = Net::HTTP::MultipartPostFile.new(
301
+ # params[:photo].original_filename,
302
+ # params[:photo].content_type,
303
+ # params[:photo].read
304
+ # )
305
+ #
306
+ # @video = facebook_session.user.upload_video(file, :description => params[:description])
307
+ # redirect_to videos_url(:canvas => true)
308
+ # end
309
+ # end
310
+ #
311
+ # Options correspond to http://wiki.developers.facebook.com/index.php/Video.upload
312
+ def upload_video(multipart_post_file, options = {})
313
+ Video.from_hash(session.post_file('facebook.video.upload',
314
+ options.merge(nil => multipart_post_file, :base => Facebooker.video_server_base)))
315
+ end
316
+
317
+ def profile_fbml
318
+ session.post('facebook.profile.getFBML', :uid => id)
319
+ end
320
+
321
+ ##
322
+ # Set the profile FBML for this user
323
+ #
324
+ # This does not set profile actions, that should be done with profile_action=
325
+ def profile_fbml=(markup)
326
+ set_profile_fbml(markup, nil, nil, nil)
327
+ end
328
+
329
+ ##
330
+ # Set the mobile profile FBML
331
+ def mobile_fbml=(markup)
332
+ set_profile_fbml(nil, markup, nil,nil)
333
+ end
334
+
335
+ def profile_action=(markup)
336
+ set_profile_fbml(nil, nil, markup,nil)
337
+ end
338
+
339
+ def profile_main=(markup)
340
+ set_profile_fbml(nil,nil,nil,markup)
341
+ end
342
+
343
+ def set_profile_fbml(profile_fbml, mobile_fbml, profile_action_fbml, profile_main = nil)
344
+ parameters = {:uid => id}
345
+ parameters[:profile] = profile_fbml if profile_fbml
346
+ parameters[:profile_action] = profile_action_fbml if profile_action_fbml
347
+ parameters[:mobile_profile] = mobile_fbml if mobile_fbml
348
+ parameters[:profile_main] = profile_main if profile_main
349
+ session.post('facebook.profile.setFBML', parameters,false)
350
+ end
351
+
352
+ ## ** NEW PROFILE DESIGN ***
353
+ # Set a info section for this user
354
+ #
355
+ # Note: using set_profile_info as I feel using user.set_info could be confused with the user.getInfo facebook method.
356
+ # Also, I feel it fits in line with user.set_profile_fbml.
357
+ def set_profile_info(title, info_fields, format = :text)
358
+ session.post('facebook.profile.setInfo', :title => title, :uid => id,
359
+ :type => format.to_s == "text" ? 1 : 5, :info_fields => info_fields.to_json)
360
+ end
361
+
362
+ def get_profile_info
363
+ session.post('facebook.profile.getInfo', :uid => id)
364
+ end
365
+
366
+ ##
367
+ # This DOES NOT set the status of a user on Facebook
368
+ # Use the set_status method instead
369
+ def status=(message)
370
+ case message
371
+ when String,Status
372
+ @status = message
373
+ when Hash
374
+ @status = Status.from_hash(message)
375
+ end
376
+ end
377
+
378
+
379
+ ##
380
+ # Return +limit+ statuses from the user
381
+ def statuses( limit = 50 )
382
+ session.post('facebook.status.get', {:uid => uid, :limit => limit}).collect { |ret| Status.from_hash(ret) }
383
+ end
384
+
385
+ ##
386
+ # Set the status for a user
387
+ # DOES NOT prepend "is" to the message
388
+ #
389
+ # requires extended permission.
390
+ def set_status(message)
391
+ self.status=message
392
+ session.post('facebook.users.setStatus',{:status=>message,:status_includes_verb=>1,:uid => uid}, false) do |ret|
393
+ ret
394
+ end
395
+ end
396
+
397
+ ##
398
+ # Checks to see if the user has enabled the given extended permission
399
+ def has_permission?(ext_perm) # ext_perm = email, offline_access, status_update, photo_upload, create_listing, create_event, rsvp_event, sms
400
+ session.post('facebook.users.hasAppPermission', {:ext_perm=>ext_perm, :uid => uid}, false) == "1"
401
+ end
402
+
403
+ ##
404
+ # Convenience method to check multiple permissions at once
405
+ def has_permissions?(ext_perms)
406
+ ext_perms.all?{|p| has_permission?(p)}
407
+ end
408
+
409
+ ##
410
+ # Convenience method to send email to the current user
411
+ def send_email(subject, text=nil, fbml=nil)
412
+ session.send_email([id], subject, text, fbml)
413
+ end
414
+
415
+ ##
416
+ # Convenience method to set cookie for the current user
417
+ def set_cookie(name, value, expires=nil, path=nil)
418
+ session.data.set_cookie(id, name, value, expires, path)
419
+ end
420
+
421
+ ##
422
+ # Convenience method to get cookies for the current user
423
+ def get_cookies(name=nil)
424
+ session.data.get_cookies(id, name)
425
+ end
426
+
427
+ ##
428
+ # Returns the user's id as an integer
429
+ def to_i
430
+ id
431
+ end
432
+
433
+ def to_s
434
+ id.to_s
435
+ end
436
+
437
+ ##
438
+ # Two Facebooker::User objects should be considered equal if their Facebook ids are equal
439
+ def ==(other_user)
440
+ other_user.is_a?(User) && id == other_user.id
441
+ end
442
+
443
+
444
+ # register a user with Facebook
445
+ # users should be a hast with at least an :email field
446
+ # you can optionally provide an :account_id field as well
447
+
448
+ def self.register(users)
449
+ user_map={}
450
+ users=users.map do |h|
451
+ returning h.dup do |d|
452
+ if email=d.delete(:email)
453
+ hash = hash_email(email)
454
+ user_map[hash]=h
455
+ d[:email_hash]=hash
456
+ end
457
+ end
458
+ end
459
+ Facebooker::Session.create.post("facebook.connect.registerUsers",:accounts=>users.to_json) do |ret|
460
+ ret.each do |hash|
461
+ user_map.delete(hash)
462
+ end
463
+ unless user_map.empty?
464
+ e=Facebooker::Session::UserRegistrationFailed.new
465
+ e.failed_users = user_map.values
466
+ raise e
467
+ end
468
+ ret
469
+ end
470
+ end
471
+
472
+ # Get a count of unconnected friends
473
+ def getUnconnectedFriendsCount
474
+ session.post("facebook.connect.getUnconnectedFriendsCount")
475
+ end
476
+
477
+
478
+ # Unregister an array of email hashes
479
+ def self.unregister(email_hashes)
480
+ Facebooker::Session.create.post("facebook.connect.unregisterUsers",:email_hashes=>email_hashes.to_json) do |ret|
481
+ ret.each do |hash|
482
+ email_hashes.delete(hash)
483
+ end
484
+ unless email_hashes.empty?
485
+ e=Facebooker::Session::UserUnRegistrationFailed.new
486
+ e.failed_users = email_hashes
487
+ raise e
488
+ end
489
+ ret
490
+ end
491
+ end
492
+
493
+ # unregister an array of email addresses
494
+ def self.unregister_emails(emails)
495
+ emails_hash = {}
496
+ emails.each {|e| emails_hash[hash_email(e)] = e}
497
+ begin
498
+ unregister(emails_hash.keys).collect {|r| emails_hash[r]}
499
+ rescue
500
+ # re-raise with emails instead of hashes.
501
+ e = Facebooker::Session::UserUnRegistrationFailed.new
502
+ e.failed_users = $!.failed_users.collect { |f| emails_hash[f] }
503
+ raise e
504
+ end
505
+ end
506
+
507
+ def self.hash_email(email)
508
+ email = email.downcase.strip
509
+ crc=Zlib.crc32(email)
510
+ md5=Digest::MD5.hexdigest(email)
511
+ "#{crc}_#{md5}"
512
+ end
513
+
514
+ def self.cast_to_facebook_id(object)
515
+ if object.respond_to?(:facebook_id)
516
+ object.facebook_id
517
+ else
518
+ object
519
+ end
520
+ end
521
+
522
+ def self.user_fields(fields = [])
523
+ valid_fields(fields)
524
+ end
525
+
526
+ def self.standard_fields(fields = [])
527
+ valid_fields(fields,STANDARD_FIELDS)
528
+ end
529
+
530
+ private
531
+ def publish(feed_story_or_action)
532
+ session.post(Facebooker::Feed::METHODS[feed_story_or_action.class.name.split(/::/).last], feed_story_or_action.to_params) == "1" ? true : false
533
+ end
534
+
535
+ def self.valid_fields(fields, allowable=FIELDS)
536
+ allowable.reject{|field_name| !fields.empty? && !fields.include?(field_name)}.join(',')
537
+ end
538
+
539
+ def collect(fields, allowable=FIELDS)
540
+ allowable.reject{|field_name| !fields.empty? && !fields.include?(field_name)}.join(',')
541
+ end
542
+
543
+ def profile_pic_album_id
544
+ merge_aid(-3, id)
545
+ end
546
+
547
+ def merge_aid(aid, uid)
548
+ (uid << 32) + (aid & 0xFFFFFFFF)
549
+ end
550
+
551
+ end
552
+ end