Skip to content

Instantly share code, notes, and snippets.

@henrik
Created July 5, 2010 18:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save henrik/464575 to your computer and use it in GitHub Desktop.
Save henrik/464575 to your computer and use it in GitHub Desktop.
Ruby class to add your Amazon Associates referral id to an Amazon URL, replacing any that were already there. See http://github.com/henrik/delishlist.com/blob/master/lib/amazon_referralizer.rb for the latest version.
# Amazon::Referralizer
# by Henrik Nyh <http://henrik.nyh.se> 2010-07-05 under the MIT license.
# Add your Amazon Associates referral id to an Amazon URL, replacing any that were already there.
#
# E.g.:
#
# Amazon::Referralizer.referralize("http://amazon.co.uk/o/ASIN/B00168PO6U/evil-20", "good-20")
# # => "http://amazon.co.uk/o/ASIN/B00168PO6U/good-20"
#
class Amazon
class Referralizer
ASIN_RE = /[A-Z0-9]{10}/
REFERRAL_ID_RE = /\w+-\d\d/
AMAZON_URL_RE = %r{^https?://(.+\.)?(amazon\.|amzn\.com\b)}i
# http://daringfireball.net/2010/07/improved_regex_for_matching_urls
GENERAL_URL_RE = %r{(?i)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))}
def self.referralize(url, tag)
url = url.to_s
if url.match(AMAZON_URL_RE)
url.sub(%r{\b(#{ASIN_RE})\b(/#{REFERRAL_ID_RE}\b)?}, "\\1/#{tag}").
gsub(/&tag=#{REFERRAL_ID_RE}\b/, '').
gsub(/\?tag=#{REFERRAL_ID_RE}\b/, '?').sub(/\?$/, '')
else
url
end
end
def self.referralize_all(text, tag)
text.gsub(GENERAL_URL_RE) { self.referralize($&, tag) }
end
end
end
if __FILE__ == $0
require "test/unit"
class AmazonReferralizerTest < Test::Unit::TestCase
def assert_conversion(expected_url, input_url)
assert_equal(expected_url, Amazon::Referralizer.referralize(input_url, "test-20"))
end
def test_url_formats
assert_conversion "http://amazon.com/gp/product/B00168PO6U/test-20",
"http://amazon.com/gp/product/B00168PO6U"
assert_conversion "http://amazon.com/exec/obidos/tg/detail/-/B00168PO6U/test-20",
"http://amazon.com/exec/obidos/tg/detail/-/B00168PO6U"
assert_conversion "http://amazon.com/o/ASIN/B00168PO6U/test-20",
"http://amazon.com/o/ASIN/B00168PO6U"
assert_conversion "http://amazon.com/dp/B00168PO6U/test-20",
"http://amazon.com/dp/B00168PO6U"
end
def test_international
assert_conversion "http://amazon.co.uk/o/ASIN/B00168PO6U/test-20",
"http://amazon.co.uk/o/ASIN/B00168PO6U"
assert_conversion "http://amazon.de/o/ASIN/B00168PO6U/test-20",
"http://amazon.de/o/ASIN/B00168PO6U"
end
def test_short
assert_conversion "http://amzn.com/B00168PO6U/test-20",
"http://amzn.com/B00168PO6U"
end
def test_keep_ref
assert_conversion "http://www.amazon.com/dp/B00168PO6U/test-20/ref=cm_sw_su_dp",
"http://www.amazon.com/dp/B00168PO6U/ref=cm_sw_su_dp"
end
def test_replace_other_tag_in_path
assert_conversion "http://www.amazon.com/dp/B00168PO6U/test-20/ref=cm_sw_su_dp",
"http://www.amazon.com/dp/B00168PO6U/evil-20/ref=cm_sw_su_dp"
end
def test_remove_other_tag_in_query_string
assert_conversion "http://www.amazon.com/dp/B00168PO6U/test-20/ref=cm_sw_su_dp",
"http://www.amazon.com/dp/B00168PO6U/ref=cm_sw_su_dp?tag=evil-20"
assert_conversion "http://www.amazon.com/dp/B00168PO6U/test-20/ref=cm_sw_su_dp?one=1&two=2",
"http://www.amazon.com/dp/B00168PO6U/ref=cm_sw_su_dp?one=1&tag=evil-20&two=2"
end
def test_referralize_all
input = "Hello http://google.com and http://amazon.com and http://amazon.com/gp/product/B00168PO6U/evil-20 and http://amzn.com/B00168PO6U!"
expected = "Hello http://google.com and http://amazon.com and http://amazon.com/gp/product/B00168PO6U/test-20 and http://amzn.com/B00168PO6U/test-20!"
assert_equal(expected, Amazon::Referralizer.referralize_all(input, "test-20"))
end
end
end
@basicfeatures
Copy link

Tack så hämsk mycket, just vad jag behøvde!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment