forked from Holek/facebook_share
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
85 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,73 @@ | ||
module FacebookShare | ||
|
||
def facebook_share_once(app_id, params = {}) | ||
facebook_script_tags app_id, facebook_share('.fb_share', params) | ||
INIT_PARAMS = %w(status cookie xfbml) | ||
REMOVE_PARAMS = %w(app_id selector status cookie xfbml locale) | ||
|
||
class << self | ||
attr_accessor :default_facebook_share_options | ||
end | ||
|
||
def default_facebook_share_options | ||
{:app_id => "0", :selector => ".fb_share", :url => request.url, :locale => "en_US", :display => "popup"} | ||
#.merge(FacebookShare.default_facebook_share_options || {}) | ||
end | ||
|
||
def facebook_share(selector, params = {}) | ||
available_params = { | ||
:display => "popup" | ||
}.merge(params) | ||
def facebook_share_once(options = {}) | ||
facebook_script_tags options, facebook_share( options ) | ||
end | ||
|
||
def facebook_share(options = {}) | ||
options = default_facebook_share_options.merge(options) | ||
script = <<-JS | ||
$("#{selector}").unbind("click.facebook_share").bind("click.facebook_share",function () { | ||
FB.ui( | ||
{ | ||
method: \'stream.publish\' | ||
#{build_params(available_params)} | ||
}); | ||
return false; | ||
}); | ||
FB.ui({method: \'stream.publish\'#{build_params(options)}}); | ||
return false; | ||
}); | ||
JS | ||
script | ||
end | ||
|
||
def facebook_script_tags(app_id, initial_script = "") | ||
def facebook_script_tags(options = {}, initial_script = "") | ||
options = default_facebook_share_options.merge(options) | ||
script = <<-JS | ||
<script type="text/javascript" src="https://connect.facebook.net/de_DE/all.js"></script> | ||
#{facebook_connect_js_tag(options)} | ||
<script type="text/javascript"> | ||
/* <![CDATA[ */ | ||
FB.init({appId:"#{app_id}", xfbml : true}); | ||
#{facebook_init_script(options)} | ||
#{initial_script} | ||
/* ]]> */ | ||
</script> | ||
JS | ||
script | ||
html_safe_string(script) | ||
end | ||
|
||
def facebook_connect_js_tag(options = {}) | ||
options = default_facebook_share_options.merge(options) | ||
html_safe_string("<script type=\"text/javascript\" src=\"https://connect.facebook.net/#{default_facebook_share_options.merge(options)[:locale]}/all.js\"></script>") | ||
end | ||
|
||
private | ||
def build_params(available_params) | ||
script = "" | ||
available_params.each do |key, value| | ||
if value | ||
value_sanitized = value.gsub(/"/, '\"') | ||
script << ", #{key}: \"#{value_sanitized}\"" | ||
end | ||
def facebook_init_script(options = {}) | ||
options = default_facebook_share_options.merge(options) | ||
params = build_params options, true | ||
html_safe_string("FB.init({appId:\"#{options[:app_id]}\"#{params}});") | ||
end | ||
|
||
def build_params(options, for_init = false) | ||
script = "" | ||
options.each do |key, value| | ||
param_check = ( for_init ) ? INIT_PARAMS.include?(key) : !(REMOVE_PARAMS.include?(key)) | ||
# if it's for init script, include only status, cookie and xfbml | ||
# if it's for stream.publish, include all except for initial | ||
if value && param_check | ||
value_sanitized = value.gsub(/"/, '\"') | ||
script << ", #{key}: \"#{value_sanitized}\"" | ||
end | ||
script | ||
end | ||
script | ||
end | ||
|
||
def html_safe_string(str) | ||
@use_html_safe ||= "".respond_to?(:html_safe) | ||
@use_html_safe ? str.html_safe : str | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
module FacebookShare | ||
VERSION = "0.0.1" | ||
VERSION = "0.0.2" | ||
end |