From c38c85f979dc73c4dbf039fe4f225eba3b20c61b Mon Sep 17 00:00:00 2001 From: overgrow Date: Fri, 29 Nov 2024 15:52:46 +0100 Subject: [PATCH] Add support for Amazon short links (#79) Adds support for the following short links and introduces a new setting for a tracking code to use with amzn.eu. * a.co * amzn.eu * amzn.to --- config/locales/server.en.yml | 3 ++- config/settings.yml | 3 +++ lib/affiliate_processor.rb | 5 ++++- spec/lib/affiliate_processor_spec.rb | 8 ++++++++ 4 files changed, 17 insertions(+), 2 deletions(-) diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index 295c682..297863b 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -5,7 +5,7 @@ en: affiliate_amazon_cn: "Your amazon.cn (China) Associates ID." affiliate_amazon_co_jp: "Your amazon.co.jp (Japan) Associates ID." affiliate_amazon_co_uk: "Your amazon.co.uk (United Kingdom) Associates ID." - affiliate_amazon_com: "Your amazon.com/amzn.com (United States) Associates ID." + affiliate_amazon_com: "Your amazon.com/amzn.com/amzn.to (United States) Associates ID." affiliate_amazon_com_au: "Your amazon.com.au (Australia) Associates ID." affiliate_amazon_com_br: "Your amazon.com.br (Brazil) Associates ID." affiliate_amazon_com_mx: "Your amazon.com.mx (Mexico) Associates ID." @@ -15,4 +15,5 @@ en: affiliate_amazon_in: "Your amazon.in (India) Associates ID." affiliate_amazon_it: "Your amazon.it (Italy) Associates ID." affiliate_amazon_nl: "Your amazon.nl (Netherlands) Associates ID." + affiliate_amazon_eu: "Your amzn.eu (Europe) Associates ID." affiliate_ldlc_com: "Your ldlc.com Affiliate ID (without the #)." diff --git a/config/settings.yml b/config/settings.yml index 8f1488b..84c592d 100644 --- a/config/settings.yml +++ b/config/settings.yml @@ -44,6 +44,9 @@ discourse_affiliate: affiliate_amazon_nl: default: "" client: true + affiliate_amazon_eu: + default: "" + client: true affiliate_ldlc_com: default: "" client: true diff --git a/lib/affiliate_processor.rb b/lib/affiliate_processor.rb index f51441a..d35e3d8 100644 --- a/lib/affiliate_processor.rb +++ b/lib/affiliate_processor.rb @@ -24,7 +24,7 @@ def self.create_amazon_rule(domain) def self.rules return @rules if @rules - postfixes = %w[com com.au com.br com.mx ca cn co.jp co.uk de es fr in it nl] + postfixes = %w[com com.au com.br com.mx ca cn co.jp co.uk de es fr in it nl to co eu] rules = {} @@ -32,6 +32,9 @@ def self.rules rule = create_amazon_rule(postfix) rules["amzn.com"] = rule if postfix == "com" + rules["amzn.to"] = create_amazon_rule("com") if postfix == "to" + rules["amzn.eu"] = rule if postfix == "eu" + rules["a.co"] = create_amazon_rule("com") if postfix == "co" rules["www.amazon.#{postfix}"] = rule rules["smile.amazon.#{postfix}"] = rule rules["amazon.#{postfix}"] = rule diff --git a/spec/lib/affiliate_processor_spec.rb b/spec/lib/affiliate_processor_spec.rb index 36d319b..38eac35 100644 --- a/spec/lib/affiliate_processor_spec.rb +++ b/spec/lib/affiliate_processor_spec.rb @@ -20,6 +20,7 @@ def r(url) SiteSetting.affiliate_amazon_com = "sams-shop" SiteSetting.affiliate_amazon_ca = "ca-sams-shop" SiteSetting.affiliate_amazon_com_au = "au-sams-shop" + SiteSetting.affiliate_amazon_eu = "eu-sams-shop" expect(r("https://www.amazon.com")).to eq("https://www.amazon.com?tag=sams-shop") expect(r("http://www.amazon.com/some_product?xyz=1")).to eq( @@ -47,6 +48,13 @@ def r(url) ).to eq( "https://www.amazon.ca/Dragon-Quest-Echoes-Elusive-Age-PlayStation/dp/B07BP3J6RG/ref=br_asw_pdt-5?tag=ca-sams-shop", ) + expect(r("https://amzn.to/d/some_short_link")).to eq( + "https://amzn.to/d/some_short_link?tag=sams-shop", + ) + expect(r("https://amzn.eu/d/some_short_link")).to eq( + "https://amzn.eu/d/some_short_link?tag=eu-sams-shop", + ) + expect(r("https://a.co/some_short_link")).to eq("https://a.co/some_short_link?tag=sams-shop") # keep node (BrowseNodeSearch) query parameter expect(r("https://www.amazon.com/b?ie=UTF8&node=13548845011")).to eq(