Class: Facebooker::Rails::Publisher
- Inherits:
-
Object
- Object
- Facebooker::Rails::Publisher
- Defined in:
- lib/facebooker/rails/publisher.rb
Overview
ActionMailer like module for publishing Facbook messages
To use, create a subclass and define methods Each method should start by calling send_as to specify the type of message Valid options are :email and :notification, :user_action, :profile, :ref, :publish_stream
Below is an example of each type
class TestPublisher < Facebooker::Rails::Publisher
# The new message templates are supported as well
# First, create a method that contains your templates:
# You may include multiple one line story templates and short story templates
# but only one full story template
# Your most specific template should be first
#
# Before using, you must register your template by calling register. For this example
# You would call TestPublisher.register_publish_action
# Registering the template will store the template id returned from Facebook in the
# facebook_templates table that is created when you create your first publisher
def publish_action_template
one_line_story_template "{*actor*} did stuff with {*friend*}"
one_line_story_template "{*actor*} did stuff"
short_story_template "{*actor*} has a title {*friend*}", render(:partial=>"short_body")
short_story_template "{*actor*} has a title", render(:partial=>"short_body")
full_story_template "{*actor*} has a title {*friend*}", render(:partial=>"full_body")
action_links action_link("My text {*template_var*}","{*link_url*}")
end
# To send a registered template, you need to create a method to set the data
# The publisher will look up the template id from the facebook_templates table
def publish_action(f)
send_as :user_action
from f
story_size SHORT # or ONE_LINE or FULL
data :friend=>"Mike"
end
# Provide a from user to send a general notification
# if from is nil, this will send an announcement
def notification(to,f)
send_as :notification
recipients to
from f
fbml "Not"
end
def email(to,f)
send_as :email
recipients to
from f
title "Email"
fbml 'text'
text fbml
end
# This will render the profile in /users/profile.fbml.erb
# it will set @user to user_to_update in the template
# The mobile profile will be rendered from the app/views/test_publisher/_mobile.erb
# template
def profile_update(user_to_update,user_with_session_to_use)
send_as :profile
from user_with_session_to_use
recipients user_to_update
profile render(:file=>"users/profile.fbml.erb",:assigns=>{:user=>user_to_update})
profile_action "A string"
mobile_profile render(:partial=>"mobile",:assigns=>{:user=>user_to_update})
end
# Update the given handle ref with the content from a
# template
def ref_update(user)
send_as :ref
from user
fbml render(:file=>"users/profile",:assigns=>{:user=>user_to_update})
handle "a_ref_handle"
end
# Publish a post into the stream on the user's Wall and News Feed.
def publish_stream(user_with_session_to_use, user_to_update, params)
send_as :publish_stream
from user_with_session_to_use
target user_to_update
attachment params[:attachment]
message params[:message]
action_links params[:action_links]
end
To send a message, use ActionMailer like semantics
TestPublisher.deliver_action(@user)
For testing, you may want to create an instance of the underlying message without sending it
TestPublisher.create_action(@user)
will create and return an instance of Facebooker::Feeds::Action
Publisher makes many helpers available, including the linking and asset helpers
Defined Under Namespace
Classes: Email, FacebookTemplate, ImageHolder, InvalidSender, Notification, Profile, PublishStream, PublisherController, Ref, UnknownBodyType, UnspecifiedBodyType, UserAction
Constant Summary collapse
- ONE_LINE =
story sizes from the Facebooker API
1- SHORT =
2- FULL =
4
Instance Attribute Summary collapse
-
#_body ⇒ Object
Returns the value of attribute _body.
- #action_links(*links) ⇒ Object
-
#one_line_story_templates ⇒ Object
Returns the value of attribute one_line_story_templates.
-
#short_story_templates ⇒ Object
Returns the value of attribute short_story_templates.
Class Method Summary collapse
-
.add_template_helper(helper_module) ⇒ Object
:nodoc:.
- .bundle_id_for_method(method_name) ⇒ Object
- .controller_path ⇒ Object
- .default_url_options ⇒ Object
- .helper(*args) ⇒ Object
- .inherited(child) ⇒ Object
- .method_missing(name, *args) ⇒ Object
- .register_all_templates ⇒ Object
- .register_all_templates_on_all_applications ⇒ Object
- .unregister_inactive_templates ⇒ Object
Instance Method Summary collapse
- #action_link(text, target) ⇒ Object
- #announcement_notification?(from, body) ⇒ Boolean
- #default_url_options ⇒ Object
- #from(*args) ⇒ Object
- #full_story_template(title = nil, body = nil, params = {}) ⇒ Object
- #image(src, target) ⇒ Object
-
#initialize ⇒ Publisher
constructor
A new instance of Publisher.
- #initialize_template_class(assigns) ⇒ Object
-
#logger ⇒ Object
nodoc needed for actionview.
- #method_missing(name, *args) ⇒ Object
- #one_line_story_template(str) ⇒ Object
- #profile_update?(body) ⇒ Boolean
- #recipients(*args) ⇒ Object
- #ref_update?(body) ⇒ Boolean
-
#render(opts) ⇒ Object
nodoc delegate to action view.
-
#request_comes_from_facebook? ⇒ Boolean
use facebook options everywhere.
- #requires_from_user?(from, body) ⇒ Boolean
- #send_as(option) ⇒ Object
- #send_message(method) ⇒ Object
- #short_story_template(title, body, params = {}) ⇒ Object
Constructor Details
#initialize ⇒ Publisher
Returns a new instance of Publisher.
107 108 109 110 111 112 |
# File 'lib/facebooker/rails/publisher.rb', line 107 def initialize @from = nil @full_story_template = nil @recipients = nil @controller = PublisherController.new(self) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args) ⇒ Object
345 346 347 348 349 350 351 352 353 |
# File 'lib/facebooker/rails/publisher.rb', line 345 def method_missing(name,*args) if args.size==1 and self._body.respond_to?("#{name}=") self._body.send("#{name}=",*args) elsif self._body.respond_to?(name) self._body.send(name,*args) else super end end |
Instance Attribute Details
#_body ⇒ Object
Returns the value of attribute _body.
275 276 277 |
# File 'lib/facebooker/rails/publisher.rb', line 275 def _body @_body end |
#action_links(*links) ⇒ Object
337 338 339 340 341 342 343 |
# File 'lib/facebooker/rails/publisher.rb', line 337 def action_links(*links) if links.blank? @action_links else @action_links = links end end |
#one_line_story_templates ⇒ Object
Returns the value of attribute one_line_story_templates.
220 221 222 |
# File 'lib/facebooker/rails/publisher.rb', line 220 def one_line_story_templates @one_line_story_templates end |
#short_story_templates ⇒ Object
Returns the value of attribute short_story_templates.
220 221 222 |
# File 'lib/facebooker/rails/publisher.rb', line 220 def short_story_templates @short_story_templates end |
Class Method Details
.add_template_helper(helper_module) ⇒ Object
:nodoc:
566 567 568 569 |
# File 'lib/facebooker/rails/publisher.rb', line 566 def add_template_helper(helper_module) #:nodoc: master_helper_module.send :include,helper_module include master_helper_module end |
.bundle_id_for_method(method_name) ⇒ Object
127 128 129 |
# File 'lib/facebooker/rails/publisher.rb', line 127 def self.bundle_id_for_method(method_name) FacebookTemplate.bundle_id_for_class_and_method(self,method_name) end |
.controller_path ⇒ Object
551 552 553 |
# File 'lib/facebooker/rails/publisher.rb', line 551 def controller_path self.to_s.underscore end |
.default_url_options ⇒ Object
114 115 116 |
# File 'lib/facebooker/rails/publisher.rb', line 114 def self. {:host => Facebooker.canvas_server_base + Facebooker.facebook_path_prefix} end |
.helper(*args) ⇒ Object
555 556 557 558 559 560 561 562 563 564 |
# File 'lib/facebooker/rails/publisher.rb', line 555 def helper(*args) args.each do |arg| case arg when Symbol,String add_template_helper("#{arg.to_s.camelcase}Helper".constantize) when Module add_template_helper(arg) end end end |
.inherited(child) ⇒ Object
572 573 574 575 576 577 578 |
# File 'lib/facebooker/rails/publisher.rb', line 572 def inherited(child) super child.master_helper_module=Module.new child.master_helper_module.__send__(:include,self.master_helper_module) child.send(:include, child.master_helper_module) FacebookTemplate.clear_cache! end |
.method_missing(name, *args) ⇒ Object
526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 |
# File 'lib/facebooker/rails/publisher.rb', line 526 def method_missing(name,*args) should_send = false method = '' if md = /^create_(.*)$/.match(name.to_s) method = md[1] elsif md = /^deliver_(.*)$/.match(name.to_s) method = md[1] should_send = true elsif md = /^register_(.*)$/.match(name.to_s) return FacebookTemplate.register(self, md[1]) else super end #now create the item (publisher=new).send(method,*args) case publisher._body when UserAction publisher._body.template_name = method publisher._body.template_id ||= FacebookTemplate.bundle_id_for_class_and_method(self,method) end should_send ? publisher.(method) : publisher._body end |
.register_all_templates ⇒ Object
508 509 510 511 512 513 514 515 |
# File 'lib/facebooker/rails/publisher.rb', line 508 def register_all_templates all_templates = instance_methods.grep(/_template$/) - %w(short_story_template full_story_template one_line_story_template) all_templates.each do |template| template_name=template.sub(/_template$/,"") puts "Registering #{template_name}" send("register_"+template_name) end end |
.register_all_templates_on_all_applications ⇒ Object
500 501 502 503 504 505 |
# File 'lib/facebooker/rails/publisher.rb', line 500 def register_all_templates_on_all_applications Facebooker.with_all_applications do puts "Registering templates for #{Facebooker.api_key}" register_all_templates end end |
.unregister_inactive_templates ⇒ Object
517 518 519 520 521 522 523 524 |
# File 'lib/facebooker/rails/publisher.rb', line 517 def unregister_inactive_templates session = Facebooker::Session.create active_template_ids = FacebookTemplate.all.map(&:bundle_id) all_template_ids = session.active_template_bundles.map {|t| t["template_bundle_id"]} (all_template_ids - active_template_ids).each do |template_bundle_id| session.deactivate_template_bundle_by_id(template_bundle_id) end end |
Instance Method Details
#action_link(text, target) ⇒ Object
376 377 378 |
# File 'lib/facebooker/rails/publisher.rb', line 376 def action_link(text,target) {:text=>text, :href=>target} end |
#announcement_notification?(from, body) ⇒ Boolean
392 393 394 |
# File 'lib/facebooker/rails/publisher.rb', line 392 def announcement_notification?(from,body) from.nil? and body.is_a?(Notification) end |
#default_url_options ⇒ Object
118 119 120 |
# File 'lib/facebooker/rails/publisher.rb', line 118 def self.class. end |
#from(*args) ⇒ Object
285 286 287 288 289 290 291 |
# File 'lib/facebooker/rails/publisher.rb', line 285 def from(*args) if args.size==0 @from else @from=args.first end end |
#full_story_template(title = nil, body = nil, params = {}) ⇒ Object
319 320 321 322 323 324 325 |
# File 'lib/facebooker/rails/publisher.rb', line 319 def full_story_template(title=nil,body=nil,params={}) if title.nil? @full_story_template else @full_story_template=params.merge(:template_title=>title, :template_body=>body) end end |
#image(src, target) ⇒ Object
372 373 374 |
# File 'lib/facebooker/rails/publisher.rb', line 372 def image(src,target) ImageHolder.new(image_path(src),target.respond_to?(:to_str) ? target : url_for(target)) end |
#initialize_template_class(assigns) ⇒ Object
447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 |
# File 'lib/facebooker/rails/publisher.rb', line 447 def initialize_template_class(assigns) template_root = "#{RAILS_ROOT}/app/views" controller_root = File.join(template_root,self.class.controller_path) #only do this on Rails 2.1 if ActionController::Base.respond_to?(:append_view_path) # only add the view path once unless ActionController::Base.view_paths.include?(controller_root) ActionController::Base.append_view_path(controller_root) ActionController::Base.append_view_path(controller_root+"/..") end view_paths = ActionController::Base.view_paths else view_paths = [template_root, controller_root] end returning ActionView::Base.new(view_paths, assigns, self) do |template| template.controller=self template.extend(self.class.master_helper_module) def template.request_comes_from_facebook? true end end end |
#logger ⇒ Object
nodoc needed for actionview
434 435 436 |
# File 'lib/facebooker/rails/publisher.rb', line 434 def logger RAILS_DEFAULT_LOGGER end |
#one_line_story_template(str) ⇒ Object
327 328 329 330 |
# File 'lib/facebooker/rails/publisher.rb', line 327 def one_line_story_template(str) @one_line_story_templates ||= [] @one_line_story_templates << str end |
#profile_update?(body) ⇒ Boolean
384 385 386 |
# File 'lib/facebooker/rails/publisher.rb', line 384 def profile_update?(body) body.is_a?(Profile) end |
#recipients(*args) ⇒ Object
277 278 279 280 281 282 283 |
# File 'lib/facebooker/rails/publisher.rb', line 277 def recipients(*args) if args.size==0 @recipients else @recipients=args.first end end |
#ref_update?(body) ⇒ Boolean
388 389 390 |
# File 'lib/facebooker/rails/publisher.rb', line 388 def ref_update?(body) body.is_a?(Ref) end |
#render(opts) ⇒ Object
nodoc delegate to action view. Set up assigns and render
440 441 442 443 444 |
# File 'lib/facebooker/rails/publisher.rb', line 440 def render(opts) opts = opts.dup body = opts.delete(:assigns) || {} initialize_template_class(body.dup.merge(:controller=>self)).render(opts) end |
#request_comes_from_facebook? ⇒ Boolean
use facebook options everywhere
123 124 125 |
# File 'lib/facebooker/rails/publisher.rb', line 123 def request_comes_from_facebook? true end |
#requires_from_user?(from, body) ⇒ Boolean
380 381 382 |
# File 'lib/facebooker/rails/publisher.rb', line 380 def requires_from_user?(from,body) ! (announcement_notification?(from,body) or ref_update?(body) or profile_update?(body)) end |
#send_as(option) ⇒ Object
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 |
# File 'lib/facebooker/rails/publisher.rb', line 294 def send_as(option) self._body=case option when :action Facebooker::Feed::Action.new when :story Facebooker::Feed::Story.new when :templatized_action Facebooker::Feed::TemplatizedAction.new when :notification Notification.new when :email Email.new when :profile Profile.new when :ref Ref.new when :user_action UserAction.new when :publish_stream PublishStream.new else raise UnknownBodyType.new("Unknown type to publish") end end |
#send_message(method) ⇒ Object
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 |
# File 'lib/facebooker/rails/publisher.rb', line 396 def (method) @recipients = @recipients.is_a?(Array) ? @recipients : [@recipients] if from.nil? and @recipients.size==1 and requires_from_user?(from,_body) @from = @recipients.first end # notifications can # omit the from address raise InvalidSender.new("Sender must be a Facebooker::User") unless from.is_a?(Facebooker::User) || !requires_from_user?(from,_body) case _body when Facebooker::Feed::TemplatizedAction,Facebooker::Feed::Action from.publish_action(_body) when Facebooker::Feed::Story @recipients.each {|r| r.publish_story(_body)} when Notification (from.nil? ? Facebooker::Session.create : from.session).send_notification(@recipients,_body.fbml) when Email from.session.send_email(@recipients, _body.title, _body.text, _body.fbml) when Profile # If recipient and from aren't the same person, create a new user object using the # userid from recipient and the session from from @from = Facebooker::User.new(Facebooker::User.cast_to_facebook_id(@recipients.first),Facebooker::Session.create) @from.set_profile_fbml(_body.profile, _body.mobile_profile, _body.profile_action, _body.profile_main) when Ref Facebooker::Session.create.server_cache.set_ref_handle(_body.handle,_body.fbml) when UserAction @from.session.publish_user_action(_body.template_id,_body.data_hash,_body.target_ids,_body.body_general,_body.story_size) when PublishStream @from.publish_to(_body.target, {:attachment => _body., :action_links => @action_links, :message => _body. }) else raise UnspecifiedBodyType.new("You must specify a valid send_as") end end |
#short_story_template(title, body, params = {}) ⇒ Object
332 333 334 335 |
# File 'lib/facebooker/rails/publisher.rb', line 332 def short_story_template(title,body,params={}) @short_story_templates ||= [] @short_story_templates << params.merge(:template_title=>title, :template_body=>body) end |