Module: Eukaliptus::FacebookHelpers

Defined in:
lib/eukaliptus/view_helpers/facebook_helpers.rb

Instance Method Summary collapse

Instance Method Details

#fb_add_friend(name = 'Add as a friend', options = {}) ⇒ Object

Options

:id Required. The ID or username of the target user to add as a friend.

:callback JS callback function to do somethink with the data



85
86
87
88
89
90
91
92
# File 'lib/eukaliptus/view_helpers/facebook_helpers.rb', line 85

def fb_add_friend(name = 'Add as a friend', options = {})
  options = {
    :method => 'friends',
    :callback => 'function(response) {}'
  }.merge(options)

  fb_ui name, options
end

#fb_app_request(name = 'Invite to use this application', options = {}) ⇒ Object

Options

:message The request the receiving user will see. It appears as a question posed by the sending user. The maximum length is 255 characters.

:to A user ID or username. Must be a friend of the sender. If this is specified, the user will not have a choice of recipients. If this is omitted, the user will see a friend selector and will be able to select a maximum of 50 recipients.

:data Optional, additional data you may pass for tracking. This will be stored as part of the request objects created.

:title Optional, the title for the friend selector dialog. Maximum length is 50 characters.

:callback JS callback function to do somethink with the data



113
114
115
116
117
118
119
120
121
# File 'lib/eukaliptus/view_helpers/facebook_helpers.rb', line 113

def fb_app_request(name = 'Invite to use this application', options = {})
  options = {
    :method => 'apprequests',
    :message => 'Invite your friends to use your app!',
    :callback => 'function(response) {}'
  }.merge(options)

  fb_ui name, options
end

#fb_init(options = {}) ⇒ Object

Renders the HTML + JS that enables the app to log in to Facebook using the Javascript method.

Accepts the permissions that your application needs to work. Use the second parameter to inject some JS that will be executed in fbAsyncInit.

Use it one time in your layout header for all pages, or add it to individual templates to ask the user for different permissions depending on the context, page, etc.



16
17
18
19
20
21
22
23
24
# File 'lib/eukaliptus/view_helpers/facebook_helpers.rb', line 16

def fb_init(options = {})
  perms = (options[:perms] || %w{email publish_stream}).join(",")
  locale = options[:locale] || "en_US"

  template = Erubis::Eruby.new File.new(File.dirname(__FILE__) + "/../templates/fb_init.erb").read
  js = template.result(:perms => perms, :locale => locale)
  
  js.html_safe
end

#fb_login(perms = %w{email publish_stream})) ⇒ Object

Deprecated



4
5
6
# File 'lib/eukaliptus/view_helpers/facebook_helpers.rb', line 4

def (perms = %w{email publish_stream})
  raise 'fb_login is a deprecated method. Please use use fb_init instead.'
end

#fb_pay(name = 'Buy credits', options = {}) ⇒ Object

Options

:credits_purchase Whether it is a Credits purchase dialog or not

:order_info The internal key of the item you are selling. IT’s required in case credits_purchase is false and should only be meaningful to you.

:dev_purchase_params More developer parameters. For details, read developers.facebook.com/docs/creditsapi/

:callback JS callback function to do somethink with the data



137
138
139
140
141
142
143
144
# File 'lib/eukaliptus/view_helpers/facebook_helpers.rb', line 137

def fb_pay(name = 'Buy credits', options = {})
  options = {
    :method => 'pay',
    :callback => 'function(response) {}'
  }.merge(options)

  fb_ui name, options
end

#fb_post_to_wall(name = 'Post to wall', options = {}) ⇒ Object

Create a new wall post You can use it directly in your templates or call it from inside your helpers:

module MyHelper

def post_something_to_wall(something)
  fb_post_to_wall('Publish this great thing to wall!', :name => Something.name)
end

end

The options are the same than in Facebook feed post passed as Hash see: developers.facebook.com/docs/reference/javascript/FB.ui/

:callback JS callback function to do somethink with the data



69
70
71
72
73
74
75
76
# File 'lib/eukaliptus/view_helpers/facebook_helpers.rb', line 69

def fb_post_to_wall(name = 'Post to wall', options = {})
  options = {
    :method => 'feed',
    :callback => 'function(response) {}'
  }.merge(options)

  fb_ui name, options
end

#fb_ui(name, options = {}) ⇒ Object

Function to create a new FB.ui dialog link



47
48
49
50
51
52
53
# File 'lib/eukaliptus/view_helpers/facebook_helpers.rb', line 47

def fb_ui(name, options ={})
  callback = options.delete(:callback)
  callback.gsub!('"', "'")

  data = "FB.ui(#{options.to_json.gsub('"', "'")}, #{callback});"
  link_to(name, '#', :onclick => data.html_safe)
end

#with_fb(script = nil, &script_block) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/eukaliptus/view_helpers/facebook_helpers.rb', line 26

def with_fb(script = nil, &script_block)
  code = script_block.try(:call) || script || ""
  scriptlet = <<-DATA
    <script>
      (function() {
        var oldFBInit = window.fbAsyncInit;
        var fn = function() {
          if (typeof(oldFBInit) === "function") {
            oldFBInit();
          }
          #{code}
        }
        
        typeof(FB) !== "undefined" ? fn() : (window.fbAsyncInit = fn);
      }())
    </script>      
  DATA
  scriptlet.html_safe
end