|
# |
|
# A set of functions for generating HTML anchors for static pages. |
|
# Twitter, Google+, and Facebook are supported. |
|
# |
|
# To create your own links, pass your params hash to the functions, |
|
# as in the "before" block of the test section below. |
|
# |
|
# Used as reference: |
|
# http://blog.hubspot.com/blog/tabid/6307/bid/29544/The-Ultimate-Cheat-Sheet-for-Creating-Social-Media-Buttons.aspx |
|
# |
|
|
|
require 'uri' |
|
|
|
BASE_URLS = { |
|
twitter: "https://twitter.com/intent/tweet", |
|
google: "https://plusone.google.com/_/+1/confirm?hl=en&url=http%3A%2F%2F%20", |
|
facebook: "http://www.facebook.com/share.php?u=" |
|
} |
|
|
|
def encoded_query_string(params) |
|
params.map do |param, value| |
|
[param, URI.encode(value)].join('=') |
|
end.join('&') |
|
end |
|
|
|
def twitter_sharing_link(params) |
|
link = [BASE_URLS[:twitter], "?", encoded_query_string(params)].join |
|
"<a href='#{link}'>Share this on Twitter</a>" |
|
end |
|
|
|
def google_sharing_link(params) |
|
link = [BASE_URLS[:google], params[:url]].join |
|
"<a href='#{link}'>Share this on Google+</a>" |
|
end |
|
|
|
def facebook_sharing_link(params) |
|
link = [BASE_URLS[:facebook], params[:url]].join |
|
"<a href='#{link}'>Share this on Facebook</a>" |
|
end |
|
|
|
require 'minitest/spec' |
|
require 'minitest/autorun' |
|
|
|
describe "tests for generating inline links on social media platforms for Nesta articles" do |
|
before do |
|
@params = { |
|
text: ["\u201D", "About Social Sharing", "\u201C", ":"].join, |
|
url: "http://jamesabbottdd.com/about-social-sharing", |
|
via: "abbotjam" |
|
} |
|
end |
|
|
|
it "turns a Hash of query params into a properly encoded URL string" do |
|
str = encoded_query_string(@params) |
|
str.must_equal "text=%E2%80%9DAbout%20Social%20Sharing%E2%80%9C:&url=http://jamesabbottdd.com/about-social-sharing&via=abbotjam" |
|
end |
|
|
|
it "creates a Twitter sharing link" do |
|
str = twitter_sharing_link(@params) |
|
str.must_equal "<a href='https://twitter.com/intent/tweet?text=%E2%80%9DAbout%20Social%20Sharing%E2%80%9C:&url=http://jamesabbottdd.com/about-social-sharing&via=abbotjam'>Share this on Twitter</a>" |
|
end |
|
|
|
it "creates a Google+ sharing link" do |
|
str = google_sharing_link(@params) |
|
str.must_equal "<a href='https://plusone.google.com/_/+1/confirm?hl=en&url=http%3A%2F%2F%20http://jamesabbottdd.com/about-social-sharing'>Share this on Google+</a>" |
|
end |
|
|
|
it "creates a Facebook sharing link" do |
|
str = facebook_sharing_link(@params) |
|
str.must_equal "<a href='http://www.facebook.com/share.php?u=http://jamesabbottdd.com/about-social-sharing'>Share this on Facebook</a>" |
|
end |
|
end |
Nice. I've got to get these links going on my sites.
And the schema.org stuff...