tenderlove-facebooker 1.0.16.20090319151701
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.rdoc +17 -0
- data/COPYING.rdoc +19 -0
- data/Manifest.txt +123 -0
- data/README.rdoc +103 -0
- data/Rakefile +86 -0
- data/TODO.rdoc +4 -0
- data/examples/desktop_login.rb +14 -0
- data/facebooker.gemspec +38 -0
- data/generators/facebook/facebook_generator.rb +14 -0
- data/generators/facebook/templates/config/facebooker.yml +46 -0
- data/generators/facebook/templates/public/javascripts/facebooker.js +99 -0
- data/generators/facebook_controller/USAGE +33 -0
- data/generators/facebook_controller/facebook_controller_generator.rb +40 -0
- data/generators/facebook_controller/templates/controller.rb +7 -0
- data/generators/facebook_controller/templates/functional_test.rb +11 -0
- data/generators/facebook_controller/templates/helper.rb +2 -0
- data/generators/facebook_controller/templates/view.fbml.erb +2 -0
- data/generators/facebook_controller/templates/view.html.erb +2 -0
- data/generators/facebook_publisher/facebook_publisher_generator.rb +14 -0
- data/generators/facebook_publisher/templates/create_facebook_templates.rb +15 -0
- data/generators/facebook_publisher/templates/publisher.rb +3 -0
- data/generators/facebook_scaffold/USAGE +27 -0
- data/generators/facebook_scaffold/facebook_scaffold_generator.rb +118 -0
- data/generators/facebook_scaffold/templates/controller.rb +93 -0
- data/generators/facebook_scaffold/templates/facebook_style.css +2579 -0
- data/generators/facebook_scaffold/templates/functional_test.rb +89 -0
- data/generators/facebook_scaffold/templates/helper.rb +2 -0
- data/generators/facebook_scaffold/templates/layout.fbml.erb +6 -0
- data/generators/facebook_scaffold/templates/layout.html.erb +17 -0
- data/generators/facebook_scaffold/templates/style.css +74 -0
- data/generators/facebook_scaffold/templates/view_edit.fbml.erb +13 -0
- data/generators/facebook_scaffold/templates/view_edit.html.erb +18 -0
- data/generators/facebook_scaffold/templates/view_index.fbml.erb +24 -0
- data/generators/facebook_scaffold/templates/view_index.html.erb +24 -0
- data/generators/facebook_scaffold/templates/view_new.fbml.erb +12 -0
- data/generators/facebook_scaffold/templates/view_new.html.erb +17 -0
- data/generators/facebook_scaffold/templates/view_show.fbml.erb +10 -0
- data/generators/facebook_scaffold/templates/view_show.html.erb +10 -0
- data/generators/publisher/publisher_generator.rb +14 -0
- data/generators/xd_receiver/templates/xd_receiver.html +10 -0
- data/generators/xd_receiver/xd_receiver_generator.rb +9 -0
- data/init.rb +72 -0
- data/install.rb +12 -0
- data/lib/facebooker/adapters/adapter_base.rb +87 -0
- data/lib/facebooker/adapters/bebo_adapter.rb +75 -0
- data/lib/facebooker/adapters/facebook_adapter.rb +52 -0
- data/lib/facebooker/admin.rb +42 -0
- data/lib/facebooker/batch_request.rb +44 -0
- data/lib/facebooker/data.rb +57 -0
- data/lib/facebooker/feed.rb +78 -0
- data/lib/facebooker/logging.rb +51 -0
- data/lib/facebooker/mobile.rb +20 -0
- data/lib/facebooker/mock/service.rb +50 -0
- data/lib/facebooker/mock/session.rb +18 -0
- data/lib/facebooker/model.rb +135 -0
- data/lib/facebooker/models/affiliation.rb +10 -0
- data/lib/facebooker/models/album.rb +11 -0
- data/lib/facebooker/models/applicationproperties.rb +39 -0
- data/lib/facebooker/models/applicationrestrictions.rb +10 -0
- data/lib/facebooker/models/cookie.rb +10 -0
- data/lib/facebooker/models/education_info.rb +11 -0
- data/lib/facebooker/models/event.rb +28 -0
- data/lib/facebooker/models/friend_list.rb +16 -0
- data/lib/facebooker/models/group.rb +36 -0
- data/lib/facebooker/models/info_item.rb +10 -0
- data/lib/facebooker/models/info_section.rb +10 -0
- data/lib/facebooker/models/location.rb +8 -0
- data/lib/facebooker/models/notifications.rb +17 -0
- data/lib/facebooker/models/page.rb +27 -0
- data/lib/facebooker/models/photo.rb +12 -0
- data/lib/facebooker/models/tag.rb +12 -0
- data/lib/facebooker/models/user.rb +426 -0
- data/lib/facebooker/models/video.rb +9 -0
- data/lib/facebooker/models/work_info.rb +9 -0
- data/lib/facebooker/parser.rb +589 -0
- data/lib/facebooker/rails/controller.rb +304 -0
- data/lib/facebooker/rails/cucumber/world.rb +46 -0
- data/lib/facebooker/rails/cucumber.rb +28 -0
- data/lib/facebooker/rails/facebook_form_builder.rb +112 -0
- data/lib/facebooker/rails/facebook_pretty_errors.rb +22 -0
- data/lib/facebooker/rails/facebook_request_fix.rb +24 -0
- data/lib/facebooker/rails/facebook_session_handling.rb +69 -0
- data/lib/facebooker/rails/facebook_url_helper.rb +192 -0
- data/lib/facebooker/rails/facebook_url_rewriting.rb +39 -0
- data/lib/facebooker/rails/helpers/fb_connect.rb +89 -0
- data/lib/facebooker/rails/helpers.rb +762 -0
- data/lib/facebooker/rails/integration_session.rb +38 -0
- data/lib/facebooker/rails/profile_publisher_extensions.rb +42 -0
- data/lib/facebooker/rails/publisher.rb +526 -0
- data/lib/facebooker/rails/routing.rb +49 -0
- data/lib/facebooker/rails/test_helpers.rb +68 -0
- data/lib/facebooker/rails/utilities.rb +22 -0
- data/lib/facebooker/server_cache.rb +24 -0
- data/lib/facebooker/service.rb +94 -0
- data/lib/facebooker/session.rb +584 -0
- data/lib/facebooker/version.rb +9 -0
- data/lib/facebooker.rb +174 -0
- data/lib/net/http_multipart_post.rb +123 -0
- data/lib/tasks/facebooker.rake +18 -0
- data/lib/tasks/tunnel.rake +46 -0
- data/rails/init.rb +1 -0
- data/setup.rb +1585 -0
- data/templates/layout.erb +24 -0
- data/test/facebooker/adapters_test.rb +96 -0
- data/test/facebooker/admin_test.rb +102 -0
- data/test/facebooker/batch_request_test.rb +83 -0
- data/test/facebooker/data_test.rb +86 -0
- data/test/facebooker/logging_test.rb +43 -0
- data/test/facebooker/mobile_test.rb +45 -0
- data/test/facebooker/model_test.rb +123 -0
- data/test/facebooker/models/event_test.rb +15 -0
- data/test/facebooker/models/user_test.rb +295 -0
- data/test/facebooker/rails/publisher_test.rb +452 -0
- data/test/facebooker/rails_integration_test.rb +1312 -0
- data/test/facebooker/server_cache_test.rb +44 -0
- data/test/facebooker/session_test.rb +614 -0
- data/test/facebooker_test.rb +925 -0
- data/test/fixtures/multipart_post_body_with_only_parameters.txt +33 -0
- data/test/fixtures/multipart_post_body_with_single_file.txt +38 -0
- data/test/fixtures/multipart_post_body_with_single_file_that_has_nil_key.txt +38 -0
- data/test/net/http_multipart_post_test.rb +52 -0
- data/test/rails_test_helper.rb +11 -0
- data/test/test_helper.rb +66 -0
- metadata +217 -0
@@ -0,0 +1,42 @@
|
|
1
|
+
module Facebooker
|
2
|
+
class Admin
|
3
|
+
def initialize(session)
|
4
|
+
@session = session
|
5
|
+
end
|
6
|
+
|
7
|
+
# ** BETA ***
|
8
|
+
# +properties+: Hash of properties you want to set
|
9
|
+
def set_app_properties(properties)
|
10
|
+
properties = properties.respond_to?(:to_json) ? properties.to_json : properties
|
11
|
+
(@session.post 'facebook.admin.setAppProperties', :properties => properties) == '1'
|
12
|
+
end
|
13
|
+
|
14
|
+
# ** BETA ***
|
15
|
+
# +properties+: Hash of properties you want to view.
|
16
|
+
def get_app_properties(*properties)
|
17
|
+
json = @session.post('facebook.admin.getAppProperties', :properties => properties.to_json)
|
18
|
+
hash = Facebooker.json_decode(CGI.unescapeHTML(json))
|
19
|
+
@properties = ApplicationProperties.from_hash(hash)
|
20
|
+
end
|
21
|
+
|
22
|
+
# ** BETA ***
|
23
|
+
# +restrictions+: Hash of restrictions you want to set.
|
24
|
+
def set_restriction_info(restrictions)
|
25
|
+
restrictions = restrictions.respond_to?(:to_json) ? restrictions.to_json : restrictions
|
26
|
+
(@session.post 'facebook.admin.setRestrictionInfo', :restriction_str => restrictions) == '1'
|
27
|
+
end
|
28
|
+
|
29
|
+
# ** BETA ***
|
30
|
+
def get_restriction_info(*restrictions)
|
31
|
+
json = @session.post('facebook.admin.getRestrictionInfo')
|
32
|
+
hash = Facebooker.json_decode(CGI.unescapeHTML(json))
|
33
|
+
@restrictions = ApplicationRestrictions.from_hash(hash)
|
34
|
+
end
|
35
|
+
|
36
|
+
# Integration points include..
|
37
|
+
# :notifications_per_day, :requests_per_day, :emails_per_day, :email_disable_message_location
|
38
|
+
def get_allocation(integration_point)
|
39
|
+
@session.post('facebook.admin.getAllocation', :integration_point_name => integration_point.to_s).to_i
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
module Facebooker
|
2
|
+
class BatchRequest
|
3
|
+
instance_methods.each { |m| undef_method m unless m =~ /(^__|^nil\?$|^send$|proxy_|^respond_to\?$|^new$)/ }
|
4
|
+
attr_reader :uri
|
5
|
+
attr_reader :method
|
6
|
+
class UnexecutedRequest < StandardError; end
|
7
|
+
def initialize(params,proc)
|
8
|
+
@method=params[:method]
|
9
|
+
@uri=params.map{|k,v| "#{k}=#{CGI.escape(v.to_s)}"}.join("&")
|
10
|
+
@proc=proc
|
11
|
+
end
|
12
|
+
|
13
|
+
def result=(result_object)
|
14
|
+
@result = @proc.nil? ? result_object : @proc.call(result_object)
|
15
|
+
end
|
16
|
+
|
17
|
+
def exception_raised=(ex)
|
18
|
+
@exception=ex
|
19
|
+
end
|
20
|
+
|
21
|
+
def exception_raised?
|
22
|
+
@exception.nil? ? false : raise(@exception)
|
23
|
+
end
|
24
|
+
|
25
|
+
def respond_to?(name)
|
26
|
+
super || @result.respond_to?(name)
|
27
|
+
end
|
28
|
+
|
29
|
+
def ===(other)
|
30
|
+
other === @result
|
31
|
+
end
|
32
|
+
|
33
|
+
|
34
|
+
def method_missing(name,*args,&proc)
|
35
|
+
if @exception
|
36
|
+
raise @exception
|
37
|
+
elsif @result.nil?
|
38
|
+
raise UnexecutedRequest.new("You must execute the batch before accessing the result: #{@uri}")
|
39
|
+
else
|
40
|
+
@result.send(name,*args,&proc)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
module Facebooker
|
2
|
+
class Data
|
3
|
+
def initialize(session)
|
4
|
+
@session = session
|
5
|
+
end
|
6
|
+
|
7
|
+
##
|
8
|
+
# ** BETA ***
|
9
|
+
# Sets a cookie on Facebook
|
10
|
+
# +user+ The user for whom this cookie needs to be set.
|
11
|
+
# +name+ Name of the cookie
|
12
|
+
# +value+ Value of the cookie
|
13
|
+
# Optional:
|
14
|
+
# +expires+ Time when the cookie should expire. If not specified, the cookie never expires.
|
15
|
+
# +path+ Path relative to the application's callback URL, with which the cookie should be associated. (default value is /?
|
16
|
+
def set_cookie(user, name, value, expires=nil, path=nil)
|
17
|
+
@session.post('facebook.data.setCookie',
|
18
|
+
:uid => User.cast_to_facebook_id(user),
|
19
|
+
:name => name,
|
20
|
+
:value => value,
|
21
|
+
:expires => expires,
|
22
|
+
:path => path) {|response| response == '1'}
|
23
|
+
end
|
24
|
+
|
25
|
+
##
|
26
|
+
# ** BETA ***
|
27
|
+
# Gets a cookie stored on Facebook
|
28
|
+
# +user+ The user from whom to get the cookies.
|
29
|
+
# Optional:
|
30
|
+
# +name+ The name of the cookie. If not specified, all the cookies for the given user get returned.
|
31
|
+
def get_cookies(user, name=nil)
|
32
|
+
@cookies = @session.post(
|
33
|
+
'facebook.data.getCookies', :uid => User.cast_to_facebook_id(user), :name => name) do |response|
|
34
|
+
response.map do |hash|
|
35
|
+
Cookie.from_hash(hash)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
##
|
41
|
+
# ** BETA ***
|
42
|
+
# Gets a preference stored on Facebook
|
43
|
+
# +pref_id+ The id of the preference to get
|
44
|
+
def get_preference(pref_id)
|
45
|
+
@session.post('facebook.data.getUserPreference', :pref_id=>pref_id)
|
46
|
+
end
|
47
|
+
|
48
|
+
##
|
49
|
+
# ** BETA ***
|
50
|
+
# Sets a preference on Facebook
|
51
|
+
# +pref_id+ The id of the preference to set
|
52
|
+
# +value+ The value to set for this preference
|
53
|
+
def set_preference(pref_id, value)
|
54
|
+
@session.post('facebook.data.setUserPreference', :pref_id=>pref_id, :value=>value)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,78 @@
|
|
1
|
+
module Facebooker
|
2
|
+
module Feed
|
3
|
+
METHODS = {'Action' => 'facebook.feed.publishActionOfUser', 'Story' => 'facebook.feed.publishStoryToUser',
|
4
|
+
'TemplatizedAction' => 'facebook.feed.publishTemplatizedAction' }
|
5
|
+
|
6
|
+
class ActionBase
|
7
|
+
1.upto(4) do |num|
|
8
|
+
attr_accessor "image_#{num}"
|
9
|
+
attr_accessor "image_#{num}_link"
|
10
|
+
end
|
11
|
+
|
12
|
+
def add_image(image,link=nil)
|
13
|
+
1.upto(4) do |num|
|
14
|
+
if send("image_#{num}").blank?
|
15
|
+
send("image_#{num}=",image)
|
16
|
+
send("image_#{num}_link=",link) unless link.nil?
|
17
|
+
return num
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
|
23
|
+
protected
|
24
|
+
def image_params
|
25
|
+
image_hash = {}
|
26
|
+
1.upto(4) do |num|
|
27
|
+
image_attribute = "image_#{num}"
|
28
|
+
image_link_attribute = image_attribute + "_link"
|
29
|
+
self.__send__(image_attribute) ? image_hash[image_attribute] = self.__send__(image_attribute) : nil
|
30
|
+
self.__send__(image_link_attribute) ? image_hash[image_link_attribute] = self.__send__(image_link_attribute) : nil
|
31
|
+
end
|
32
|
+
image_hash
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
##
|
37
|
+
# Representation of a templatized action to be published into a user's news feed
|
38
|
+
class TemplatizedAction < ActionBase
|
39
|
+
attr_accessor :page_actor_id, :title_template, :title_data, :body_template, :body_data, :body_general, :target_ids
|
40
|
+
|
41
|
+
def to_params
|
42
|
+
raise "Must set title_template" if self.title_template.nil?
|
43
|
+
{ :page_actor_id => page_actor_id,
|
44
|
+
:title_template => title_template,
|
45
|
+
:title_data => convert_json(title_data),
|
46
|
+
:body_template => body_template,
|
47
|
+
:body_data => convert_json(body_data),
|
48
|
+
:body_general => body_general,
|
49
|
+
:target_ids => target_ids }.merge image_params
|
50
|
+
end
|
51
|
+
|
52
|
+
def convert_json(hash_or_string)
|
53
|
+
(hash_or_string.is_a?(Hash) and hash_or_string.respond_to?(:to_json)) ? hash_or_string.to_json : hash_or_string
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
##
|
58
|
+
# Representation of a story to be published into a user's news feed.
|
59
|
+
class Story < ActionBase
|
60
|
+
attr_accessor :title, :body
|
61
|
+
|
62
|
+
##
|
63
|
+
# Converts Story to a Hash of its attributes for use as parameters to Facebook REST API calls
|
64
|
+
def to_params
|
65
|
+
raise "Must set title before converting" if self.title.nil?
|
66
|
+
{ :title => title, :body => body }.merge image_params
|
67
|
+
end
|
68
|
+
|
69
|
+
end
|
70
|
+
Action = Story.dup
|
71
|
+
def Action.name
|
72
|
+
"Action"
|
73
|
+
end
|
74
|
+
##
|
75
|
+
# Representation of an action to be published into a user's news feed. Alias for Story.
|
76
|
+
class Action; end
|
77
|
+
end
|
78
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
require 'benchmark'
|
2
|
+
module Facebooker
|
3
|
+
@@logger = nil
|
4
|
+
def self.logger=(logger)
|
5
|
+
@@logger = logger
|
6
|
+
end
|
7
|
+
def self.logger
|
8
|
+
@@logger
|
9
|
+
end
|
10
|
+
|
11
|
+
module Logging
|
12
|
+
|
13
|
+
def self.skip_api_logging=(val)
|
14
|
+
@skip_api_logging=val
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.skip_api_logging
|
18
|
+
@skip_api_logging
|
19
|
+
end
|
20
|
+
|
21
|
+
|
22
|
+
def self.log_fb_api(method, params)
|
23
|
+
message = method # might customize later
|
24
|
+
dump = format_fb_params(params)
|
25
|
+
if block_given?
|
26
|
+
result = nil
|
27
|
+
seconds = Benchmark.realtime { result = yield }
|
28
|
+
log_info(message, dump, seconds) unless skip_api_logging
|
29
|
+
result
|
30
|
+
else
|
31
|
+
log_info(message, dump) unless skip_api_logging
|
32
|
+
nil
|
33
|
+
end
|
34
|
+
rescue Exception => e
|
35
|
+
exception = "#{e.class.name}: #{e.message}: #{dump}"
|
36
|
+
log_info(message, exception)
|
37
|
+
raise
|
38
|
+
end
|
39
|
+
|
40
|
+
def self.format_fb_params(params)
|
41
|
+
params.map { |key,value| "#{key} = #{value}" }.join(', ')
|
42
|
+
end
|
43
|
+
|
44
|
+
def self.log_info(message, dump, seconds = 0)
|
45
|
+
return unless Facebooker.logger
|
46
|
+
log_message = "#{message} (#{seconds}) #{dump}"
|
47
|
+
Facebooker.logger.debug(log_message)
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module Facebooker
|
2
|
+
class Mobile
|
3
|
+
def initialize(session)
|
4
|
+
@session = session
|
5
|
+
end
|
6
|
+
|
7
|
+
# Used to determine whether the user identified by "uid" has enabled SMS for this application.
|
8
|
+
def can_send(user)
|
9
|
+
@session.post('facebook.sms.canSend', :uid => User.cast_to_facebook_id(user))
|
10
|
+
end
|
11
|
+
|
12
|
+
# Send the given message to the user.
|
13
|
+
# See http://wiki.developers.facebook.com/index.php/Mobile
|
14
|
+
def send(user, message)
|
15
|
+
@session.post('facebook.sms.send',
|
16
|
+
{:uid => User.cast_to_facebook_id(user),
|
17
|
+
:message => message}, false)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
require 'digest/md5'
|
2
|
+
require 'facebooker/service'
|
3
|
+
|
4
|
+
module Facebooker
|
5
|
+
# A mock service that reads the Facebook response from fixtures
|
6
|
+
# Adapted from http://gist.github.com/44344
|
7
|
+
#
|
8
|
+
# Facebooker::MockService.fixture_path = 'path/to/dir'
|
9
|
+
# Facebooker::Session.current = Facebooker::MockSession.create
|
10
|
+
#
|
11
|
+
class MockService < Service
|
12
|
+
class << self
|
13
|
+
attr_accessor :fixture_path
|
14
|
+
end
|
15
|
+
|
16
|
+
def read_fixture(method, filename, original = nil)
|
17
|
+
path = fixture_path(method, filename)
|
18
|
+
File.read path
|
19
|
+
rescue Errno::ENAMETOOLONG
|
20
|
+
read_fixture(method, hash_fixture_name(filename), filename)
|
21
|
+
rescue Errno::ENOENT => e
|
22
|
+
if File.exists?(fixture_path(method, 'default'))
|
23
|
+
File.read fixture_path(method, 'default')
|
24
|
+
else
|
25
|
+
e.message << "\n(Non-hashed path is #{original})" if original
|
26
|
+
e.message << "\nFacebook API Reference: http://wiki.developers.facebook.com/index.php/#{method.sub(/^facebook\./, '')}#Example_Return_XML"
|
27
|
+
raise e
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def post(params)
|
32
|
+
method = params.delete(:method)
|
33
|
+
params.delete_if {|k,_| [:v, :api_key, :call_id, :sig].include?(k) }
|
34
|
+
Parser.parse(method, read_fixture(method, fixture_name(params)))
|
35
|
+
end
|
36
|
+
|
37
|
+
private
|
38
|
+
def fixture_path(method, filename)
|
39
|
+
File.join(self.class.fixture_path, method, "#{filename}.xml")
|
40
|
+
end
|
41
|
+
|
42
|
+
def hash_fixture_name(filename)
|
43
|
+
Digest::MD5.hexdigest(filename)
|
44
|
+
end
|
45
|
+
|
46
|
+
def fixture_name(params)
|
47
|
+
params.map {|*args| args.join('=') }.sort.join('&')
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'facebooker/session'
|
2
|
+
|
3
|
+
module Facebooker
|
4
|
+
class MockSession < Session
|
5
|
+
def secured?
|
6
|
+
true
|
7
|
+
end
|
8
|
+
|
9
|
+
def secure!
|
10
|
+
@uid = 1
|
11
|
+
true
|
12
|
+
end
|
13
|
+
|
14
|
+
def service
|
15
|
+
@service ||= MockService.new(Facebooker.api_server_base, Facebooker.api_rest_path, @api_key)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,135 @@
|
|
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
|
+
|
81
|
+
def id_is(attribute)
|
82
|
+
class_eval <<-EOS
|
83
|
+
def #{attribute}=(value)
|
84
|
+
@#{attribute} = value.to_i
|
85
|
+
end
|
86
|
+
|
87
|
+
attr_reader #{attribute.inspect}
|
88
|
+
alias :id #{attribute.inspect}
|
89
|
+
alias :id= #{"#{attribute}=".to_sym.inspect}
|
90
|
+
EOS
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
##
|
95
|
+
# Centralized, error-checked place for a model to get the session to which it is bound.
|
96
|
+
# Any Facebook API queries require a Session instance.
|
97
|
+
def session
|
98
|
+
@session || (raise UnboundSessionException, "Must bind this object to a Facebook session before querying")
|
99
|
+
end
|
100
|
+
|
101
|
+
#
|
102
|
+
# This gets populated from FQL queries.
|
103
|
+
def anon=(value)
|
104
|
+
@anonymous_fields = value
|
105
|
+
end
|
106
|
+
|
107
|
+
def initialize(hash = {})
|
108
|
+
populate_from_hash!(hash)
|
109
|
+
end
|
110
|
+
|
111
|
+
def populate
|
112
|
+
raise NotImplementedError, "#{self.class} included me and should have overriden me"
|
113
|
+
end
|
114
|
+
|
115
|
+
def populated?
|
116
|
+
!@populated.nil?
|
117
|
+
end
|
118
|
+
|
119
|
+
##
|
120
|
+
# Set model's attributes via Hash. Keys should map directly to the model's attribute names.
|
121
|
+
def populate_from_hash!(hash)
|
122
|
+
unless hash.nil? || hash.empty?
|
123
|
+
hash.each do |key, value|
|
124
|
+
set_attr_method = "#{key}="
|
125
|
+
if !value.nil? && respond_to?(set_attr_method)
|
126
|
+
self.__send__(set_attr_method, value)
|
127
|
+
else
|
128
|
+
Facebooker::Logging.log_info("**Warning**, Attempt to set non-attribute: #{key}",hash)
|
129
|
+
end
|
130
|
+
end
|
131
|
+
@populated = true
|
132
|
+
end
|
133
|
+
end
|
134
|
+
end
|
135
|
+
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,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,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 :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
|
+
|
26
|
+
id_is :eid
|
27
|
+
end
|
28
|
+
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 :flid, :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, :gid, :update_time, :group_subtype, :group_type, :website, :office, :description, :venue, :nid
|
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
|