class Alexandria::BookProviders::AmazonProvider

Constants

LOCALES

CACHE_DIR = File.join(Alexandria::Library::DIR, '.amazon_cache')

LOCALE_URLS

Public Class Methods

new() click to toggle source
# File lib/alexandria/book_providers/amazon_aws.rb, line 23
def initialize
  super("Amazon", "Amazon")
  # prefs.add("enabled", _("Enabled"), true, [true,false])
  prefs.add("locale", _("Locale"), "us", AmazonProvider::LOCALES)
  prefs.add("dev_token", _("Access key ID"), "")
  prefs.add("secret_key", _("Secret access key"), "")
  prefs.add("associate_tag", _("Associate Tag"), "")

  prefs.read
  token = prefs.variable_named("dev_token")
  # kill old (shorter) tokens, or previously distributed Access Key Id (see #26250)

  if token
    token.new_value = token.value.strip if token.value != token.value.strip
    if (token.value.size != 20) || (token.value == "0J356Z09CN88KB743582")
      token.new_value = ""
    end
  end

  secret = prefs.variable_named("secret_key")
  if secret && (secret.value != secret.value.strip)
    secret.new_value = secret.value.strip
  end

  associate = prefs.variable_named("associate_tag") or return

  associate.new_value = "rubyalexa-20" if associate.value.strip.empty?
  if associate.value != associate.value.strip
    associate.new_value = associate.value.strip
  end
end

Public Instance Methods

normalize(str) click to toggle source
# File lib/alexandria/book_providers/amazon_aws.rb, line 212
def normalize(str)
  str = str.squeeze(" ").strip unless str.nil?
  str
end
url(book) click to toggle source
# File lib/alexandria/book_providers/amazon_aws.rb, line 203
def url(book)
  isbn = Library.canonicalise_isbn(book.isbn)
  url = LOCALE_URLS.fetch(prefs["locale"])
  url % isbn
rescue StandardError => ex
  log.warn { "Cannot create url for book #{book}; #{ex.message}" }
  nil
end

Private Instance Methods

exact_match_or_first(criterion, results) click to toggle source
# File lib/alexandria/book_providers/amazon_aws.rb, line 219
def exact_match_or_first(criterion, results)
  log.info { "Found multiple results for lookup: checking for exact isbn match" }
  query_isbn_canon = Library.canonicalise_ean(criterion)
  exact_match = results.find do |book, _|
    book_isbn_canon = Library.canonicalise_ean(book.isbn)
    query_isbn_canon == book_isbn_canon
  end

  if exact_match
    # gone through all and no ISBN match, so just return first result
    log.info do
      "no more results to check. Returning first result, just an approximation"
    end
    exact_match
  else
    results.first
  end
end