facebooker-fbg1 1.0.61.1

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