jaap3-addthis 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Jaap Roes
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,91 @@
1
+ = Addthis
2
+
3
+ Addthis.com provides widgets that make sharing, bookmarking and emailing pages,
4
+ and subscribing to feeds easy for your visitors. It supports a broad range of
5
+ social network sites and web applications.
6
+
7
+ This plugin makes it easy to add plain or customized Addthis.com widgets to
8
+ your Rails application.
9
+
10
+ = Installation
11
+
12
+ As a Gem in environment.rb:
13
+
14
+ config.gem 'jaap3-addthis', :lib => 'addthis',
15
+ :source => 'http://gems.github.com'
16
+
17
+ As a plugin:
18
+
19
+ script/plugin install git://github.com/jaap3/addthis.git
20
+
21
+ = Quick Start
22
+
23
+ The most basic use is simply providing no arguments:
24
+
25
+ addthis_bookmark_button
26
+ addthis_email_button
27
+
28
+ The code generated by Addthis will then try to figure out the url and title by
29
+ itself. This is not possible for addthis_feed_button.
30
+
31
+ You can set a custom url, this is required for addthis_feed_button:
32
+
33
+ addthis_bookmark_button("http://www.example.com/")
34
+ addthis_email_button("http://www.example.com/")
35
+ addthis_feed_button("http://www.example.com/")
36
+
37
+ Setting a title is possible when adding a bookmark or email button:
38
+
39
+ addthis_bookmark_button("http://www.example.com/", "Example website")
40
+ addthis_email_button("http://www.example.com/", "Example website")
41
+
42
+ To only set a custom title you can use to options hash:
43
+
44
+ addthis_bookmark_button(:title => "Example title")
45
+ addthis_email_button(:title => "Example title")
46
+
47
+ The options hash can be used to customize the widget to a great extend. Each of
48
+ the shown examples can take the options hash as an optional extra argument.
49
+
50
+ = Usage
51
+
52
+ If you wish to track analytics for your button, you must create an account at
53
+ http://addthis.com/. Joining is free!
54
+
55
+ After signing up create a new ruby file in config/initializers called
56
+ addthis.rb. You can use this file to configure your publisher name (your
57
+ Addthis.com username) like so:
58
+
59
+ Jaap3::Addthis::CONFIG[:publisher] = "YOUR_USERNAME_HERE"
60
+
61
+ You can then use this initializer to further override any other settings you
62
+ might like.
63
+
64
+ The default settings are stored in the following constants. You can look in
65
+ the source to see what they are.
66
+
67
+ Jaap3::Addthis::CONFIG
68
+ Jaap3::Addthis::DEFAULT_OPTIONS
69
+ Jaap3::Addthis::BOOKMARK_BUTTON_DEFAULTS
70
+ Jaap3::Addthis::FEED_BUTTON_DEFAULTS
71
+ Jaap3::Addthis::EMAIL_BUTTON_DEFAULTS
72
+
73
+ Besides overriding the default settings using the initializer, each helper
74
+ method takes an options hash that can be used to override any setting for
75
+ a specific button.
76
+
77
+ So calling
78
+
79
+ addthis_bookmark_button(:publisher => "example", :secure => true)
80
+
81
+ Will output a button usable on https pages with the publisher variable set
82
+ to "example" regardless of any configuration changes.
83
+
84
+ = Contributing
85
+
86
+ If you would like to contribute to the Addthis plugin, just fork the code
87
+ and send me a pull request after you are done fiddling.
88
+
89
+ == Copyright
90
+
91
+ Copyright (c) 2009 Jaap Roes. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,56 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "addthis"
8
+ gem.summary = %Q{TODO}
9
+ gem.email = "jaap@u-e-h.net"
10
+ gem.homepage = "http://github.com/jaap3/addthis"
11
+ gem.authors = ["Jaap Roes"]
12
+
13
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
14
+ end
15
+ rescue LoadError
16
+ puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
17
+ end
18
+
19
+ require 'rake/testtask'
20
+ Rake::TestTask.new(:test) do |test|
21
+ test.libs << 'lib' << 'test'
22
+ test.pattern = 'test/**/*_test.rb'
23
+ test.verbose = true
24
+ end
25
+
26
+ begin
27
+ require 'rcov/rcovtask'
28
+ Rcov::RcovTask.new do |test|
29
+ test.libs << 'test'
30
+ test.pattern = 'test/**/*_test.rb'
31
+ test.verbose = true
32
+ end
33
+ rescue LoadError
34
+ task :rcov do
35
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
36
+ end
37
+ end
38
+
39
+
40
+ task :default => :test
41
+
42
+ require 'rake/rdoctask'
43
+ Rake::RDocTask.new do |rdoc|
44
+ if File.exist?('VERSION.yml')
45
+ config = YAML.load(File.read('VERSION.yml'))
46
+ version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
47
+ else
48
+ version = ""
49
+ end
50
+
51
+ rdoc.rdoc_dir = 'rdoc'
52
+ rdoc.title = "addthis #{version}"
53
+ rdoc.rdoc_files.include('README*')
54
+ rdoc.rdoc_files.include('lib/**/*.rb')
55
+ end
56
+
data/VERSION.yml ADDED
@@ -0,0 +1,4 @@
1
+ ---
2
+ :major: 0
3
+ :minor: 2
4
+ :patch: 0
data/lib/addthis.rb ADDED
@@ -0,0 +1,83 @@
1
+ module Jaap3
2
+ module Addthis
3
+ CONFIG = {
4
+ :publisher => ""
5
+ }
6
+ DEFAULT_OPTIONS = {
7
+ :script_src => "http://s7.addthis.com/js/200/addthis_widget.js",
8
+ :secure => false
9
+ }
10
+ BOOKMARK_BUTTON_DEFAULTS = {
11
+ :title => "",
12
+ :alt => "Bookmark and Share"
13
+ }
14
+ FEED_BUTTON_DEFAULTS = {
15
+ :title => "Subscribe using any feed reader!",
16
+ :alt => "Subscribe"
17
+ }
18
+ EMAIL_BUTTON_DEFAULTS = {
19
+ :title => "",
20
+ :alt => "Email"
21
+ }
22
+
23
+ module Helper
24
+ def addthis_bookmark_button(*args)
25
+ url, options = extract_addthis_url_and_options(args)
26
+ options = BOOKMARK_BUTTON_DEFAULTS.merge(options)
27
+ s = %Q{<a href="http://www.addthis.com/bookmark.php?v=20" onmouseover="}
28
+ s << %Q{return addthis_open(this, '', '#{url}', '#{options[:page_title]}')"}
29
+ s << %Q{ title="#{options[:title]}"}
30
+ s << %Q{ onmouseout="addthis_close()" onclick="return addthis_sendto()">}
31
+ s << %Q{<img src="http://s7.addthis.com/static/btn/lg-share-en.gif"}
32
+ s << %Q{ width="125" height="16" alt="#{options[:alt]}" style="border:0"/></a>}
33
+ addthis_tag(s, options)
34
+ end
35
+ alias addthis_share_button addthis_bookmark_button
36
+
37
+ def addthis_feed_button(url, *args)
38
+ options = FEED_BUTTON_DEFAULTS.merge(extract_addthis_options(args))
39
+ s = %Q{<a href="http://www.addthis.com/feed.php?pub=#{options[:publisher]}&h1=#{url}&t1="}
40
+ s << %Q{ onclick="return addthis_open(this, 'feed', '#{url}')"}
41
+ s << %Q{ title="#{options[:title]}" target="_blank">}
42
+ s << %Q{<img src="http://s7.addthis.com/static/btn/lg-feed-en.gif"}
43
+ s << %Q{width="125" height="16" alt="#{options[:alt]}" style="border:0"/></a>}
44
+ addthis_tag(s, options)
45
+ end
46
+
47
+ def addthis_email_button(*args)
48
+ url, options = extract_addthis_url_and_options(args)
49
+ options = EMAIL_BUTTON_DEFAULTS.merge(options)
50
+ s = %Q{<a href="http://www.addthis.com/bookmark.php"}
51
+ s << %Q{ style="text-decoration:none;" title="#{options[:title]}"}
52
+ s << %Q{ onclick="return addthis_open(this, 'email', '#{url}', '#{options[:page_title]}');">}
53
+ s << %Q{<img src="http://s7.addthis.com/button1-email.gif" width="54" height="16" border="0" alt="#{options[:alt]}" /></a>}
54
+ addthis_tag(s, options)
55
+ end
56
+
57
+ protected
58
+ def addthis_tag(str, options = {})
59
+ s = [%Q{<!-- AddThis Button BEGIN -->}]
60
+ s << %Q{<script type="text/javascript">var addthis_pub="#{options[:publisher]}";</script>}
61
+ s << str
62
+ s << %Q{<script type="text/javascript" src="#{options[:script_src]}"></script>}
63
+ s << %Q{<!-- AddThis Button END -->}
64
+ s = s * "\n"
65
+ options[:secure] ? s.gsub(/http:\/\/s[57]\.addthis\.com/, "https://secure.addthis.com") : s
66
+ end
67
+
68
+ def extract_addthis_url_and_options(args, options = {:page_title => "[TITLE]"})
69
+ url = args[0].is_a?(String) ? args.shift : "[URL]"
70
+ return url, options = extract_addthis_options(args, options)
71
+ end
72
+
73
+ def extract_addthis_options(args, options = {})
74
+ page_title = args[0].is_a?(String) ? args.shift : options[:page_title]
75
+ options = args[0].is_a?(Hash) ? args.shift : options
76
+ options[:page_title] = page_title
77
+ options = CONFIG.merge(DEFAULT_OPTIONS).merge(options)
78
+ options.symbolize_keys! if options.respond_to?(:symbolize_keys!)
79
+ return options
80
+ end
81
+ end
82
+ end
83
+ end
data/rails/init.rb ADDED
@@ -0,0 +1,3 @@
1
+ require 'addthis'
2
+
3
+ ActionView::Base.send :include, Jaap3::AddthisHelper::ClassMethods
@@ -0,0 +1,150 @@
1
+ require 'test_helper'
2
+ include Jaap3::Addthis::Helper
3
+
4
+ class AddthisTest < Test::Unit::TestCase
5
+
6
+ should "provide addthis_bookmark_button" do
7
+ assert respond_to?(:addthis_bookmark_button)
8
+ end
9
+
10
+ should "alias addthis_bookmark_button as addthis_share_button" do
11
+ assert respond_to?(:addthis_share_button)
12
+ end
13
+
14
+ should "provide addthis_feed_button" do
15
+ assert respond_to?(:addthis_feed_button)
16
+ end
17
+
18
+ should "provide addthis_email_button" do
19
+ assert respond_to?(:addthis_email_button)
20
+ end
21
+
22
+ [:addthis_bookmark_button, :addthis_feed_button, :addthis_email_button].each do |m|
23
+ context "the output of #{m}" do
24
+ setup do
25
+ @output = method(m).call("http://example.com")
26
+ @output_lines = @output.split("\n")
27
+ end
28
+
29
+ should_set_script_src_to Jaap3::Addthis::DEFAULT_OPTIONS[:script_src]
30
+
31
+ should "start with an html comment" do
32
+ assert_equal "<!-- AddThis Button BEGIN -->", @output_lines.first
33
+ end
34
+
35
+ should "end with an html comment" do
36
+ assert_equal "<!-- AddThis Button END -->", @output_lines.last
37
+ end
38
+ end
39
+ end
40
+
41
+ context "a bookmark/share button" do
42
+ setup { @output = addthis_bookmark_button }
43
+
44
+ should_set_alt_to Jaap3::Addthis::BOOKMARK_BUTTON_DEFAULTS[:alt]
45
+ should_set_title_to Jaap3::Addthis::BOOKMARK_BUTTON_DEFAULTS[:title]
46
+ should_set_href_to "http://www.addthis.com/bookmark.php?v=20"
47
+
48
+ should "set url to [URL]" do
49
+ assert_match "'[URL]'", @output
50
+ end
51
+
52
+ should "set title to [TITLE]" do
53
+ assert_match "'[TITLE]'", @output
54
+ end
55
+ end
56
+
57
+ context "a feed button" do
58
+ setup { @output = addthis_feed_button("http://example.com") }
59
+
60
+ should_set_alt_to Jaap3::Addthis::FEED_BUTTON_DEFAULTS[:alt]
61
+ should_set_title_to Jaap3::Addthis::FEED_BUTTON_DEFAULTS[:title]
62
+ should_set_href_to "http://www.addthis.com/feed.php?pub=&h1=http://example.com&t1="
63
+
64
+ should "set url to example.com" do
65
+ assert_match "'http://example.com')", @output
66
+ end
67
+ end
68
+
69
+ context "an email button" do
70
+ setup { @output = addthis_email_button }
71
+
72
+ should_set_alt_to Jaap3::Addthis::EMAIL_BUTTON_DEFAULTS[:alt]
73
+ should_set_title_to Jaap3::Addthis::EMAIL_BUTTON_DEFAULTS[:title]
74
+ should_set_href_to "http://www.addthis.com/bookmark.php"
75
+ end
76
+
77
+ context "with publisher configured" do
78
+ setup { Jaap3::Addthis::CONFIG[:publisher] = "test_publisher" }
79
+
80
+ [:addthis_bookmark_button, :addthis_feed_button, :addthis_email_button].each do |m|
81
+ context "the output of #{m}" do
82
+ setup { @output = method(m).call("http://example.com") }
83
+
84
+ should "set addthis_pub to test_publisher" do
85
+ assert_match 'var addthis_pub="test_publisher";', @output
86
+ end
87
+ end
88
+ end
89
+
90
+ context "a feed button" do
91
+ setup { @output = addthis_feed_button("http://example.com") }
92
+
93
+ should_set_href_to "http://www.addthis.com/feed.php?pub=test_publisher&h1=http://example.com&t1="
94
+ end
95
+ end
96
+
97
+ context "with altered script_src" do
98
+ setup { Jaap3::Addthis::DEFAULT_OPTIONS[:script_src] = "http://example.com/example.js" }
99
+
100
+ [:addthis_bookmark_button, :addthis_feed_button, :addthis_email_button].each do |m|
101
+ context "the output of #{m}" do
102
+ setup do
103
+ @output = method(m).call("http://example.com")
104
+ @output_lines = @output.split("\n")
105
+ end
106
+
107
+ should_set_script_src_to "http://example.com/example.js"
108
+ end
109
+ end
110
+
111
+ context "in turn overwritten by options hash" do
112
+ [:addthis_bookmark_button, :addthis_feed_button, :addthis_email_button].each do |m|
113
+ context "the output of #{m}" do
114
+ setup do
115
+ @output = method(m).call("http://example.com", :script_src => "http://www.example.com/example.js")
116
+ @output_lines = @output.split("\n")
117
+ end
118
+
119
+ should_set_script_src_to "http://www.example.com/example.js"
120
+ end
121
+ end
122
+ end
123
+ end
124
+
125
+ context "when setting secure to true" do
126
+ [:addthis_bookmark_button, :addthis_feed_button, :addthis_email_button].each do |m|
127
+ context "by using the options hash the output of #{m}" do
128
+ setup do
129
+ @output = method(m).call("http://example.com", :secure => true)
130
+ @output_lines = @output.split("\n")
131
+ end
132
+
133
+ should_set_script_src_to "https://secure.addthis.com/js/200/addthis_widget.js"
134
+ end
135
+ end
136
+
137
+ [:addthis_bookmark_button, :addthis_feed_button, :addthis_email_button].each do |m|
138
+ context "by altering the defaults the output of #{m}" do
139
+ setup do
140
+ Jaap3::Addthis::DEFAULT_OPTIONS[:secure] = true
141
+ @output = method(m).call("http://example.com")
142
+ @output_lines = @output.split("\n")
143
+ end
144
+
145
+ should_set_script_src_to "https://secure.addthis.com/js/200/addthis_widget.js"
146
+ end
147
+ end
148
+ end
149
+
150
+ end
@@ -0,0 +1,31 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ require 'addthis'
8
+
9
+ class Test::Unit::TestCase
10
+
11
+ class << self
12
+ %w(alt href src title).each do |attribute|
13
+ define_method(:"should_set_#{attribute}_to") do |expected|
14
+ should "set #{attribute} to '#{expected}'" do
15
+ assert_equal %Q{#{attribute}="#{expected}"}, @output[/#{attribute}="[^"]*"/]
16
+ end
17
+ end
18
+ end
19
+
20
+ def should_set_script_src_to(expected)
21
+ context "" do
22
+ setup do
23
+ @output = @output[/<script.+src=[^>]+>/]
24
+ end
25
+
26
+ should_set_src_to expected
27
+ end
28
+ end
29
+ end
30
+
31
+ end
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jaap3-addthis
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Jaap Roes
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-04-23 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: jaap@u-e-h.net
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - LICENSE
24
+ - README.rdoc
25
+ files:
26
+ - LICENSE
27
+ - README.rdoc
28
+ - Rakefile
29
+ - VERSION.yml
30
+ - lib/addthis.rb
31
+ - rails/init.rb
32
+ - test/addthis_test.rb
33
+ - test/test_helper.rb
34
+ has_rdoc: true
35
+ homepage: http://github.com/jaap3/addthis
36
+ post_install_message:
37
+ rdoc_options:
38
+ - --charset=UTF-8
39
+ require_paths:
40
+ - lib
41
+ required_ruby_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: "0"
46
+ version:
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: "0"
52
+ version:
53
+ requirements: []
54
+
55
+ rubyforge_project:
56
+ rubygems_version: 1.2.0
57
+ signing_key:
58
+ specification_version: 2
59
+ summary: TODO
60
+ test_files:
61
+ - test/test_helper.rb
62
+ - test/addthis_test.rb