From 15acb3780c48b22663de125914fc5077dc68d6d0 Mon Sep 17 00:00:00 2001 From: Jaap Roes Date: Wed, 22 Apr 2009 14:48:28 +0200 Subject: [PATCH] Implemented basic buttons. --- lib/addthis.rb | 44 +++++++++++++++++++++ test/addthis_test.rb | 91 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 135 insertions(+) diff --git a/lib/addthis.rb b/lib/addthis.rb index 99d8b81..f37092a 100644 --- a/lib/addthis.rb +++ b/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{} + s << %Q{Bookmark and Share} + addthis_tag(s, options) + end + alias addthis_share_button addthis_bookmark_button + def addthis_feed_button(url, *args) + options = extract_options(args) + s = %Q{} + s << %Q{Subscribe} + addthis_tag(s, options) + end + + def addthis_email_button(*args) + url, options = extract_url_and_options(args) + s = %Q{} + s << %Q{Email} + addthis_tag(s, options) + end + + protected + def addthis_tag(str, options = {}) + s = [%Q{}] + s << %Q{} if false + s << str + s << %Q{} + s << %Q{} + 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 \ No newline at end of file diff --git a/test/addthis_test.rb b/test/addthis_test.rb index fa1ccdf..8296362 100644 --- a/test/addthis_test.rb +++ b/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 "", @output_lines.first + end + + should "end with an html comment" do + assert_equal "", @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 '