djanowski-mmangino-facebooker 1.0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (111) hide show
  1. data/CHANGELOG.txt +0 -0
  2. data/COPYING +19 -0
  3. data/History.txt +16 -0
  4. data/Manifest.txt +110 -0
  5. data/README +46 -0
  6. data/README.txt +81 -0
  7. data/Rakefile +76 -0
  8. data/TODO.txt +10 -0
  9. data/generators/facebook/facebook_generator.rb +14 -0
  10. data/generators/facebook/templates/config/facebooker.yml +41 -0
  11. data/generators/facebook/templates/public/javascripts/facebooker.js +106 -0
  12. data/generators/facebook_controller/USAGE +33 -0
  13. data/generators/facebook_controller/facebook_controller_generator.rb +40 -0
  14. data/generators/facebook_controller/templates/controller.rb +7 -0
  15. data/generators/facebook_controller/templates/functional_test.rb +11 -0
  16. data/generators/facebook_controller/templates/helper.rb +2 -0
  17. data/generators/facebook_controller/templates/view.fbml.erb +2 -0
  18. data/generators/facebook_controller/templates/view.html.erb +2 -0
  19. data/generators/facebook_publisher/facebook_publisher_generator.rb +14 -0
  20. data/generators/facebook_publisher/templates/create_facebook_templates.rb +15 -0
  21. data/generators/facebook_publisher/templates/publisher.rb +3 -0
  22. data/generators/facebook_scaffold/USAGE +27 -0
  23. data/generators/facebook_scaffold/facebook_scaffold_generator.rb +118 -0
  24. data/generators/facebook_scaffold/templates/controller.rb +93 -0
  25. data/generators/facebook_scaffold/templates/facebook_style.css +2579 -0
  26. data/generators/facebook_scaffold/templates/functional_test.rb +89 -0
  27. data/generators/facebook_scaffold/templates/helper.rb +2 -0
  28. data/generators/facebook_scaffold/templates/layout.fbml.erb +6 -0
  29. data/generators/facebook_scaffold/templates/layout.html.erb +17 -0
  30. data/generators/facebook_scaffold/templates/style.css +74 -0
  31. data/generators/facebook_scaffold/templates/view_edit.fbml.erb +13 -0
  32. data/generators/facebook_scaffold/templates/view_edit.html.erb +18 -0
  33. data/generators/facebook_scaffold/templates/view_index.fbml.erb +24 -0
  34. data/generators/facebook_scaffold/templates/view_index.html.erb +24 -0
  35. data/generators/facebook_scaffold/templates/view_new.fbml.erb +12 -0
  36. data/generators/facebook_scaffold/templates/view_new.html.erb +17 -0
  37. data/generators/facebook_scaffold/templates/view_show.fbml.erb +10 -0
  38. data/generators/facebook_scaffold/templates/view_show.html.erb +10 -0
  39. data/generators/publisher/publisher_generator.rb +14 -0
  40. data/init.rb +70 -0
  41. data/install.rb +12 -0
  42. data/lib/facebooker.rb +143 -0
  43. data/lib/facebooker/adapters/adapter_base.rb +87 -0
  44. data/lib/facebooker/adapters/bebo_adapter.rb +75 -0
  45. data/lib/facebooker/adapters/facebook_adapter.rb +48 -0
  46. data/lib/facebooker/admin.rb +28 -0
  47. data/lib/facebooker/batch_request.rb +44 -0
  48. data/lib/facebooker/data.rb +57 -0
  49. data/lib/facebooker/feed.rb +78 -0
  50. data/lib/facebooker/logging.rb +51 -0
  51. data/lib/facebooker/model.rb +123 -0
  52. data/lib/facebooker/models/affiliation.rb +10 -0
  53. data/lib/facebooker/models/album.rb +11 -0
  54. data/lib/facebooker/models/applicationproperties.rb +39 -0
  55. data/lib/facebooker/models/cookie.rb +10 -0
  56. data/lib/facebooker/models/education_info.rb +11 -0
  57. data/lib/facebooker/models/event.rb +26 -0
  58. data/lib/facebooker/models/friend_list.rb +14 -0
  59. data/lib/facebooker/models/group.rb +35 -0
  60. data/lib/facebooker/models/info_item.rb +10 -0
  61. data/lib/facebooker/models/info_section.rb +10 -0
  62. data/lib/facebooker/models/location.rb +8 -0
  63. data/lib/facebooker/models/notifications.rb +17 -0
  64. data/lib/facebooker/models/page.rb +27 -0
  65. data/lib/facebooker/models/photo.rb +10 -0
  66. data/lib/facebooker/models/tag.rb +12 -0
  67. data/lib/facebooker/models/user.rb +367 -0
  68. data/lib/facebooker/models/work_info.rb +9 -0
  69. data/lib/facebooker/parser.rb +547 -0
  70. data/lib/facebooker/rails/controller.rb +246 -0
  71. data/lib/facebooker/rails/facebook_asset_path.rb +18 -0
  72. data/lib/facebooker/rails/facebook_form_builder.rb +112 -0
  73. data/lib/facebooker/rails/facebook_pretty_errors.rb +14 -0
  74. data/lib/facebooker/rails/facebook_request_fix.rb +24 -0
  75. data/lib/facebooker/rails/facebook_session_handling.rb +69 -0
  76. data/lib/facebooker/rails/facebook_url_rewriting.rb +39 -0
  77. data/lib/facebooker/rails/helpers.rb +615 -0
  78. data/lib/facebooker/rails/profile_publisher_extensions.rb +42 -0
  79. data/lib/facebooker/rails/publisher.rb +497 -0
  80. data/lib/facebooker/rails/routing.rb +49 -0
  81. data/lib/facebooker/rails/test_helpers.rb +88 -0
  82. data/lib/facebooker/rails/utilities.rb +22 -0
  83. data/lib/facebooker/server_cache.rb +24 -0
  84. data/lib/facebooker/service.rb +31 -0
  85. data/lib/facebooker/session.rb +564 -0
  86. data/lib/facebooker/version.rb +9 -0
  87. data/lib/net/http_multipart_post.rb +123 -0
  88. data/lib/tasks/facebooker.rake +18 -0
  89. data/lib/tasks/tunnel.rake +46 -0
  90. data/rails/init.rb +1 -0
  91. data/setup.rb +1585 -0
  92. data/templates/layout.erb +24 -0
  93. data/test/adapters_test.rb +98 -0
  94. data/test/batch_request_test.rb +82 -0
  95. data/test/event_test.rb +15 -0
  96. data/test/facebook_admin_test.rb +75 -0
  97. data/test/facebook_cache_test.rb +43 -0
  98. data/test/facebook_data_test.rb +84 -0
  99. data/test/facebooker_test.rb +855 -0
  100. data/test/fixtures/multipart_post_body_with_only_parameters.txt +33 -0
  101. data/test/fixtures/multipart_post_body_with_single_file.txt +38 -0
  102. data/test/fixtures/multipart_post_body_with_single_file_that_has_nil_key.txt +38 -0
  103. data/test/http_multipart_post_test.rb +54 -0
  104. data/test/logging_test.rb +43 -0
  105. data/test/model_test.rb +91 -0
  106. data/test/publisher_test.rb +436 -0
  107. data/test/rails_integration_test.rb +1126 -0
  108. data/test/session_test.rb +587 -0
  109. data/test/test_helper.rb +60 -0
  110. data/test/user_test.rb +253 -0
  111. metadata +181 -0
@@ -0,0 +1,123 @@
1
+ module Facebooker
2
+ ##
3
+ # helper methods primarily supporting the management of Ruby objects which are populatable via Hashes.
4
+ # Since most Facebook API calls accept and return hashes of data (as XML), the Model module allows us to
5
+ # directly populate a model's attributes given a Hash with matching key names.
6
+ module Model
7
+ class UnboundSessionException < Exception; end
8
+ def self.included(includer)
9
+ includer.extend ClassMethods
10
+ includer.__send__(:attr_writer, :session)
11
+ includer.__send__(:attr_reader, :anonymous_fields)
12
+ end
13
+ module ClassMethods
14
+ ##
15
+ # Instantiate a new instance of the class into which we are included and populate that instance's
16
+ # attributes given the provided Hash. Key names in the Hash should map to attribute names on the model.
17
+ def from_hash(hash)
18
+ instance = new(hash)
19
+ yield instance if block_given?
20
+ instance
21
+ end
22
+
23
+ ##
24
+ # Create a standard attr_writer and a populating_attr_reader
25
+ def populating_attr_accessor(*symbols)
26
+ attr_writer *symbols
27
+ populating_attr_reader *symbols
28
+ end
29
+
30
+ ##
31
+ # Create a reader that will attempt to populate the model if it has not already been populated
32
+ def populating_attr_reader(*symbols)
33
+ symbols.each do |symbol|
34
+ define_method(symbol) do
35
+ populate unless populated?
36
+ instance_variable_get("@#{symbol}")
37
+ end
38
+ end
39
+ end
40
+
41
+ def populating_hash_settable_accessor(symbol, klass)
42
+ populating_attr_reader symbol
43
+ hash_settable_writer(symbol, klass)
44
+ end
45
+
46
+ def populating_hash_settable_list_accessor(symbol, klass)
47
+ populating_attr_reader symbol
48
+ hash_settable_list_writer(symbol, klass)
49
+ end
50
+
51
+ #
52
+ # Declares an attribute named ::symbol:: which can be set with either an instance of ::klass::
53
+ # or a Hash which will be used to populate a new instance of ::klass::.
54
+ def hash_settable_accessor(symbol, klass)
55
+ attr_reader symbol
56
+ hash_settable_writer(symbol, klass)
57
+ end
58
+
59
+ def hash_settable_writer(symbol, klass)
60
+ define_method("#{symbol}=") do |value|
61
+ instance_variable_set("@#{symbol}", value.kind_of?(Hash) ? klass.from_hash(value) : value)
62
+ end
63
+ end
64
+
65
+ #
66
+ # Declares an attribute named ::symbol:: which can be set with either a list of instances of ::klass::
67
+ # or a list of Hashes which will be used to populate a new instance of ::klass::.
68
+ def hash_settable_list_accessor(symbol, klass)
69
+ attr_reader symbol
70
+ hash_settable_list_writer(symbol, klass)
71
+ end
72
+
73
+ def hash_settable_list_writer(symbol, klass)
74
+ define_method("#{symbol}=") do |list|
75
+ instance_variable_set("@#{symbol}", list.map do |item|
76
+ item.kind_of?(Hash) ? klass.from_hash(item) : item
77
+ end)
78
+ end
79
+ end
80
+ end
81
+
82
+ ##
83
+ # Centralized, error-checked place for a model to get the session to which it is bound.
84
+ # Any Facebook API queries require a Session instance.
85
+ def session
86
+ @session || (raise UnboundSessionException, "Must bind this object to a Facebook session before querying")
87
+ end
88
+
89
+ #
90
+ # This gets populated from FQL queries.
91
+ def anon=(value)
92
+ @anonymous_fields = value
93
+ end
94
+
95
+ def initialize(hash = {})
96
+ populate_from_hash!(hash)
97
+ end
98
+
99
+ def populate
100
+ raise NotImplementedError, "#{self.class} included me and should have overriden me"
101
+ end
102
+
103
+ def populated?
104
+ !@populated.nil?
105
+ end
106
+
107
+ ##
108
+ # Set model's attributes via Hash. Keys should map directly to the model's attribute names.
109
+ def populate_from_hash!(hash)
110
+ unless hash.empty?
111
+ hash.each do |key, value|
112
+ set_attr_method = "#{key}="
113
+ if respond_to?(set_attr_method)
114
+ self.__send__(set_attr_method, value)
115
+ else
116
+ Facebooker::Logging.log_info("**Warning**, Attempt to set non-attribute: #{key}",hash)
117
+ end
118
+ end
119
+ @populated = true
120
+ end
121
+ end
122
+ end
123
+ end
@@ -0,0 +1,10 @@
1
+ require 'facebooker/model'
2
+ module Facebooker
3
+ ##
4
+ # Represents a user's affiliation, for example, which educational institutions
5
+ # the user is associated with.
6
+ class Affiliation
7
+ include Model
8
+ attr_accessor :name, :status, :type, :year, :nid
9
+ end
10
+ end
@@ -0,0 +1,11 @@
1
+ require 'facebooker/model'
2
+ module Facebooker
3
+ ##
4
+ # A simple representation of a photo album.
5
+ class Album
6
+ include Model
7
+ attr_accessor :aid, :cover_pid, :owner, :name, :created,
8
+ :modified, :description, :location, :link, :size
9
+
10
+ end
11
+ end
@@ -0,0 +1,39 @@
1
+ module Facebooker
2
+
3
+ # application_name string The name of your application.
4
+ # callback_url string Your application's callback URL. The callback URL cannot be longer than 100 characters.
5
+ # post_install_url string The URL where a user gets redirected after installing your application. The post-install URL cannot be longer than 100 characters.
6
+ # edit_url string
7
+ # dashboard_url string
8
+ # uninstall_url string The URL where a user gets redirected after removing your application.
9
+ # ip_list string For Web-based applications, these are the IP addresses of your servers that can access Facebook's servers and serve information to your application.
10
+ # email string The email address associated with the application; the email address Facebook uses to contact you about your application. (default value is your Facebook email address.)
11
+ # description string The description of your application.
12
+ # use_iframe bool Indicates whether you render your application with FBML (0) or in an iframe (1). (default value is 1)
13
+ # desktop bool Indicates whether your application is Web-based (0) or gets installed on a user's desktop (1). (default value is 1)
14
+ # is_mobile bool Indicates whether your application can run on a mobile device (1) or not (0). (default value is 1)
15
+ # default_fbml string The default FBML code that appears in the user's profile box when he or she adds your application.
16
+ # default_column bool Indicates whether your application appears in the wide (1) or narrow (0) column of a user's Facebook profile. (default value is 1)
17
+ # message_url string For applications that can create attachments, this is the URL where you store the attachment's content.
18
+ # message_action string For applications that can create attachments, this is the label for the action that creates the attachment. It cannot be more than 20 characters.
19
+ # about_url string This is the URL to your application's About page. About pages are now Facebook Pages.
20
+ # private_install bool Indicates whether you want to disable (1) or enable (0) News Feed and Mini-Feed stories when a user installs your application. (default value is 1)
21
+ # installable bool Indicates whether a user can (1) or cannot (0) install your application. (default value is 1)
22
+ # privacy_url string The URL to your application's privacy terms.
23
+ # help_url string The URL to your application's help page.
24
+ # see_all_url string
25
+ # tos_url string The URL to your application's Terms of Service.
26
+ # dev_mode bool Indicates whether developer mode is enabled (1) or disabled (0). Only developers can install applications in developer mode. (default value is 1)
27
+ # preload_fql string A preloaded FQL query.
28
+ class ApplicationProperties
29
+ include Model
30
+ FIELDS = [ :application_name, :callback_url, :post_install_url, :edit_url, :dashboard_url,
31
+ :uninstall_url, :ip_list, :email, :description, :use_iframe, :desktop, :is_mobile,
32
+ :default_fbml, :default_column, :message_url, :message_action, :about_url,
33
+ :private_install, :installable, :privacy_url, :help_url, :see_all_url, :tos_url,
34
+ :dev_mode, :preload_fql, :icon_url, :canvas_name, :logo_url, :connect_logo_url ]
35
+
36
+ attr_accessor *FIELDS
37
+
38
+ end
39
+ end
@@ -0,0 +1,10 @@
1
+ require 'facebooker/model'
2
+ module Facebooker
3
+
4
+ ##
5
+ # A simple representation of a cookie.
6
+ class Cookie
7
+ include Model
8
+ attr_accessor :uid, :name, :value, :expires, :path
9
+ end
10
+ end
@@ -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
10
+ end
11
+ end
@@ -0,0 +1,26 @@
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 :eid, :pic, :pic_small, :pic_big, :name, :creator, :update_time, :description, :tagline, :venue, :host, :event_type, :nid, :location, :end_time, :start_time, :event_subtype
25
+ end
26
+ end
@@ -0,0 +1,14 @@
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 :flid, :name
8
+
9
+ # We need this to be an integer, so do the conversion
10
+ def flid=(f)
11
+ @flid= ( f.nil? ? nil : f.to_i)
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,35 @@
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, :gid, :update_time, :group_subtype, :group_type, :website, :office, :description, :venue, :nid
12
+
13
+
14
+ ##
15
+ # Get the full list of members as populated User objects. First time fetches group members via Facebook API call.
16
+ # Subsequent calls return cached values.
17
+ # This is a convenience method for getting all of the Membership instances and instantiating User instances for each Membership.
18
+ def members
19
+ @members ||= memberships.map do |membership|
20
+ User.new(membership.uid, session)
21
+ end
22
+ end
23
+
24
+ ##
25
+ # Get a list of Membership instances associated with this Group. First call retrieves the Membership instances via a Facebook
26
+ # API call. Subsequent calls are retrieved from in-memory cache.
27
+ def memberships
28
+ @memberships ||= session.post('facebook.groups.getMembers', :gid => gid).map do |hash|
29
+ Membership.from_hash(hash) do |membership|
30
+ membership.gid = gid
31
+ end
32
+ end
33
+ end
34
+ end
35
+ 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,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,27 @@
1
+ require 'facebooker/model'
2
+ module Facebooker
3
+ class Page
4
+
5
+ class Genre
6
+ include Model
7
+ FIELDS = [ :dance, :party, :relax, :talk, :think, :workout, :sing, :intimate, :raunchy, :headphones ]
8
+ attr_accessor *FIELDS
9
+
10
+ def initialize(*args)
11
+ super
12
+
13
+ # convert '1'/'0' to true/false
14
+ FIELDS.each do |field|
15
+ self.send("#{field}=", self.send(field) == '1')
16
+ end
17
+ end
18
+ end
19
+
20
+ include Model
21
+ attr_accessor :page_id, :name, :pic_small, :pic_big, :pic_square, :pic_large, :type, :type, :website, :location, :hours, :band_members, :bio, :hometown, :genre, :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
22
+
23
+ def genre=(value)
24
+ @genre = value.kind_of?(Hash) ? Genre.from_hash(value) : value
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,10 @@
1
+ require 'facebooker/model'
2
+ module Facebooker
3
+ class Photo
4
+ include Model
5
+ attr_accessor :pid, :aid, :owner, :title,
6
+ :link, :caption, :created,
7
+ :src, :src_big, :src_small,
8
+ :story_fbid
9
+ end
10
+ end
@@ -0,0 +1,12 @@
1
+ require 'facebooker/model'
2
+ module Facebooker
3
+ class Tag
4
+ include Model
5
+ attr_accessor :pid, :subject, :xcoord, :ycoord
6
+
7
+ def coordinates
8
+ [xcoord, ycoord]
9
+ end
10
+
11
+ end
12
+ end
@@ -0,0 +1,367 @@
1
+ require 'facebooker/model'
2
+ require 'facebooker/models/affiliation'
3
+ require 'facebooker/models/work_info'
4
+ module Facebooker
5
+ #
6
+ # Holds attributes and behavior for a Facebook User
7
+ class User
8
+ include Model
9
+ class Status
10
+ include Model
11
+ attr_accessor :message, :time, :status_id
12
+ end
13
+ FIELDS = [:status, :political, :pic_small, :name, :quotes, :is_app_user, :tv, :profile_update_time, :meeting_sex, :hs_info, :timezone, :relationship_status, :hometown_location, :about_me, :wall_count, :significant_other_id, :pic_big, :music, :uid, :work_history, :sex, :religion, :notes_count, :activities, :pic_square, :movies, :has_added_app, :education_history, :birthday, :first_name, :meeting_for, :last_name, :interests, :current_location, :pic, :books, :affiliations]
14
+ STANDARD_FIELDS = [:uid, :first_name, :last_name, :name, :timezone, :birthday, :sex, :affiliations, :locale, :profile_url]
15
+ attr_accessor :id, :session
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_accessor :status, Status
22
+ populating_hash_settable_list_accessor :affiliations, Affiliation
23
+ populating_hash_settable_list_accessor :education_history, EducationInfo
24
+ populating_hash_settable_list_accessor :work_history, WorkInfo
25
+
26
+ # Can pass in these two forms:
27
+ # id, session, (optional) attribute_hash
28
+ # attribute_hash
29
+ def initialize(*args)
30
+ if (args.first.kind_of?(String) || args.first.kind_of?(Integer)) && args.size==1
31
+ @id=Integer(args.shift)
32
+ @session = Session.current
33
+ elsif (args.first.kind_of?(String) || args.first.kind_of?(Integer)) && args[1].kind_of?(Session)
34
+ @id = Integer(args.shift)
35
+ @session = args.shift
36
+ end
37
+ if args.last.kind_of?(Hash)
38
+ populate_from_hash!(args.pop)
39
+ end
40
+ end
41
+
42
+ # Returns a user's events, params correspond to API call parameters (except UID):
43
+ # http://wiki.developers.facebook.com/index.php/Events.get
44
+ # E.g:
45
+ # @user.events(:start_time => Time.now, :end_time => 1.month.from_now)
46
+ # # => Returns events betwen now and a month from now
47
+ def events(params={})
48
+ @events ||= {}
49
+ [:start_time,:end_time].compact.each do |key|
50
+ params[key] = params[key].to_i
51
+ end
52
+ # puts @events[params.to_s].nil?
53
+ @events[params.to_s] ||= @session.post('facebook.events.get', {:uid => self.id}.merge(params)).map do |event|
54
+ Event.from_hash(event)
55
+ end
56
+ end
57
+
58
+ #
59
+ # Set the list of friends, given an array of User objects. If the list has been retrieved previously, will not set
60
+ def friends=(list_of_friends,flid=nil)
61
+ @friends_hash ||= {}
62
+ flid=cast_to_friend_list_id(flid)
63
+ #use __blank instead of nil so that this is cached
64
+ cache_key = flid||"__blank"
65
+
66
+ @friends_hash[cache_key] ||= list_of_friends
67
+ end
68
+
69
+ def cast_to_friend_list_id(flid)
70
+ case flid
71
+ when String
72
+ list=friend_lists.detect {|f| f.name==flid}
73
+ raise Facebooker::Session::InvalidFriendList unless list
74
+ list.flid
75
+ when FriendList
76
+ flid.flid
77
+ else
78
+ flid
79
+ end
80
+ end
81
+ ##
82
+ # Retrieve friends
83
+ def friends(flid = nil)
84
+ @friends_hash ||= {}
85
+ flid=cast_to_friend_list_id(flid)
86
+
87
+ #use __blank instead of nil so that this is cached
88
+ cache_key = flid||"__blank"
89
+ @friends_hash[cache_key] ||= @session.post('facebook.friends.get', (flid.nil? ? {} : {:flid => flid})).map do |uid|
90
+ User.new(uid, @session)
91
+ end
92
+ @friends_hash[cache_key]
93
+ end
94
+
95
+ def friend_lists
96
+ @friend_lists ||= @session.post('facebook.friends.getLists').map do |hash|
97
+ friend_list = FriendList.from_hash(hash)
98
+ friend_list.session = session
99
+ friend_list
100
+ end
101
+ end
102
+ ###
103
+ # Retrieve friends with user info populated
104
+ # Subsequent calls will be retrieved from memory.
105
+ # Optional: list of fields to retrieve as symbols
106
+ def friends!(*fields)
107
+ @friends ||= session.post('facebook.users.getInfo', :fields => collect(fields), :uids => friends.map{|f| f.id}.join(',')).map do |hash|
108
+ User.new(hash['uid'], session, hash)
109
+ end
110
+ end
111
+
112
+ ###
113
+ # Retrieve profile data for logged in user
114
+ # Optional: list of fields to retrieve as symbols
115
+ def populate(*fields)
116
+ session.post('facebook.users.getInfo', :fields => collect(fields), :uids => id) do |response|
117
+ populate_from_hash!(response.first)
118
+ end
119
+ end
120
+
121
+ def friends_with?(user_or_id)
122
+ friends.map{|f| f.to_i}.include?(user_or_id.to_i)
123
+ end
124
+
125
+ def friends_with_this_app
126
+ @friends_with_this_app ||= session.post('facebook.friends.getAppUsers').map do |uid|
127
+ User.new(uid, session)
128
+ end
129
+ end
130
+
131
+ def groups(gids = [])
132
+ args = gids.empty? ? {} : {:gids => gids}
133
+ @groups ||= session.post('facebook.groups.get', args).map do |hash|
134
+ group = Group.from_hash(hash)
135
+ group.session = session
136
+ group
137
+ end
138
+ end
139
+
140
+ def notifications
141
+ @notifications ||= Notifications.from_hash(session.post('facebook.notifications.get'))
142
+ end
143
+
144
+ def publish_story(story)
145
+ publish(story)
146
+ end
147
+
148
+ def publish_action(action)
149
+ publish(action)
150
+ end
151
+
152
+ def publish_templatized_action(action)
153
+ publish(action)
154
+ end
155
+
156
+ def albums
157
+ @albums ||= session.post('facebook.photos.getAlbums', :uid => self.id) do |response|
158
+ response.map do |hash|
159
+ Album.from_hash(hash)
160
+ end
161
+ end
162
+ end
163
+
164
+ def create_album(params)
165
+ @album = session.post('facebook.photos.createAlbum', params) {|response| Album.from_hash(response)}
166
+ end
167
+
168
+ def profile_photos
169
+ session.get_photos(nil, nil, profile_pic_album_id)
170
+ end
171
+
172
+ def upload_photo(multipart_post_file)
173
+ Photo.from_hash(session.post_file('facebook.photos.upload', {nil => multipart_post_file}))
174
+ end
175
+
176
+ def profile_fbml
177
+ session.post('facebook.profile.getFBML', :uid => @id)
178
+ end
179
+
180
+ ##
181
+ # Set the profile FBML for this user
182
+ #
183
+ # This does not set profile actions, that should be done with profile_action=
184
+ def profile_fbml=(markup)
185
+ set_profile_fbml(markup, nil, nil)
186
+ end
187
+
188
+ ##
189
+ # Set the mobile profile FBML
190
+ def mobile_fbml=(markup)
191
+ set_profile_fbml(nil, markup, nil)
192
+ end
193
+
194
+ def profile_action=(markup)
195
+ set_profile_fbml(nil, nil, markup)
196
+ end
197
+
198
+ def profile_main=(markup)
199
+ set_profile_fbml(nil,nil,nil,markup)
200
+ end
201
+
202
+ def set_profile_fbml(profile_fbml, mobile_fbml, profile_action_fbml, profile_main = nil)
203
+ parameters = {:uid => @id}
204
+ parameters[:profile] = profile_fbml if profile_fbml
205
+ parameters[:profile_action] = profile_action_fbml if profile_action_fbml
206
+ parameters[:mobile_profile] = mobile_fbml if mobile_fbml
207
+ parameters[:profile_main] = profile_main if profile_main
208
+ session.post('facebook.profile.setFBML', parameters,false)
209
+ end
210
+
211
+ ## ** NEW PROFILE DESIGN ***
212
+ # Set a info section for this user
213
+ #
214
+ # Note: using set_profile_info as I feel using user.set_info could be confused with the user.getInfo facebook method.
215
+ # Also, I feel it fits in line with user.set_profile_fbml.
216
+ def set_profile_info(title, info_fields, format = :text)
217
+ session.post('facebook.profile.setInfo', :title => title, :uid => @id,
218
+ :type => format.to_s == "text" ? 1 : 5, :info_fields => info_fields.to_json)
219
+ end
220
+
221
+ def get_profile_info
222
+ session.post('facebook.profile.getInfo', :uid => @id)
223
+ end
224
+
225
+ ##
226
+ # This DOES NOT set the status of a user on Facebook
227
+ # Use the set_status method instead
228
+ def status=(message)
229
+ case message
230
+ when String,Status
231
+ @status = message
232
+ when Hash
233
+ @status = Status.from_hash(message)
234
+ end
235
+ end
236
+
237
+ ##
238
+ # Set the status for a user
239
+ # DOES NOT prepend "is" to the message
240
+ #
241
+ # requires extended permission.
242
+ def set_status(message)
243
+ self.status=message
244
+ session.post('facebook.users.setStatus',:status=>message,:status_includes_verb=>1) do |ret|
245
+ ret
246
+ end
247
+ end
248
+
249
+ ##
250
+ # Checks to see if the user has enabled the given extended permission
251
+ def has_permission?(ext_perm) # ext_perm = email, offline_access, status_update, photo_upload, create_listing, create_event, rsvp_event, sms
252
+ session.post('facebook.users.hasAppPermission',:ext_perm=>ext_perm) == "1"
253
+ end
254
+
255
+ ##
256
+ # Convenience method to send email to the current user
257
+ def send_email(subject, text=nil, fbml=nil)
258
+ session.send_email([@id], subject, text, fbml)
259
+ end
260
+
261
+ ##
262
+ # Convenience method to set cookie for the current user
263
+ def set_cookie(name, value, expires=nil, path=nil)
264
+ session.data.set_cookie(@id, name, value, expires, path)
265
+ end
266
+
267
+ ##
268
+ # Convenience method to get cookies for the current user
269
+ def get_cookies(name=nil)
270
+ session.data.get_cookies(@id, name)
271
+ end
272
+
273
+ ##
274
+ # Returns the user's id as an integer
275
+ def to_i
276
+ id
277
+ end
278
+
279
+ def to_s
280
+ id.to_s
281
+ end
282
+
283
+ ##
284
+ # Two Facebooker::User objects should be considered equal if their Facebook ids are equal
285
+ def ==(other_user)
286
+ id == other_user.id
287
+ end
288
+
289
+
290
+ # register a user with Facebook
291
+ # users should be a hast with at least an :email field
292
+ # you can optionally provide an :account_id field as well
293
+
294
+ def self.register(users)
295
+ user_map={}
296
+ users=users.map do |h|
297
+ returning h.dup do |d|
298
+ if email=d.delete(:email)
299
+ hash = hash_email(email)
300
+ user_map[hash]=h
301
+ d[:email_hash]=hash
302
+ end
303
+ end
304
+ end
305
+ Facebooker::Session.create.post("facebook.connect.registerUsers",:accounts=>users.to_json) do |ret|
306
+ ret.each do |hash|
307
+ user_map.delete(hash)
308
+ end
309
+ unless user_map.empty?
310
+ e=Facebooker::Session::UserRegistrationFailed.new
311
+ e.failed_users = user_map.values
312
+ raise e
313
+ end
314
+ ret
315
+ end
316
+ end
317
+
318
+ def self.hash_email(email)
319
+ email = email.downcase.strip
320
+ crc=Zlib.crc32(email)
321
+ md5=Digest::MD5.hexdigest(email)
322
+ "#{crc}_#{md5}"
323
+ end
324
+
325
+ def self.cast_to_facebook_id(object)
326
+ if object.respond_to?(:facebook_id)
327
+ object.facebook_id
328
+ else
329
+ object
330
+ end
331
+ end
332
+
333
+ def facebook_id
334
+ @id
335
+ end
336
+
337
+ def self.user_fields(fields = [])
338
+ valid_fields(fields)
339
+ end
340
+
341
+ def self.standard_fields(fields = [])
342
+ valid_fields(fields,STANDARD_FIELDS)
343
+ end
344
+
345
+ private
346
+ def publish(feed_story_or_action)
347
+ session.post(Facebooker::Feed::METHODS[feed_story_or_action.class.name.split(/::/).last], feed_story_or_action.to_params) == "1" ? true : false
348
+ end
349
+
350
+ def self.valid_fields(fields, allowable=FIELDS)
351
+ allowable.reject{|field_name| !fields.empty? && !fields.include?(field_name)}.join(',')
352
+ end
353
+
354
+ def collect(fields, allowable=FIELDS)
355
+ allowable.reject{|field_name| !fields.empty? && !fields.include?(field_name)}.join(',')
356
+ end
357
+
358
+ def profile_pic_album_id
359
+ merge_aid(-3, @id)
360
+ end
361
+
362
+ def merge_aid(aid, uid)
363
+ (uid << 32) + (aid & 0xFFFFFFFF)
364
+ end
365
+
366
+ end
367
+ end