Skip to content

Commit

Permalink
Implemented basic buttons.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaap3 committed Apr 22, 2009
1 parent bc3e60b commit 15acb37
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 0 deletions.
44 changes: 44 additions & 0 deletions lib/addthis.rb
@@ -1,5 +1,49 @@
module Jaap3
module AddThisHelper
module ClassMethods
def addthis_bookmark_button(*args)
url, options = extract_url_and_options(args)
s = %Q{<a href="http://www.addthis.com/bookmark.php?v=20" onmouseover="return addthis_open(this, '', '#{url}', '#{options[:title]}')" onmouseout="addthis_close()" onclick="return addthis_sendto()">}
s << %Q{<img src="http://s7.addthis.com/static/btn/lg-share-en.gif" width="125" height="16" alt="Bookmark and Share" style="border:0"/></a>}
addthis_tag(s, options)
end
alias addthis_share_button addthis_bookmark_button

def addthis_feed_button(url, *args)
options = extract_options(args)
s = %Q{<a href="http://www.addthis.com/feed.php?pub=&h1=#{url}&t1=" onclick="return addthis_open(this, 'feed', '#{url}')" alt="Subscribe using any feed reader!" target="_blank">}
s << %Q{<img src="http://s7.addthis.com/static/btn/lg-feed-en.gif" width="125" height="16" alt="Subscribe" style="border:0"/></a>}
addthis_tag(s, options)
end

def addthis_email_button(*args)
url, options = extract_url_and_options(args)
s = %Q{<a href="http://www.addthis.com/bookmark.php" style="text-decoration:none;" onclick="return addthis_open(this, 'email', '#{url}', '#{options[:title]}');" >}
s << %Q{<img src="http://s7.addthis.com/button1-email.gif" width="54" height="16" border="0" alt="Email" /></a>}
addthis_tag(s, options)
end

protected
def addthis_tag(str, options = {})
s = [%Q{<!-- AddThis Button BEGIN -->}]
s << %Q{<script type="text/javascript">var addthis_pub="";</script>} if false
s << str
s << %Q{<script type="text/javascript" src="http://s7.addthis.com/js/200/addthis_widget.js"></script>}
s << %Q{<!-- AddThis Button END -->}
s * "\n"
end

def extract_url_and_options(args, options = {:title => "[TITLE]"})
url = args[0].is_a?(String) ? args.shift : "[URL]"
return url, options = extract_options(args, options)
end

def extract_options(args, options = {})
title = args[0].is_a?(String) ? args.shift : options[:title]
options = args[0].is_a?(Hash) ? args.shift : options
options[:title] = title
return options
end
end
end
end
91 changes: 91 additions & 0 deletions test/addthis_test.rb
@@ -1,5 +1,96 @@
require 'test_helper'
include Jaap3::AddThisHelper::ClassMethods

class AddthisTest < Test::Unit::TestCase

should "be configurable"

should "provide addthis_bookmark_button" do
assert respond_to?(:addthis_bookmark_button)
end

should "alias addthis_bookmark_button as addthis_share_button" do
assert respond_to?(:addthis_share_button)
end

should "provide addthis_feed_button" do
assert respond_to?(:addthis_feed_button)
end

should "provide addthis_email_button" do
assert respond_to?(:addthis_email_button)
end

[:addthis_bookmark_button, :addthis_feed_button, :addthis_email_button].each do |m|
context "the output of #{m}" do
setup do
@output_lines = method(m).call("http://example.com").split("\n")
end

should "start with an html comment" do
assert_equal "<!-- AddThis Button BEGIN -->", @output_lines.first
end

should "end with an html comment" do
assert_equal "<!-- AddThis Button END -->", @output_lines.last
end
end
end

context "a bookmark/share tag created without params" do
setup do
@output = addthis_bookmark_button
end

should "link to http://www.addthis.com/bookmark.php" do
assert_match '<a href="http://www.addthis.com/bookmark.php', @output
end

should "set url to [URL]" do
assert_match "'[URL]'", @output
end

should "set title to [TITLE]" do
assert_match "'[TITLE]'", @output
end
end

context "a feed tag" do
setup do
@output = addthis_feed_button("http://example.com")
end

should "link to http://www.addthis.com/feed.php" do
assert_match '<a href="http://www.addthis.com/feed.php', @output
end

should "set h1 param to example.com" do
assert_match "&h1=http://example.com", @output
end

should "set url to example.com" do
assert_match "'http://example.com')", @output
end
end

context "without configuration" do
context "a bookmark / share tag" do
end

context "a feed tag" do
end
end

context "with configuration" do
context "a bookmark / share tag" do
should "output bookmark tag with tracking"
end

context "a feed tag" do
should "output feed tag with tracking"
end
end

should "use secure links"

end

0 comments on commit 15acb37

Please sign in to comment.