facebook_share 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +47 -10
- data/lib/facebook_share.rb +55 -26
- data/lib/facebook_share/version.rb +1 -1
- metadata +4 -4
data/README.md
CHANGED
@@ -1,24 +1,61 @@
|
|
1
1
|
# Facebook Share
|
2
2
|
|
3
|
-
This gem will add an easy-to-use Facebook Share button feature to your Rails project.
|
3
|
+
This gem will add an easy-to-use Facebook Share button feature to your Rails project. This gem does not take care of authentication or authorization. It's only purpose is to bind Facebook Share button to anything you want.
|
4
4
|
|
5
|
-
|
5
|
+
Any public method will return just JavaScript code and nothing else.
|
6
6
|
|
7
|
-
|
7
|
+
## How to install
|
8
|
+
|
9
|
+
This gem relies on jQuery, be sure to have it installed in your project. It does not depend on jquery-rails gem, because some projects use jQuery without it.
|
8
10
|
|
9
11
|
gem install facebook_share
|
10
12
|
|
11
|
-
|
13
|
+
## Code changes
|
14
|
+
|
15
|
+
If you don't have a Facebook Application for your project yet, [create one](http://www.facebook.com/developers/createapp.php).
|
16
|
+
|
17
|
+
Then add this to your ApplicationHelper
|
18
|
+
|
19
|
+
module ApplicationHelper
|
20
|
+
include FacebookShare
|
21
|
+
|
22
|
+
FacebookShare.default_facebook_share_options = {
|
23
|
+
:app_id => "YOUR_APP_ID",
|
24
|
+
:status => false,
|
25
|
+
:cookie => false,
|
26
|
+
:xfbml => false,
|
27
|
+
|
28
|
+
:selector => '.fb_share',
|
29
|
+
:locale => "en_US"
|
30
|
+
}
|
31
|
+
end
|
32
|
+
|
33
|
+
You can ommit *app_id* parameter, if you already have a Facebook Application initialized in your project.
|
34
|
+
|
35
|
+
Be sure you have <div id="fb-root"></div> in your application layout before you load the Facebook Connect JS
|
36
|
+
|
37
|
+
Default facebook Share options can be changed with the above code snippet
|
38
|
+
|
39
|
+
* *appid* - your Facebook application ID that will connect your site to Facebook
|
40
|
+
* *status*. *cookie* and *xfbml* - as described at [FB.init JS SDK](http://developers.facebook.com/docs/reference/javascript/fb.init/)
|
41
|
+
* *locale* - Facebook locale code representations, ie. en_US, de_DE, pl_PL, etc. The full list of Facebook supported languages is available in http://www.facebook.com/translations/FacebookLocales.xml or at [Facebook Developer Wiki](http://fbdevwiki.com/wiki/Locales)
|
42
|
+
* *selector* - a selector to target Facebook share binding, ".fb_share" by default
|
43
|
+
* any other parameter will be passed to Facebook's **[FB.ui](http://developers.facebook.com/docs/reference/javascript/fb.ui/)** function, so you can use whichever parameters you need, except for *method*, which defaults always to *publish.stream*
|
44
|
+
|
45
|
+
## Usage
|
46
|
+
|
47
|
+
The simplest usage (given you specified your project's Facebook Application ID) is as follows:
|
12
48
|
|
13
|
-
|
49
|
+
<%= link_to 'Share on Facebook', '#', :class => "fb_share" %>
|
50
|
+
<%= facebook_share_once %>
|
14
51
|
|
15
|
-
|
52
|
+
That will produce a link "Share on Facebook" with a class of "fb_share" and a corresponding JavaScript script tags initializing Facebook app and sharing method bind to click on that link. By default gem passes ".fb_share" selector to jQuery.
|
16
53
|
|
17
|
-
##
|
54
|
+
## Todo
|
18
55
|
|
19
|
-
|
56
|
+
* add tests
|
20
57
|
|
21
|
-
## Note on
|
58
|
+
## Note on patches/pull requests
|
22
59
|
|
23
60
|
* Fork the project.
|
24
61
|
* Create a feature branch
|
@@ -29,7 +66,7 @@ at the top of your ApplicationHelper
|
|
29
66
|
|
30
67
|
## Copyright
|
31
68
|
|
32
|
-
Copyright (c) 2011 Mike Połtyn.
|
69
|
+
Copyright (c) 2011 Mike Połtyn. Originally build as part of work at [Railslove](http://railslove.com).
|
33
70
|
|
34
71
|
## License
|
35
72
|
|
data/lib/facebook_share.rb
CHANGED
@@ -1,49 +1,78 @@
|
|
1
1
|
module FacebookShare
|
2
|
+
INIT_PARAMS = %w(status cookie xfbml)
|
3
|
+
REMOVE_PARAMS = %w(app_id selector status cookie xfbml locale)
|
2
4
|
|
3
|
-
|
4
|
-
|
5
|
+
class << self
|
6
|
+
attr_accessor :default_facebook_share_options
|
5
7
|
end
|
6
8
|
|
7
|
-
def
|
8
|
-
|
9
|
+
def default_facebook_share_options
|
10
|
+
{
|
11
|
+
:app_id => "0",
|
12
|
+
:selector => ".fb_share",
|
13
|
+
:link => request.url,
|
14
|
+
:locale => "en_US",
|
9
15
|
:display => "popup"
|
10
|
-
}.merge(
|
16
|
+
}.merge(FacebookShare.default_facebook_share_options || {})
|
17
|
+
end
|
18
|
+
|
19
|
+
def facebook_share_once(options = {})
|
20
|
+
facebook_script_tags options, facebook_share( options )
|
21
|
+
end
|
11
22
|
|
23
|
+
def facebook_share(options = {})
|
24
|
+
options = default_facebook_share_options.merge(options)
|
12
25
|
script = <<-JS
|
13
|
-
$("#{selector}").unbind("click.facebook_share").bind("click.facebook_share",function () {
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
#{build_params(available_params)}
|
18
|
-
});
|
19
|
-
return false;
|
20
|
-
});
|
26
|
+
$("#{options[:selector]}").unbind("click.facebook_share").bind("click.facebook_share",function () {
|
27
|
+
FB.ui({method: \'stream.publish\'#{build_params(options)}});
|
28
|
+
return false;
|
29
|
+
});
|
21
30
|
JS
|
22
31
|
script
|
23
32
|
end
|
24
33
|
|
25
|
-
def facebook_script_tags(
|
34
|
+
def facebook_script_tags(options = {}, initial_script = "")
|
35
|
+
options = default_facebook_share_options.merge(options)
|
26
36
|
script = <<-JS
|
27
|
-
|
37
|
+
#{facebook_connect_js_tag(options)}
|
28
38
|
<script type="text/javascript">
|
29
39
|
/* <![CDATA[ */
|
30
|
-
|
40
|
+
#{facebook_init_script(options)}
|
31
41
|
#{initial_script}
|
32
42
|
/* ]]> */
|
33
43
|
</script>
|
34
44
|
JS
|
35
|
-
script
|
45
|
+
html_safe_string(script)
|
46
|
+
end
|
47
|
+
|
48
|
+
def facebook_connect_js_tag(options = {})
|
49
|
+
options = default_facebook_share_options.merge(options)
|
50
|
+
html_safe_string("<script type=\"text/javascript\" src=\"https://connect.facebook.net/#{default_facebook_share_options.merge(options)[:locale]}/all.js\"></script>")
|
36
51
|
end
|
37
52
|
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
53
|
+
def facebook_init_script(options = {})
|
54
|
+
options = default_facebook_share_options.merge(options)
|
55
|
+
params = build_params options, true
|
56
|
+
html_safe_string("FB.init({appId:\"#{options[:app_id]}\"#{params}});")
|
57
|
+
end
|
58
|
+
|
59
|
+
def build_params(options, for_init = false)
|
60
|
+
script = ""
|
61
|
+
options.each do |key, value|
|
62
|
+
# if it's for init script, include only status, cookie and xfbml
|
63
|
+
# if it's for stream.publish, include all except for initial
|
64
|
+
param_check = ( for_init ) ? FacebookShare::INIT_PARAMS.include?(key.to_s) : !(FacebookShare::REMOVE_PARAMS.include?(key.to_s))
|
65
|
+
|
66
|
+
if value && param_check
|
67
|
+
value_sanitized = value.gsub(/"/, '\"')
|
68
|
+
script << ", #{key}: \"#{value_sanitized}\""
|
46
69
|
end
|
47
|
-
script
|
48
70
|
end
|
71
|
+
script
|
72
|
+
end
|
73
|
+
|
74
|
+
def html_safe_string(str)
|
75
|
+
@use_html_safe ||= "".respond_to?(:html_safe)
|
76
|
+
@use_html_safe ? str.html_safe : str
|
77
|
+
end
|
49
78
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: facebook_share
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 27
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 2
|
10
|
+
version: 0.0.2
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- "Mike Po\xC5\x82tyn"
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-02-
|
18
|
+
date: 2011-02-16 00:00:00 +01:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|