Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
miloops committed Aug 8, 2008
0 parents commit 06bd91f
Show file tree
Hide file tree
Showing 14 changed files with 244 additions and 0 deletions.
20 changes: 20 additions & 0 deletions MIT-LICENSE
@@ -0,0 +1,20 @@
Copyright (c) 2008 Emilio Tagua

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
38 changes: 38 additions & 0 deletions README
@@ -0,0 +1,38 @@
ShareThis
=========

Simple plugin to share pages with most popular social sites.

At the moment, the following APIs are supported:
- Facebook
- Digg
- MySpace
- StumbleUpon
- del.icio.us

Example
=======

The following will render in the view the links to share the current page with the defaults settings of ShareThis.

<%= share_this %>


self-explanatory example:
<%= share_this(:include => [:facebook, :digg], :discard_image => true,
:url => article_path(article),
:title => article.title) %>


Discarding the images is also possible:
<%= share_this(:discard_image => true) %>

TODO
====
- Improve documentation
- Remove document.title and javascript when getting the page title with Rails becomes possible.
- Add more APIs.

Feedback? Comments? Bugs? Drop me a line to miloops (at) gmail (dot) com

Copyright (c) 2008 Emilio Tagua, released under the MIT license
22 changes: 22 additions & 0 deletions Rakefile
@@ -0,0 +1,22 @@
require 'rake'
require 'rake/testtask'
require 'rake/rdoctask'

desc 'Default: run unit tests.'
task :default => :test

desc 'Test the share_this plugin.'
Rake::TestTask.new(:test) do |t|
t.libs << 'lib'
t.pattern = 'test/**/*_test.rb'
t.verbose = true
end

desc 'Generate documentation for the share_this plugin.'
Rake::RDocTask.new(:rdoc) do |rdoc|
rdoc.rdoc_dir = 'rdoc'
rdoc.title = 'ShareThis'
rdoc.options << '--line-numbers' << '--inline-source'
rdoc.rdoc_files.include('README')
rdoc.rdoc_files.include('lib/**/*.rb')
end
2 changes: 2 additions & 0 deletions init.rb
@@ -0,0 +1,2 @@
require 'share_this'
ActionView::Base.send :include, ShareThis
1 change: 1 addition & 0 deletions install.rb
@@ -0,0 +1 @@
# Install hook code here
10 changes: 10 additions & 0 deletions lib/apis/delicious.rb
@@ -0,0 +1,10 @@
module ShareThis
module Deliciou
NAME = "del.icio.us"
URL_PARAM = "url"
TITLE_PARAM = "title"
BASE_URL = "http://del.icio.us/post?"
STYLE = "icn_share_delicious"
EXTRA_PARAM = ["&v=4", "&jump=close", "&noui=yes"]
end
end
10 changes: 10 additions & 0 deletions lib/apis/digg.rb
@@ -0,0 +1,10 @@
module ShareThis
module Digg
NAME = "Digg"
URL_PARAM = "url"
TITLE_PARAM = "title"
BASE_URL = "http://digg.com/submit?"
STYLE = "icn_share_digg"
EXTRA_PARAM = ["&phase=2"]
end
end
10 changes: 10 additions & 0 deletions lib/apis/facebook.rb
@@ -0,0 +1,10 @@
module ShareThis
module Facebook
NAME = "Facebook"
URL_PARAM = "u"
TITLE_PARAM = "t"
BASE_URL = "http://www.facebook.com/sharer.php?"
STYLE = "icn_share_facebook"
EXTRA_PARAM = []
end
end
10 changes: 10 additions & 0 deletions lib/apis/my_space.rb
@@ -0,0 +1,10 @@
module ShareThis
module MySpace
NAME = "MySpace"
URL_PARAM = "u"
TITLE_PARAM = "t"
BASE_URL = "http://www.myspace.com/Modules/PostTo/Pages/?"
STYLE = "icn_share_myspace"
EXTRA_PARAM = ["&l=1"]
end
end
10 changes: 10 additions & 0 deletions lib/apis/stumble_upon.rb
@@ -0,0 +1,10 @@
module ShareThis
module StumbleUpon
NAME = "StumbleUpon"
URL_PARAM = "url"
TITLE_PARAM = "title"
BASE_URL = "http://www.stumbleupon.com/submit?"
STYLE = "icn_share_stumbleupon"
EXTRA_PARAM = []
end
end
51 changes: 51 additions & 0 deletions lib/share_this.rb
@@ -0,0 +1,51 @@
require 'apis/facebook'
require 'apis/digg'
require 'apis/stumble_upon'
require 'apis/my_space'
require 'apis/delicious'

module ShareThis
def share_this(options = {})
apis = [*options.delete(:include)] if options[:include]
apis ||= [:facebook, :digg, :stumble_upon, :delicious, :my_space]

apis.inject('') do |t, v|
t << construct_api(v, options) + (options[:separator] || "&nbsp;")
end
end

protected
def share_this_url(url = nil)
url ? url : "#{request.protocol}#{request.host_with_port}#{request.request_uri}"
end

def share_this_title(title = nil)
title ? "encodeURIComponent('#{title}')" : "encodeURIComponent(document.title)"
end

def construct_api(api, options = {})
api = api.to_s.classify
if ShareThis.constants.include?(api)
api = "ShareThis::#{api}".constantize
else
raise NameError, "Invalid API name: #{api}"
end

url = ''
url << api::BASE_URL
url << "#{api::URL_PARAM}=#{share_this_url(options[:url])}"
url << "&#{api::TITLE_PARAM}=' + #{share_this_title(options[:title])} + '"
url << api::EXTRA_PARAM.join unless api::EXTRA_PARAM.empty?


# Don't show the API name if discard name is given as an option.
name = options[:discard_name] ? '' : content_tag(:span, api::NAME)

# Don't show the API image if discard image is given as an option.
image = options[:discard_image] ? '' : image_tag("share_this_pixel.gif",
:class => api::STYLE, :alt => '', :width => 16, :height => 16)
image << "&nbsp;" unless image.blank? || options[:discard_name]

"<script type=\"#{Mime::JS}\">document.write('#{link_to(image + name, url, :target => :blank)}')</script>"
end
end
4 changes: 4 additions & 0 deletions tasks/share_this_tasks.rake
@@ -0,0 +1,4 @@
# desc "Explaining what the task does"
# task :share_this do
# # Task goes here
# end
55 changes: 55 additions & 0 deletions test/share_this_test.rb
@@ -0,0 +1,55 @@
require 'test/unit'
require File.expand_path(File.join(File.dirname(__FILE__), '../../../../config/environment.rb'))
ENV['RAILS_ASSET_ID'] = ''

module ShareThis
class RequestMock
class << self
def protocol; "http://"; end
def host_with_port; "www.example.com"; end
def request_uri; "/articles/1"; end
end
end

def request; RequestMock; end
end

class ShareThisTest < Test::Unit::TestCase
include ShareThis
include ActionView::Helpers::AssetTagHelper

def test_should_share_this
assert_equal %Q(<script type="text/javascript">document.write('<a href="http://www.facebook.com/sharer.php?u=http://www.example.com/articles/1&t=' + encodeURIComponent(document.title) + '" target="blank"><img alt="" class="icn_share_facebook" height="16" src="/images/share_this_pixel.gif" width="16" />&nbsp;<span>Facebook</span></a>')</script>&nbsp;<script type="text/javascript">document.write('<a href="http://digg.com/submit?url=http://www.example.com/articles/1&title=' + encodeURIComponent(document.title) + '&phase=2" target="blank"><img alt="" class="icn_share_digg" height="16" src="/images/share_this_pixel.gif" width="16" />&nbsp;<span>Digg</span></a>')</script>&nbsp;<script type="text/javascript">document.write('<a href="http://www.stumbleupon.com/submit?url=http://www.example.com/articles/1&title=' + encodeURIComponent(document.title) + '" target="blank"><img alt="" class="icn_share_stumbleupon" height="16" src="/images/share_this_pixel.gif" width="16" />&nbsp;<span>StumbleUpon</span></a>')</script>&nbsp;<script type="text/javascript">document.write('<a href="http://del.icio.us/post?url=http://www.example.com/articles/1&title=' + encodeURIComponent(document.title) + '&v=4&jump=close&noui=yes" target="blank"><img alt="" class="icn_share_delicious" height="16" src="/images/share_this_pixel.gif" width="16" />&nbsp;<span>del.icio.us</span></a>')</script>&nbsp;<script type="text/javascript">document.write('<a href="http://www.myspace.com/Modules/PostTo/Pages/?u=http://www.example.com/articles/1&t=' + encodeURIComponent(document.title) + '&l=1" target="blank"><img alt="" class="icn_share_myspace" height="16" src="/images/share_this_pixel.gif" width="16" />&nbsp;<span>MySpace</span></a>')</script>&nbsp;),
share_this
end

def test_should_share_this_without_name
assert_equal %Q(<script type="text/javascript">document.write('<a href="http://www.facebook.com/sharer.php?u=http://www.example.com/articles/1&t=' + encodeURIComponent(document.title) + '" target="blank"><img alt="" class="icn_share_facebook" height="16" src="/images/share_this_pixel.gif" width="16" /></a>')</script>&nbsp;<script type="text/javascript">document.write('<a href="http://digg.com/submit?url=http://www.example.com/articles/1&title=' + encodeURIComponent(document.title) + '&phase=2" target="blank"><img alt="" class="icn_share_digg" height="16" src="/images/share_this_pixel.gif" width="16" /></a>')</script>&nbsp;<script type="text/javascript">document.write('<a href="http://www.stumbleupon.com/submit?url=http://www.example.com/articles/1&title=' + encodeURIComponent(document.title) + '" target="blank"><img alt="" class="icn_share_stumbleupon" height="16" src="/images/share_this_pixel.gif" width="16" /></a>')</script>&nbsp;<script type="text/javascript">document.write('<a href="http://del.icio.us/post?url=http://www.example.com/articles/1&title=' + encodeURIComponent(document.title) + '&v=4&jump=close&noui=yes" target="blank"><img alt="" class="icn_share_delicious" height="16" src="/images/share_this_pixel.gif" width="16" /></a>')</script>&nbsp;<script type="text/javascript">document.write('<a href="http://www.myspace.com/Modules/PostTo/Pages/?u=http://www.example.com/articles/1&t=' + encodeURIComponent(document.title) + '&l=1" target="blank"><img alt="" class="icn_share_myspace" height="16" src="/images/share_this_pixel.gif" width="16" /></a>')</script>&nbsp;),
share_this(:discard_name => true)
end

def test_should_share_this_without_image
assert_equal %Q(<script type="text/javascript">document.write('<a href="http://www.facebook.com/sharer.php?u=http://www.example.com/articles/1&t=' + encodeURIComponent(document.title) + '" target="blank"><span>Facebook</span></a>')</script>&nbsp;<script type="text/javascript">document.write('<a href="http://digg.com/submit?url=http://www.example.com/articles/1&title=' + encodeURIComponent(document.title) + '&phase=2" target="blank"><span>Digg</span></a>')</script>&nbsp;<script type="text/javascript">document.write('<a href="http://www.stumbleupon.com/submit?url=http://www.example.com/articles/1&title=' + encodeURIComponent(document.title) + '" target="blank"><span>StumbleUpon</span></a>')</script>&nbsp;<script type="text/javascript">document.write('<a href="http://del.icio.us/post?url=http://www.example.com/articles/1&title=' + encodeURIComponent(document.title) + '&v=4&jump=close&noui=yes" target="blank"><span>del.icio.us</span></a>')</script>&nbsp;<script type="text/javascript">document.write('<a href="http://www.myspace.com/Modules/PostTo/Pages/?u=http://www.example.com/articles/1&t=' + encodeURIComponent(document.title) + '&l=1" target="blank"><span>MySpace</span></a>')</script>&nbsp;),
share_this(:discard_image => true)
end

def test_should_share_this_for_one_api
assert_equal %Q(<script type="text/javascript">document.write('<a href="http://www.facebook.com/sharer.php?u=http://www.example.com/articles/1&t=' + encodeURIComponent(document.title) + '" target="blank"><img alt="" class="icn_share_facebook" height="16" src="/images/share_this_pixel.gif" width="16" />&nbsp;<span>Facebook</span></a>')</script>&nbsp;),
share_this(:include => :facebook)
end

def test_should_share_this_for_one_api_without_image
assert_equal %Q(<script type="text/javascript">document.write('<a href="http://www.facebook.com/sharer.php?u=http://www.example.com/articles/1&t=' + encodeURIComponent(document.title) + '" target="blank"><span>Facebook</span></a>')</script>&nbsp;),
share_this(:include => :facebook, :discard_image => true)
end

def test_should_share_this_for_one_api_with_custom_title
assert_equal %Q(<script type="text/javascript">document.write('<a href="http://www.facebook.com/sharer.php?u=http://www.example.com/articles/1&t=' + encodeURIComponent('A random custom title') + '" target="blank"><img alt="" class="icn_share_facebook" height="16" src="/images/share_this_pixel.gif" width="16" />&nbsp;<span>Facebook</span></a>')</script>&nbsp;),
share_this(:include => :facebook, :title => "A random custom title")
end

def test_should_share_this_for_one_api_with_custom_url
assert_equal %Q(<script type="text/javascript">document.write('<a href="http://www.facebook.com/sharer.php?u=http://www.exameple.com/articles/&t=' + encodeURIComponent(document.title) + '" target="blank"><img alt="" class="icn_share_facebook" height="16" src="/images/share_this_pixel.gif" width="16" />&nbsp;<span>Facebook</span></a>')</script>&nbsp;),
share_this(:include => :facebook, :url => "http://www.exameple.com/articles/")
end
end
1 change: 1 addition & 0 deletions uninstall.rb
@@ -0,0 +1 @@
# Uninstall hook code here

0 comments on commit 06bd91f

Please sign in to comment.