class AmazonAlbumArt::Client

Public Class Methods

new(access_key, secret_key, associate_tag, locale) click to toggle source
# File lib/amazon-album-art/client.rb, line 12
def initialize(access_key, secret_key, associate_tag, locale)
  raise ArgumentError.new("Access, secret key and associate tag are required") if access_key.empty? || secret_key.empty? || associate_tag.empty?
  
  @worker = Sucker.new(
    :locale => "us",
    :key    => access_key, 
    :secret => secret_key,
    :associate_tag => associate_tag
  )
end

Public Instance Methods

Private Instance Methods

check_response?(results, msg) click to toggle source
# File lib/amazon-album-art/client.rb, line 133
def check_response?(results, msg)
  begin
    return results.valid? || results.find("Error").size == 0
  rescue StandardError => bang
    # handled error from Sucker in some cases
    return false
  end
end
levenshtein(str1, str2) click to toggle source
# File lib/amazon-album-art/client.rb, line 90
def levenshtein(str1, str2)
  s, t = [str1.unpack('U*'), str2.unpack('U*')]
  n, m = [s.length, t.length]
  return m if (0 == n)
  return n if (0 == m)
  d = (0..m).to_a
  x = nil
  (0...n).each do |i|
      e = i+1
      (0...m).each do |j|
          cost = (s[i] == t[j]) ? 0 : 1
          x = [d[j+1] + 1, e + 1, d[j] + cost].min
          d[j] = e
          e = x
      end
      d[m] = x
  end
  return x
end
load_album(attribs) click to toggle source
# File lib/amazon-album-art/client.rb, line 120
def load_album(attribs)
  attribs.has_key?('Title') ? attribs['Title'] : ""
end
load_artist(attribs) click to toggle source
# File lib/amazon-album-art/client.rb, line 110
def load_artist(attribs)
  # found artists are returned in many different permutations
  return attribs['Artist'].first if attribs.has_key?('Artist') && attribs['Artist'].is_a?(Array)
  return attribs['Artist'] if attribs.has_key?('Artist')
  return attribs['Author'] if attribs.has_key?('Author')
  return attribs['Creator'].map { |item| item["__content__"] if item.has_key?("__content__") }.join(" and ") if attribs.has_key?("Creator") && attribs['Creator'].is_a?(Array)
  return attribs["Creator"]["__content__"] if attribs.has_key?("Creator") && attribs["Creator"].has_key?('__content__')
  return nil
end
load_images(doc, sizes) click to toggle source
# File lib/amazon-album-art/client.rb, line 124
def load_images(doc, sizes)
  # build the hash with requested values
  {}.tap do |urls|
    sizes.each do |size|
      urls.merge!({ size => doc.css("Item > ImageSets > ImageSet[@Category=\"primary\"] > #{size.to_s.capitalize}Image > URL").text })
    end
  end
end
matches?(s1, s2, tolerance = 2) click to toggle source
# File lib/amazon-album-art/client.rb, line 83
def matches?(s1, s2, tolerance = 2)
  s1 == s2 ||
  s1 =~ /#{Regexp.escape s2}/i ||
  s2 =~ /#{Regexp.escape s1}/i ||
  (levenshtein(s1, s2) <= tolerance)
end