facebooker-micah 1.0.74

Sign up to get free protection for your applications and to get access to all the features.
Files changed (156) hide show
  1. data/.autotest +15 -0
  2. data/CHANGELOG.rdoc +24 -0
  3. data/COPYING.rdoc +19 -0
  4. data/Manifest.txt +152 -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 +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/templates/xd_receiver_ssl.html +10 -0
  43. data/generators/xd_receiver/xd_receiver_generator.rb +10 -0
  44. data/init.rb +28 -0
  45. data/install.rb +12 -0
  46. data/lib/facebooker.rb +261 -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 +37 -0
  52. data/lib/facebooker/attachment.rb +59 -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/comment.rb +9 -0
  66. data/lib/facebooker/models/cookie.rb +10 -0
  67. data/lib/facebooker/models/education_info.rb +11 -0
  68. data/lib/facebooker/models/event.rb +28 -0
  69. data/lib/facebooker/models/family_relative_info.rb +7 -0
  70. data/lib/facebooker/models/friend_list.rb +16 -0
  71. data/lib/facebooker/models/group.rb +36 -0
  72. data/lib/facebooker/models/info_item.rb +10 -0
  73. data/lib/facebooker/models/info_section.rb +10 -0
  74. data/lib/facebooker/models/location.rb +8 -0
  75. data/lib/facebooker/models/message_thread.rb +89 -0
  76. data/lib/facebooker/models/notifications.rb +17 -0
  77. data/lib/facebooker/models/page.rb +46 -0
  78. data/lib/facebooker/models/photo.rb +19 -0
  79. data/lib/facebooker/models/tag.rb +12 -0
  80. data/lib/facebooker/models/user.rb +761 -0
  81. data/lib/facebooker/models/video.rb +9 -0
  82. data/lib/facebooker/models/work_info.rb +10 -0
  83. data/lib/facebooker/parser.rb +980 -0
  84. data/lib/facebooker/rails/backwards_compatible_param_checks.rb +31 -0
  85. data/lib/facebooker/rails/controller.rb +353 -0
  86. data/lib/facebooker/rails/cucumber.rb +28 -0
  87. data/lib/facebooker/rails/cucumber/world.rb +40 -0
  88. data/lib/facebooker/rails/extensions/action_controller.rb +48 -0
  89. data/lib/facebooker/rails/extensions/rack_setup.rb +16 -0
  90. data/lib/facebooker/rails/extensions/routing.rb +15 -0
  91. data/lib/facebooker/rails/facebook_form_builder.rb +141 -0
  92. data/lib/facebooker/rails/facebook_pretty_errors.rb +22 -0
  93. data/lib/facebooker/rails/facebook_request_fix.rb +28 -0
  94. data/lib/facebooker/rails/facebook_request_fix_2-3.rb +31 -0
  95. data/lib/facebooker/rails/facebook_session_handling.rb +68 -0
  96. data/lib/facebooker/rails/facebook_url_helper.rb +192 -0
  97. data/lib/facebooker/rails/facebook_url_rewriting.rb +60 -0
  98. data/lib/facebooker/rails/helpers.rb +835 -0
  99. data/lib/facebooker/rails/helpers/fb_connect.rb +165 -0
  100. data/lib/facebooker/rails/helpers/stream_publish.rb +22 -0
  101. data/lib/facebooker/rails/integration_session.rb +38 -0
  102. data/lib/facebooker/rails/profile_publisher_extensions.rb +42 -0
  103. data/lib/facebooker/rails/publisher.rb +608 -0
  104. data/lib/facebooker/rails/routing.rb +49 -0
  105. data/lib/facebooker/rails/test_helpers.rb +68 -0
  106. data/lib/facebooker/rails/utilities.rb +22 -0
  107. data/lib/facebooker/server_cache.rb +24 -0
  108. data/lib/facebooker/service.rb +103 -0
  109. data/lib/facebooker/service/base_service.rb +19 -0
  110. data/lib/facebooker/service/curl_service.rb +44 -0
  111. data/lib/facebooker/service/net_http_service.rb +12 -0
  112. data/lib/facebooker/service/typhoeus_multi_service.rb +27 -0
  113. data/lib/facebooker/service/typhoeus_service.rb +17 -0
  114. data/lib/facebooker/session.rb +788 -0
  115. data/lib/facebooker/stream_post.rb +19 -0
  116. data/lib/facebooker/version.rb +9 -0
  117. data/lib/net/http_multipart_post.rb +123 -0
  118. data/lib/rack/facebook.rb +89 -0
  119. data/lib/rack/facebook_session.rb +21 -0
  120. data/lib/tasks/facebooker.rake +19 -0
  121. data/lib/tasks/facebooker.rb +2 -0
  122. data/lib/tasks/tunnel.rake +46 -0
  123. data/rails/init.rb +1 -0
  124. data/setup.rb +1585 -0
  125. data/templates/layout.erb +24 -0
  126. data/test/facebooker/adapters_test.rb +191 -0
  127. data/test/facebooker/admin_test.rb +102 -0
  128. data/test/facebooker/application_test.rb +110 -0
  129. data/test/facebooker/attachment_test.rb +72 -0
  130. data/test/facebooker/batch_request_test.rb +83 -0
  131. data/test/facebooker/data_test.rb +86 -0
  132. data/test/facebooker/logging_test.rb +43 -0
  133. data/test/facebooker/mobile_test.rb +45 -0
  134. data/test/facebooker/model_test.rb +133 -0
  135. data/test/facebooker/models/event_test.rb +15 -0
  136. data/test/facebooker/models/page_test.rb +56 -0
  137. data/test/facebooker/models/photo_test.rb +16 -0
  138. data/test/facebooker/models/user_test.rb +1087 -0
  139. data/test/facebooker/rails/facebook_request_fix_2-3_test.rb +25 -0
  140. data/test/facebooker/rails/facebook_url_rewriting_test.rb +76 -0
  141. data/test/facebooker/rails/integration_session_test.rb +13 -0
  142. data/test/facebooker/rails/publisher_test.rb +538 -0
  143. data/test/facebooker/rails_integration_test.rb +1543 -0
  144. data/test/facebooker/server_cache_test.rb +44 -0
  145. data/test/facebooker/service_test.rb +58 -0
  146. data/test/facebooker/session_test.rb +883 -0
  147. data/test/facebooker_test.rb +1263 -0
  148. data/test/fixtures/multipart_post_body_with_only_parameters.txt +33 -0
  149. data/test/fixtures/multipart_post_body_with_single_file.txt +38 -0
  150. data/test/fixtures/multipart_post_body_with_single_file_that_has_nil_key.txt +38 -0
  151. data/test/net/http_multipart_post_test.rb +52 -0
  152. data/test/rack/facebook_session_test.rb +34 -0
  153. data/test/rack/facebook_test.rb +73 -0
  154. data/test/rails_test_helper.rb +36 -0
  155. data/test/test_helper.rb +74 -0
  156. metadata +300 -0
@@ -0,0 +1,11 @@
1
+ module Facebooker
2
+ class EducationInfo
3
+ class HighschoolInfo
4
+ include Model
5
+ attr_accessor :hs1_id, :hs2_id, :grad_year, :hs1_name, :hs2_name
6
+ end
7
+
8
+ include Model
9
+ attr_accessor :concentrations, :name, :year, :degree, :school_type
10
+ end
11
+ end
@@ -0,0 +1,28 @@
1
+ require 'facebooker/model'
2
+ module Facebooker
3
+ class Event
4
+
5
+ ##
6
+ # The relationship between a Facebook user and an Event to which he or she has been
7
+ # invited and may or may not be attending (based on #rsvp_status)
8
+ class Attendance
9
+ include Model
10
+ attr_accessor :eid, :uid, :rsvp_status
11
+
12
+ ##
13
+ # Get the full, populated Event object which this Attendance is associated with.
14
+ # First access will query the Facebook API (facebook.events.get). Subsequent
15
+ # calls are retrieved from in-memory cache.
16
+ def event
17
+ @event ||= Event.from_hash(session.post('facebook.events.get', :eids => [eid]).first)
18
+ end
19
+
20
+ #TODO: implement user() method
21
+ end
22
+
23
+ include Model
24
+ attr_accessor :pic, :pic_small, :pic_big, :name, :creator, :update_time, :description, :tagline, :venue, :host, :event_type, :nid, :location, :end_time, :start_time, :event_subtype
25
+
26
+ id_is :eid
27
+ end
28
+ end
@@ -0,0 +1,7 @@
1
+ require 'facebooker/model'
2
+ module Facebooker
3
+ class FamilyRelativeInfo
4
+ include Model
5
+ attr_accessor :relationship, :uid, :name, :birthday
6
+ end
7
+ end
@@ -0,0 +1,16 @@
1
+ require 'facebooker/model'
2
+ module Facebooker
3
+ ##
4
+ # A simple representation of a friend list.
5
+ class FriendList
6
+ include Model
7
+ attr_accessor :name
8
+
9
+ id_is :flid
10
+
11
+ # We need this to be an integer, so do the conversion
12
+ def flid=(f)
13
+ @flid= ( f.nil? ? nil : f.to_i)
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,36 @@
1
+ require 'facebooker/model'
2
+ module Facebooker
3
+ class Group
4
+ ##
5
+ # The model of a user's relationship to a group. Users can occupy different positions within a group (e.g. 'owner')
6
+ class Membership
7
+ include Model
8
+ attr_accessor :position, :gid, :uid
9
+ end
10
+ include Model
11
+ attr_accessor :pic, :pic_small, :pic_big, :name, :creator, :recent_news, :update_time, :group_subtype, :group_type, :website, :office, :description, :venue, :nid, :privacy
12
+
13
+ id_is :gid
14
+
15
+ ##
16
+ # Get the full list of members as populated User objects. First time fetches group members via Facebook API call.
17
+ # Subsequent calls return cached values.
18
+ # This is a convenience method for getting all of the Membership instances and instantiating User instances for each Membership.
19
+ def members
20
+ @members ||= memberships.map do |membership|
21
+ User.new(membership.uid, session)
22
+ end
23
+ end
24
+
25
+ ##
26
+ # Get a list of Membership instances associated with this Group. First call retrieves the Membership instances via a Facebook
27
+ # API call. Subsequent calls are retrieved from in-memory cache.
28
+ def memberships
29
+ @memberships ||= session.post('facebook.groups.getMembers', :gid => gid).map do |hash|
30
+ Membership.from_hash(hash) do |membership|
31
+ membership.gid = gid
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -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,89 @@
1
+ require 'facebooker/model'
2
+ module Facebooker
3
+ class MessageThread
4
+ include Model
5
+
6
+ id_is :thread_id
7
+ attr_accessor :subject, :updated_time, :recipients
8
+ attr_accessor :parent_message_id, :parent_thread_id, :message_count
9
+ attr_accessor :snippet, :snippet_author, :object_id, :unread
10
+
11
+ class Message
12
+ include Model
13
+
14
+ attr_accessor :message_id, :author_id, :body, :created_time, :attachment, :thread_id
15
+
16
+ # An attachment can be a photo, a video, or a link
17
+ class Attachment
18
+ include Model
19
+
20
+ attr_accessor :name, :href, :icon, :caption, :description
21
+
22
+ # The Facebook messages API is in beta, this helper method is supposed to fail anytime soon
23
+ def video?
24
+ self.href =~ /\Ahttp:\/\/www\.facebook\.com\/video\/video\.php.*/
25
+ end
26
+
27
+ # The Facebook messages API is in beta, this helper method is supposed to fail anytime soon
28
+ def photo?
29
+ self.href =~ /\Ahttp:\/\/www\.facebook\.com\/photo\.php.*/
30
+ end
31
+
32
+ # The Facebook messages API is in beta, this helper method is supposed to fail anytime soon
33
+ def link?
34
+ !video? && !photo?
35
+ end
36
+ end
37
+ hash_settable_accessor :attachment, Facebooker::MessageThread::Message::Attachment
38
+ end
39
+ hash_settable_list_accessor :messages, Facebooker::MessageThread::Message
40
+
41
+ end
42
+ end
43
+
44
+ # Example of attachments
45
+
46
+ # -- Photo --
47
+
48
+ # <attachment>
49
+ # <media list="true"/>
50
+ # <href>http://www.facebook.com/photo.php?pid=12345&amp;id=54321</href>
51
+ # <properties list="true"/>
52
+ # <icon>http://b.static.ak.fbcdn.net/rsrc.php/zB010/hash/9yvl71tw.gif</icon>
53
+ # <fb_object_type/>
54
+ # <fb_object_id/>
55
+ # <tagged_ids list="true"/>
56
+ # </attachment>
57
+
58
+ # -- Webcam video --
59
+
60
+ # <attachment>
61
+ # <media list="true"/>
62
+ # <name>Feb 10, 2010 1:26pm</name>
63
+ # <href>http://www.facebook.com/video/video.php?v=12345</href>
64
+ # <properties list="true"/>
65
+ # <icon>http://static.ak.fbcdn.net/rsrc.php/zB010/hash/9yvl71tw.gif</icon>
66
+ # <fb_object_type/>
67
+ # <fb_object_id/>
68
+ # <tagged_ids list="true"/>
69
+ # </attachment>
70
+
71
+ # -- Link --
72
+
73
+ # <attachment>
74
+ # <media list="true">
75
+ # <stream_media>
76
+ # <href>http://www.facebook.com/l.php?u=http%253A%252F%252Fwww.google.fr%252F&amp;h=e46dd63cdbfadb74958fbe44e98f339c</href>
77
+ # <type>link</type>
78
+ # <src>http://external.ak.fbcdn.net/safe_image.php?d=dd54bba6b6e6479a89bb8084573c02c8&amp;w=90&amp;h=90&amp;url=http%3A%2F%2Fwww.google.fr%2Fintl%2Ffr_fr%2Fimages%2Flogo.gif</src>
79
+ # </stream_media>
80
+ # </media>
81
+ # <name>Google</name>
82
+ # <href>http://www.facebook.com/l.php?u=http%253A%252F%252Fwww.google.fr%252F&amp;h=e46dd63cdbfadb74958fbe44e98f339c</href>
83
+ # <caption>www.google.fr</caption>
84
+ # <properties list="true"/>
85
+ # <icon>http://b.static.ak.fbcdn.net/rsrc.php/zB010/hash/9yvl71tw.gif</icon>
86
+ # <fb_object_type/>
87
+ # <fb_object_id/>
88
+ # <tagged_ids list="true"/>
89
+ # </attachment>
@@ -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,46 @@
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,:page_url
30
+ attr_reader :genre
31
+
32
+ alias_method :id, :page_id
33
+
34
+ def genre=(value)
35
+ @genre = value.kind_of?(Hash) ? Genre.from_hash(value) : value
36
+ end
37
+
38
+ def user_is_admin?(user)
39
+ Session.current.post('facebook.pages.isAdmin', :page_id=>self.page_id, :uid=>Facebooker::User.cast_to_facebook_id(user))
40
+ end
41
+
42
+ def user_is_fan?(user)
43
+ Session.current.post('facebook.pages.isFan', :page_id=>self.page_id, :uid=>Facebooker::User.cast_to_facebook_id(user))
44
+ end
45
+ end
46
+ 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,761 @@
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 :uid, :message, :time, :status_id, :source
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
+ attr_accessor :request_locale
29
+
30
+ # Can pass in these two forms:
31
+ # id, session, (optional) attribute_hash
32
+ # attribute_hash
33
+ def initialize(*args)
34
+ @friends = nil
35
+ @current_location = nil
36
+ @pic = nil
37
+ @hometown_location = nil
38
+ @populated = false
39
+ @session = nil
40
+ @id = nil
41
+ if (args.first.kind_of?(String) || args.first.kind_of?(Integer)) && args.size==1
42
+ self.uid = args.shift
43
+ @session = Session.current
44
+ elsif (args.first.kind_of?(String) || args.first.kind_of?(Integer)) && args[1].kind_of?(Session)
45
+ self.uid = args.shift
46
+ @session = args.shift
47
+ end
48
+ if args.last.kind_of?(Hash)
49
+ populate_from_hash!(args.pop)
50
+ end
51
+ end
52
+
53
+ id_is :uid
54
+ alias :facebook_id :id
55
+
56
+ # Returns a user's events, params correspond to API call parameters (except UID):
57
+ # http://wiki.developers.facebook.com/index.php/Events.get
58
+ # E.g:
59
+ # @user.events(:start_time => Time.now, :end_time => 1.month.from_now)
60
+ # # => Returns events betwen now and a month from now
61
+ def events(params={})
62
+ @events ||= {}
63
+ [:start_time,:end_time].compact.each do |key|
64
+ params[key] = params[key].to_i
65
+ end
66
+ # puts @events[params.to_s].nil?
67
+ @events[params.to_s] ||= @session.post('facebook.events.get', {:uid => self.id}.merge(params)).map do |event|
68
+ Event.from_hash(event)
69
+ end
70
+ end
71
+
72
+ # Rsvp to an event with the eid and rsvp_status which can be 'attending', 'unsure', or 'declined'.
73
+ # http://wiki.developers.facebook.com/index.php/Events.rsvp
74
+ # E.g:
75
+ # @user.rsvp_event('100321123', 'attending')
76
+ # # => Returns true if all went well
77
+ def rsvp_event(eid, rsvp_status, options = {})
78
+ result = @session.post('facebook.events.rsvp', options.merge(:eid => eid, :rsvp_status => rsvp_status))
79
+ result == '1' ? true : false
80
+ end
81
+
82
+ #
83
+ # Set the list of friends, given an array of User objects. If the list has been retrieved previously, will not set
84
+ def friends=(list_of_friends,flid=nil)
85
+ @friends_hash ||= {}
86
+ flid=cast_to_friend_list_id(flid)
87
+ #use __blank instead of nil so that this is cached
88
+ cache_key = flid||"__blank"
89
+
90
+ @friends_hash[cache_key] ||= list_of_friends
91
+ end
92
+
93
+ def cast_to_friend_list_id(flid)
94
+ case flid
95
+ when String
96
+ list=friend_lists.detect {|f| f.name==flid}
97
+ raise Facebooker::Session::InvalidFriendList unless list
98
+ list.flid
99
+ when FriendList
100
+ flid.flid
101
+ else
102
+ flid
103
+ end
104
+ end
105
+ ##
106
+ # Retrieve friends
107
+ def friends(flid = nil)
108
+ @friends_hash ||= {}
109
+ flid=cast_to_friend_list_id(flid)
110
+
111
+ #use __blank instead of nil so that this is cached
112
+ cache_key = flid||"__blank"
113
+ options = {:uid=>self.id}
114
+ options[:flid] = flid unless flid.nil?
115
+ @friends_hash[cache_key] ||= @session.post('facebook.friends.get', options,false).map do |uid|
116
+ User.new(uid, @session)
117
+ end
118
+ @friends_hash[cache_key]
119
+ end
120
+
121
+ def friend_ids
122
+ options = {:uid => self.id}
123
+ @session.post('facebook.friends.get', options, false)
124
+ end
125
+
126
+ ###
127
+ # Publish a post into the stream on the user's Wall and News Feed. This
128
+ # post also appears in the user's friend's streams. The +publish_stream+
129
+ # extended permission must be granted in order to use this method.
130
+ #
131
+ # See: http://wiki.developers.facebook.com/index.php/Stream.publish
132
+ #
133
+ # +target+ can be the current user or some other user.
134
+ #
135
+ # To publish to a Page on the Page's behave, specify the page id as
136
+ # :uid and set :post_as_page to 'true', use the current user as target
137
+ #
138
+ # Example:
139
+ # # Publish a message to my own wall:
140
+ # me.publish_to(me, :message => 'hello world')
141
+ #
142
+ # # Publish to a friend's wall with an action link:
143
+ # me.publish_to(my_friend, :message => 'how are you?', :action_links => [
144
+ # :text => 'my website',
145
+ # :href => 'http://tenderlovemaking.com/'
146
+ # ])
147
+ def publish_to(target, options = {})
148
+ @session.post('facebook.stream.publish', prepare_publish_to_options(target, options), false)
149
+ end
150
+
151
+ # Prepares options for the stream.publish
152
+ def prepare_publish_to_options(target, options)
153
+ opts = {:uid => self.id,
154
+ :target_id => target.id,
155
+ :message => options[:message]}
156
+
157
+ if a = options[:attachment]
158
+ opts[:attachment] = convert_attachment_to_json(a)
159
+ end
160
+ if (links = options[:action_links] && Facebooker.json_encode(options[:action_links]))
161
+ opts[:action_links] = links
162
+ end
163
+ unless options[:uid].nil?
164
+ opts[:uid] = options[:uid]
165
+ end
166
+ if options[:post_as_page]
167
+ opts.delete(:target_id)
168
+ end
169
+ opts
170
+ end
171
+
172
+ def convert_attachment_to_json(attachment)
173
+ a = attachment.respond_to?(:to_hash) ? attachment.to_hash : attachment
174
+ Facebooker.json_encode(a)
175
+ end
176
+
177
+ ###
178
+ # Publish a comment on a post
179
+ #
180
+ # See: http://wiki.developers.facebook.com/index.php/Stream.addComment
181
+ #
182
+ # +post_id+ the post_id for the post that is being commented on
183
+ # +comment+ the text of the comment
184
+ def comment_on(post_id, comment)
185
+ @session.post('facebook.stream.addComment', {:post_id=>post_id, :comment=>comment})
186
+ end
187
+
188
+
189
+ ###
190
+ # Publish a comment to a specific comment set by xid
191
+ #
192
+ # See: http://wiki.developers.facebook.com/index.php/Comments.add
193
+ #
194
+ # +xid+ the xid for the set of comments
195
+ # +text+ the text of the comment
196
+ def add_comment(xid, text,title=nil,url=nil,publish_to_stream=false)
197
+ @session.post('facebook.comments.add',{:xid=>xid,:text=>text,:title=>title,:url=>url,:publish_to_stream=>publish_to_stream})
198
+ end
199
+
200
+ ###
201
+ # Add a like on a post
202
+ #
203
+ # See: http://wiki.developers.facebook.com/index.php/Stream.addLike
204
+ #
205
+ # +post_id+ the post_id for the post that is being commented on
206
+ def add_like_on(post_id)
207
+ @session.post('facebook.stream.addLike', {:post_id=>post_id})
208
+ end
209
+
210
+ ###
211
+ # Remove a like on a post
212
+ #
213
+ # See: http://wiki.developers.facebook.com/index.php/Stream.removeLike
214
+ #
215
+ # +post_id+ the post_id for the post that is being commented on
216
+ def remove_like_on(post_id)
217
+ @session.post('facebook.stream.removeLike', {:post_id=>post_id})
218
+ end
219
+
220
+ def friend_lists
221
+ @friend_lists ||= @session.post('facebook.friends.getLists').map do |hash|
222
+ friend_list = FriendList.from_hash(hash)
223
+ friend_list.session = session
224
+ friend_list
225
+ end
226
+ end
227
+ ###
228
+ # Retrieve friends with user info populated
229
+ # Subsequent calls will be retrieved from memory.
230
+ # Optional: list of fields to retrieve as symbols
231
+ def friends!(*fields)
232
+ @friends ||= session.post('facebook.users.getInfo', :fields => collect(fields), :uids => friends.map{|f| f.id}.join(',')).map do |hash|
233
+ User.new(hash['uid'], session, hash)
234
+ end
235
+ end
236
+
237
+ ###
238
+ # Retrieve profile data for logged in user
239
+ # Optional: list of fields to retrieve as symbols
240
+ def populate(*fields)
241
+ arguments = {:fields => collect(fields), :uids => id}
242
+ arguments[:locale]=request_locale unless request_locale.nil?
243
+ session.post('facebook.users.getInfo', arguments) do |response|
244
+ populate_from_hash!(response.first)
245
+ end
246
+ end
247
+
248
+ def friends_with?(user_or_id)
249
+ friends.map{|f| f.to_i}.include?(user_or_id.to_i)
250
+ end
251
+
252
+ def friends_with_this_app
253
+ @friends_with_this_app ||= friend_ids_with_this_app.map do |uid|
254
+ User.new(uid, session)
255
+ end
256
+ end
257
+
258
+ def friend_ids_with_this_app
259
+ @friend_ids_with_this_app ||= session.post('facebook.friends.getAppUsers')
260
+ end
261
+
262
+ def groups(gids = [])
263
+ args = gids.empty? ? {} : {:gids => gids}
264
+ @groups ||= session.post('facebook.groups.get', args).map do |hash|
265
+ group = Group.from_hash(hash)
266
+ group.session = session
267
+ group
268
+ end
269
+ end
270
+
271
+ ###
272
+ # Get threads in a folder
273
+ #
274
+ # See: http://wiki.developers.facebook.com/index.php/Message.getThreadsInFolder
275
+ #
276
+ # +options+ possible options are :folder_id, :limit and :offset
277
+ def threads(options = {})
278
+ options ||= {}
279
+ @threads = session.post('facebook.message.getThreadsInFolder', options) do |response|
280
+ response.map do |hash|
281
+ MessageThread.from_hash(hash)
282
+ end
283
+ end
284
+ end
285
+
286
+ def notifications
287
+ @notifications ||= Notifications.from_hash(session.post('facebook.notifications.get'))
288
+ end
289
+
290
+ def publish_story(story)
291
+ publish(story)
292
+ end
293
+
294
+ def publish_action(action)
295
+ publish(action)
296
+ end
297
+
298
+ def publish_templatized_action(action)
299
+ publish(action)
300
+ end
301
+
302
+ def albums
303
+ @albums ||= session.post('facebook.photos.getAlbums', :uid => self.id) do |response|
304
+ response.map do |hash|
305
+ Album.from_hash(hash)
306
+ end
307
+ end
308
+ end
309
+
310
+ ###
311
+ # Retrieve user's facebook stream
312
+ # See http://wiki.developers.facebook.com/index.php/Stream.get for options
313
+ #
314
+
315
+ def stream(options = {})
316
+ @stream = session.post('facebook.stream.get', prepare_get_stream_options(options)) do |response|
317
+ response
318
+ end
319
+ end
320
+
321
+ def create_album(params)
322
+ @album = session.post('facebook.photos.createAlbum', params) {|response| Album.from_hash(response)}
323
+ end
324
+
325
+ def profile_photos
326
+ session.get_photos(nil, nil, profile_pic_album_id)
327
+ end
328
+
329
+ # Upload a photo to the user's profile.
330
+ #
331
+ # In your view, create a multipart form that posts directly to your application (not through canvas):
332
+ #
333
+ # <% form_tag photos_url(:canvas => false), :html => {:multipart => true, :promptpermission => 'photo_upload'} do %>
334
+ # Photo: <%= file_field_tag 'photo' %>
335
+ # Caption: <%= text_area_tag 'caption' %>
336
+ # <%= submit_tag 'Upload Photo', :class => 'inputsubmit' %>
337
+ # <% end %>
338
+ #
339
+ # And in your controller:
340
+ #
341
+ # class PhotosController < ApplicationController
342
+ # def create
343
+ # file = Net::HTTP::MultipartPostFile.new(
344
+ # params[:photo].original_filename,
345
+ # params[:photo].content_type,
346
+ # params[:photo].read
347
+ # )
348
+ #
349
+ # @photo = facebook_session.user.upload_photo(file, :caption => params[:caption])
350
+ # redirect_to photos_url(:canvas => true)
351
+ # end
352
+ # end
353
+ #
354
+ # Options correspond to http://wiki.developers.facebook.com/index.php/Photos.upload
355
+ def upload_photo(multipart_post_file, options = {})
356
+ Photo.from_hash(session.post_file('facebook.photos.upload',
357
+ options.merge(nil => multipart_post_file)))
358
+ end
359
+
360
+ # Upload a video to the user's profile.
361
+ #
362
+ # In your view, create a multipart form that posts directly to your application (not through canvas):
363
+ #
364
+ # <% form_tag videos_url(:canvas => false), :html => {:multipart => true, :promptpermission => 'video_upload'} do %>
365
+ # Video: <%= file_field_tag 'video' %>
366
+ # Title: <%= text_area_tag 'title' %>
367
+ # Description: <%= text_area_tag 'description' %>
368
+ # <%= submit_tag 'Upload Video', :class => 'inputsubmit' %>
369
+ # <% end %>
370
+ #
371
+ # And in your controller:
372
+ #
373
+ # class VideosController < ApplicationController
374
+ # def create
375
+ # file = Net::HTTP::MultipartPostFile.new(
376
+ # params[:photo].original_filename,
377
+ # params[:photo].content_type,
378
+ # params[:photo].read
379
+ # )
380
+ #
381
+ # @video = facebook_session.user.upload_video(file, :description => params[:description])
382
+ # redirect_to videos_url(:canvas => true)
383
+ # end
384
+ # end
385
+ #
386
+ # Options correspond to http://wiki.developers.facebook.com/index.php/Video.upload
387
+ def upload_video(multipart_post_file, options = {})
388
+ Video.from_hash(session.post_file('facebook.video.upload',
389
+ options.merge(nil => multipart_post_file, :base => Facebooker.video_server_base)))
390
+ end
391
+
392
+ def profile_fbml
393
+ session.post('facebook.profile.getFBML', :uid => id)
394
+ end
395
+
396
+ ##
397
+ # Set the profile FBML for this user
398
+ #
399
+ # This does not set profile actions, that should be done with profile_action=
400
+ def profile_fbml=(markup)
401
+ set_profile_fbml(markup, nil, nil, nil)
402
+ end
403
+
404
+ ##
405
+ # Set the mobile profile FBML
406
+ def mobile_fbml=(markup)
407
+ set_profile_fbml(nil, markup, nil,nil)
408
+ end
409
+
410
+ def profile_action=(markup)
411
+ set_profile_fbml(nil, nil, markup,nil)
412
+ end
413
+
414
+ def profile_main=(markup)
415
+ set_profile_fbml(nil,nil,nil,markup)
416
+ end
417
+
418
+ def set_profile_fbml(profile_fbml, mobile_fbml, profile_action_fbml, profile_main = nil)
419
+ parameters = {:uid => id}
420
+ parameters[:profile] = profile_fbml if profile_fbml
421
+ parameters[:profile_action] = profile_action_fbml if profile_action_fbml
422
+ parameters[:mobile_profile] = mobile_fbml if mobile_fbml
423
+ parameters[:profile_main] = profile_main if profile_main
424
+ session.post('facebook.profile.setFBML', parameters,false)
425
+ end
426
+
427
+ ## ** NEW PROFILE DESIGN ***
428
+ # Set a info section for this user
429
+ #
430
+ # Note: using set_profile_info as I feel using user.set_info could be confused with the user.getInfo facebook method.
431
+ # Also, I feel it fits in line with user.set_profile_fbml.
432
+ def set_profile_info(title, info_fields, format = :text)
433
+ session.post('facebook.profile.setInfo', :title => title, :uid => id,
434
+ :type => format.to_s == "text" ? 1 : 5, :info_fields => info_fields.to_json)
435
+ end
436
+
437
+ def get_profile_info
438
+ session.post('facebook.profile.getInfo', :uid => id)
439
+ end
440
+
441
+ ##
442
+ # This DOES NOT set the status of a user on Facebook
443
+ # Use the set_status method instead
444
+ def status=(message)
445
+ case message
446
+ when String,Status
447
+ @status = message
448
+ when Hash
449
+ @status = Status.from_hash(message)
450
+ end
451
+ end
452
+
453
+
454
+ ##
455
+ # Return +limit+ statuses from the user
456
+ def statuses( limit = 50 )
457
+ session.post('facebook.status.get', {:uid => uid, :limit => limit}).collect { |ret| Status.from_hash(ret) }
458
+ end
459
+
460
+ ##
461
+ # Set the status for a user
462
+ # DOES NOT prepend "is" to the message
463
+ #
464
+ # requires extended permission.
465
+ def set_status(message)
466
+ self.status=message
467
+ session.post('facebook.users.setStatus',{:status=>message,:status_includes_verb=>1,:uid => uid}, false) do |ret|
468
+ ret
469
+ end
470
+ end
471
+
472
+ ##
473
+ # Checks to see if the user has enabled the given extended permission
474
+ def has_permission?(ext_perm) # ext_perm = email, offline_access, status_update, photo_upload, create_listing, create_event, rsvp_event, sms
475
+ session.post('facebook.users.hasAppPermission', {:ext_perm=>ext_perm, :uid => uid}, false) == "1"
476
+ end
477
+
478
+ ##
479
+ # Returns whether the user (either the session user or user specified by uid) has authorized the calling application
480
+ def app_user?
481
+ session.post('facebook.users.isAppUser', {:uid => self.id}, use_session_key = true)
482
+ end
483
+
484
+ ##
485
+ # Convenience method to check multiple permissions at once
486
+ def has_permissions?(ext_perms)
487
+ ext_perms.all?{|p| has_permission?(p)}
488
+ end
489
+
490
+ ##
491
+ ## Revoke any extended permission given by a user
492
+ def revoke_permission(ext_perm)
493
+ session.post('facebook.auth.revokeExtendedPermission', { :perm => ext_perm, :uid => uid }, false)
494
+ end
495
+
496
+ ##
497
+ # Convenience method to send email to the current user
498
+ def send_email(subject, text=nil, fbml=nil)
499
+ session.send_email([id], subject, text, fbml)
500
+ end
501
+
502
+ ##
503
+ # Convenience method to set cookie for the current user
504
+ def set_cookie(name, value, expires=nil, path=nil)
505
+ session.data.set_cookie(id, name, value, expires, path)
506
+ end
507
+
508
+ ##
509
+ # Convenience method to get cookies for the current user
510
+ def get_cookies(name=nil)
511
+ session.data.get_cookies(id, name)
512
+ end
513
+
514
+ ##
515
+ # Returns the user's id as an integer
516
+ def to_i
517
+ id
518
+ end
519
+
520
+ def to_s
521
+ id.to_s
522
+ end
523
+
524
+
525
+ ### NEW DASHBOARD API STUFF
526
+
527
+ # facebook_session.user.dashboard_count
528
+ def dashboard_count
529
+ session.post('facebook.dashboard.getCount', :uid => uid)
530
+ end
531
+
532
+ # facebook_session.user.dashboard_count = 5
533
+ def dashboard_count=(new_count)
534
+ session.post('facebook.dashboard.setCount', :uid => uid, :count => new_count)
535
+ end
536
+
537
+ # facebook_session.user.dashboard_increment_count
538
+ def dashboard_increment_count
539
+ session.post('facebook.dashboard.incrementCount', :uid => uid)
540
+ end
541
+
542
+ # facebook_session.user.dashboard_decrement_count
543
+ def dashboard_decrement_count
544
+ session.post('facebook.dashboard.decrementCount', :uid => uid)
545
+ end
546
+
547
+ # The following methods are not bound to a specific user but do relate to Users in general,
548
+ # so I've made them into class methods.
549
+
550
+ # Facebooker::User.dashboard_multi_get_count ['1234', '5678']
551
+ def self.dashboard_multi_get_count(*uids)
552
+ Facebooker::Session.create.post("facebook.dashboard.multiGetCount", :uids => uids.flatten)
553
+ end
554
+
555
+ # Facebooker::User.dashboard_multi_set_count({ '1234' => '11', '5678' => '22' })
556
+ def self.dashboard_multi_set_count(ids)
557
+ Facebooker::Session.create.post("facebook.dashboard.multiSetCount", :ids => ids.to_json)
558
+ end
559
+
560
+ # Facebooker::User.dashboard_multi_increment_count ['1234', '5678']
561
+ def self.dashboard_multi_increment_count(*uids)
562
+ Facebooker::Session.create.post("facebook.dashboard.multiIncrementCount", :uids => uids.flatten.collect{ |uid| uid.to_s }.to_json)
563
+ end
564
+
565
+ # Facebooker::User.dashboard_multi_decrement_count ['1234', '5678']
566
+ def self.dashboard_multi_decrement_count(*uids)
567
+ Facebooker::Session.create.post("facebook.dashboard.multiDecrementCount", :uids => uids.flatten.collect{ |uid| uid.to_s }.to_json)
568
+ end
569
+
570
+
571
+
572
+
573
+ def get_news(*news_ids)
574
+ params = { :uid => uid }
575
+ params[:news_ids] = news_ids.flatten if news_ids
576
+
577
+ session.post('facebook.dashboard.getNews', params)
578
+ end
579
+
580
+ # facebook_session.user.add_news [{ :message => 'Hey, who are you?', :action_link => { :text => "I-I'm a test user", :href => 'http://facebook.er/' }}], 'http://facebook.er/icon.png'
581
+ def add_news(news, image=nil)
582
+ params = { :uid => uid }
583
+ params[:news] = news
584
+ params[:image] = image if image
585
+
586
+ session.post('facebook.dashboard.addNews', params)
587
+ end
588
+
589
+ # facebook_session.user.clear_news ['111111']
590
+ def clear_news(*news_ids)
591
+ params = { :uid => uid }
592
+ params[:news_ids] = news_ids.flatten if news_ids
593
+
594
+ session.post('facebook.dashboard.clearNews', params)
595
+ end
596
+
597
+ # Facebooker::User.multi_add_news(['1234', '4321'], [{ :message => 'Hi users', :action_link => { :text => "Uh hey there app", :href => 'http://facebook.er/' }}], 'http://facebook.er/icon.png')
598
+ def self.multi_add_news(uids, news, image=nil)
599
+ params = { :uids => uids, :news => news }
600
+ params[:image] = image if image
601
+
602
+ Facebooker::Session.create.post("facebook.dashboard.multiAddNews", params)
603
+ end
604
+
605
+ # Facebooker::User.multi_clear_news({"1234"=>["319103117527"], "4321"=>["313954287803"]})
606
+ def self.multi_clear_news(ids)
607
+ Facebooker::Session.create.post("facebook.dashboard.multiClearNews", :ids => ids.to_json)
608
+ end
609
+
610
+ # Facebooker::User.multi_get_news({"1234"=>["319103117527"], "4321"=>["313954287803"]})
611
+ def self.multi_get_news(ids)
612
+ Facebooker::Session.create.post('facebook.dashboard.multiGetNews', :ids => ids.to_json)
613
+ end
614
+
615
+ # facebook_session.user.get_activity '123'
616
+ def get_activity(*activity_ids)
617
+ params = {}
618
+ params[:activity_ids] = activity_ids.flatten if activity_ids
619
+
620
+ session.post('facebook.dashboard.getActivity', params)
621
+ end
622
+
623
+ # facebook_session.user.publish_activity({ :message => '{*actor*} rolled around', :action_link => { :text => 'Roll around too', :href => 'http://facebook.er/' }})
624
+ def publish_activity(activity)
625
+ session.post('facebook.dashboard.publishActivity', { :activity => activity.to_json })
626
+ end
627
+
628
+ # facebook_session.user.remove_activity ['123']
629
+ def remove_activity(*activity_ids)
630
+ session.post('facebook.dashboard.removeActivity', { :activity_ids => activity_ids.flatten })
631
+ end
632
+
633
+
634
+ ##
635
+ # Two Facebooker::User objects should be considered equal if their Facebook ids are equal
636
+ def ==(other_user)
637
+ other_user.is_a?(User) && id == other_user.id
638
+ end
639
+
640
+
641
+ # register a user with Facebook
642
+ # users should be a hast with at least an :email field
643
+ # you can optionally provide an :account_id field as well
644
+
645
+ def self.register(users)
646
+ user_map={}
647
+ users=users.map do |h|
648
+ returning h.dup do |d|
649
+ if email=d.delete(:email)
650
+ hash = hash_email(email)
651
+ user_map[hash]=h
652
+ d[:email_hash]=hash
653
+ end
654
+ end
655
+ end
656
+ Facebooker::Session.create.post("facebook.connect.registerUsers",:accounts=>users.to_json) do |ret|
657
+ ret.each do |hash|
658
+ user_map.delete(hash)
659
+ end
660
+ unless user_map.empty?
661
+ e=Facebooker::Session::UserRegistrationFailed.new
662
+ e.failed_users = user_map.values
663
+ raise e
664
+ end
665
+ ret
666
+ end
667
+ end
668
+
669
+ # Get a count of unconnected friends
670
+ def getUnconnectedFriendsCount
671
+ session.post("facebook.connect.getUnconnectedFriendsCount")
672
+ end
673
+
674
+
675
+ # Unregister an array of email hashes
676
+ def self.unregister(email_hashes)
677
+ Facebooker::Session.create.post("facebook.connect.unregisterUsers",:email_hashes=>email_hashes.to_json) do |ret|
678
+ ret.each do |hash|
679
+ email_hashes.delete(hash)
680
+ end
681
+ unless email_hashes.empty?
682
+ e=Facebooker::Session::UserUnRegistrationFailed.new
683
+ e.failed_users = email_hashes
684
+ raise e
685
+ end
686
+ ret
687
+ end
688
+ end
689
+
690
+ # unregister an array of email addresses
691
+ def self.unregister_emails(emails)
692
+ emails_hash = {}
693
+ emails.each {|e| emails_hash[hash_email(e)] = e}
694
+ begin
695
+ unregister(emails_hash.keys).collect {|r| emails_hash[r]}
696
+ rescue
697
+ # re-raise with emails instead of hashes.
698
+ e = Facebooker::Session::UserUnRegistrationFailed.new
699
+ e.failed_users = $!.failed_users.collect { |f| emails_hash[f] }
700
+ raise e
701
+ end
702
+ end
703
+
704
+ def self.hash_email(email)
705
+ email = email.downcase.strip
706
+ crc=Zlib.crc32(email)
707
+ md5=Digest::MD5.hexdigest(email)
708
+ "#{crc}_#{md5}"
709
+ end
710
+
711
+ def self.cast_to_facebook_id(object)
712
+ if object.respond_to?(:facebook_id)
713
+ object.facebook_id
714
+ else
715
+ object
716
+ end
717
+ end
718
+
719
+ def self.user_fields(fields = [])
720
+ valid_fields(fields)
721
+ end
722
+
723
+ def self.standard_fields(fields = [])
724
+ valid_fields(fields,STANDARD_FIELDS)
725
+ end
726
+
727
+ private
728
+ def publish(feed_story_or_action)
729
+ session.post(Facebooker::Feed::METHODS[feed_story_or_action.class.name.split(/::/).last], feed_story_or_action.to_params) == "1" ? true : false
730
+ end
731
+
732
+ def self.valid_fields(fields, allowable=FIELDS)
733
+ allowable.reject{|field_name| !fields.empty? && !fields.include?(field_name)}.join(',')
734
+ end
735
+
736
+ def collect(fields, allowable=FIELDS)
737
+ allowable.reject{|field_name| !fields.empty? && !fields.include?(field_name)}.join(',')
738
+ end
739
+
740
+ def profile_pic_album_id
741
+ merge_aid(-3, id)
742
+ end
743
+
744
+ def merge_aid(aid, uid)
745
+ (uid << 32) + (aid & 0xFFFFFFFF)
746
+ end
747
+
748
+ def prepare_get_stream_options(options)
749
+ opts = {}
750
+
751
+ opts[:viewer_id] = self.id
752
+ opts[:source_ids] = options[:source_ids] if options[:source_ids]
753
+ opts[:start_time] = options[:start_time].to_i if options[:start_time]
754
+ opts[:end_time] = options[:end_time].to_i if options[:end_time]
755
+ opts[:limit] = options[:limit] if options[:limit].is_a?(Integer)
756
+ opts[:metadata] = Facebooker.json_encode(options[:metadata]) if options[:metadata]
757
+ opts
758
+ end
759
+
760
+ end
761
+ end