Class: Amazon::Hacks::Link
- Inherits:
-
Object
- Object
- Amazon::Hacks::Link
- Includes:
- Countries
- Defined in:
- lib/amazon-hacks.rb
Instance Attribute Summary collapse
-
#asin ⇒ Object
readonly
Returns the value of attribute asin.
-
#country ⇒ Object
readonly
Returns the value of attribute country.
Class Method Summary collapse
-
.extract_asin(url) ⇒ Object
Extracts the Amazon Standard Identification Number (ASIN) from a string
url
. -
.extract_country(url) ⇒ Object
This method analyzes the
url
string and returns a symbol representing the country (eg,:us
).
Instance Method Summary collapse
-
#associate_url(assoc_id) ⇒ Object
Returns a normalized URL for the product with an Amazon Associate ID appended to it.
-
#initialize(url = nil, country = nil, asin = nil) ⇒ Link
constructor
A new instance of Link.
-
#normalize ⇒ Object
Returns a normalized form of the Link’s URL.
-
#normalize! ⇒ Object
Like #normalize, but changes the internal url.
-
#url ⇒ Object
(also: #to_s)
Returns the saved URL.
-
#url=(url) ⇒ Object
Sets the internal url of the link to be
url
.
Methods included from Countries
#country_host, #country_imgcode, #country_imghost
Constructor Details
#initialize(url = nil, country = nil, asin = nil) ⇒ Link
Returns a new instance of Link.
59 60 61 62 63 64 65 66 67 |
# File 'lib/amazon-hacks.rb', line 59 def initialize(url=nil, country=nil, asin=nil) if url.nil? @url = @country = @asin = nil else @url = URI.parse(url) @country = country ? country : Amazon::Hacks::Link.extract_country(url) @asin = asin ? asin : Amazon::Hacks::Link.extract_asin(url) end end |
Instance Attribute Details
#asin ⇒ Object (readonly)
Returns the value of attribute asin.
57 58 59 |
# File 'lib/amazon-hacks.rb', line 57 def asin @asin end |
#country ⇒ Object (readonly)
Returns the value of attribute country.
57 58 59 |
# File 'lib/amazon-hacks.rb', line 57 def country @country end |
Class Method Details
.extract_asin(url) ⇒ Object
Extracts the Amazon Standard Identification Number (ASIN) from a string url
. Each product on an Amazon store has a unique ASIN used to identify it. As Amazon’s help documents describe it:
“For books, the ASIN is the same as the ISBN number, but for all other products a new ASIN is created when the item is uploaded to our catalog”
ASIN values are specific to the store they’re applied to and can not be used against another store nor are they guaranteed to be globally unique. Put another way, the ASIN for the US edition of a book will not return the UK edition when used with the UK store and might return nothing or something completely different. Also, ASINs are alphanumeric strings, not numbers.
Amazon has used several different major URL formats interchangeably and this function extracts the ASIN from the following URL paths:
/o/asin/<ASIN VALUE>
/exec/obidos/ASIN/<ASIN VALUE>
/exec/obidos/tg/detail/-/<ASIN VALUE>
/long-title-goes-here/dp/<ASIN VALUE>
This is only an informally gathered list. If you find a case where extract_asin
fails, please send me the URL. If the ASIN is not matched, returns nil
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/amazon-hacks.rb', line 127 def Link.extract_asin(url) u = URI.parse(url) asin = nil asin_regexps = [ /^\/o\/asin\/([^\/]+)/, /^\/exec\/obidos\/ASIN\/([^\/]+)/, /^\/exec\/obidos\/tg\/detail\/-\/([^\/]+)/, /^\/[^\/]+\/dp\/([^\/]+)/ ] asin_regexps.each do |re| md = re.match(u.path) if md then asin = md[1] break end end asin end |
.extract_country(url) ⇒ Object
This method analyzes the url
string and returns a symbol representing the country (eg, :us
). If the host is not recognized, returns nil
152 153 154 155 156 |
# File 'lib/amazon-hacks.rb', line 152 def Link.extract_country(url) u = URI.parse(url) c = COUNTRIES.find { |key, value| value.host == u.host } c[0] unless c.nil? #.country unless c.nil? end |
Instance Method Details
#associate_url(assoc_id) ⇒ Object
Returns a normalized URL for the product with an Amazon Associate ID appended to it.
161 162 163 |
# File 'lib/amazon-hacks.rb', line 161 def associate_url(assoc_id) normalize + "/#{assoc_id}" end |
#normalize ⇒ Object
Returns a normalized form of the Link’s URL. This is the shortest valid URL that links to the product on Amazon
For example, the normalized form of a URL like:
would be:
www.amazon.com/o/asin/0201485672
Since it removes the session and other variables from the URL, normalization is very useful for comparing two Amazon links directly. The Link== method does NOT normalize, but you might want to consider that if you need it.
94 95 96 |
# File 'lib/amazon-hacks.rb', line 94 def normalize "http://#{country_host}/o/asin/#{asin}" end |
#normalize! ⇒ Object
Like #normalize, but changes the internal url
167 168 169 170 171 |
# File 'lib/amazon-hacks.rb', line 167 def normalize! u = normalize @url = URI.parse(u) self end |
#url ⇒ Object Also known as: to_s
Returns the saved URL
175 176 177 |
# File 'lib/amazon-hacks.rb', line 175 def url @url.to_s end |
#url=(url) ⇒ Object
Sets the internal url of the link to be url
. Also parses the values of the asin
and country
from the URL
72 73 74 75 76 |
# File 'lib/amazon-hacks.rb', line 72 def url=(url) @url = URI.parse(url) @country = Amazon::Hacks::Link.extract_country(url) @asin = Amazon::Hacks::Link.extract_asin(url) end |