Class: Jekyll::Amazon::AmazonResultCache

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/jekyll-amazon/amazon_tag.rb

Constant Summary collapse

CACHE_DIR =
'.amazon-cache/'.freeze
RESPONSE_GROUP =
'SalesRank,Images,ItemAttributes,EditorialReview'.freeze
ITEM_HASH =
{
  asin:             'ASIN',
  salesrank:        'SalesRank',
  title:            'ItemAttributes/Title',
  author:           'ItemAttributes/Author',
  publisher:        'ItemAttributes/Manufacturer',
  publication_date: 'ItemAttributes/PublicationDate',
  release_date:     'ItemAttributes/ReleaseDate',
  detail_page_url:  'DetailPageURL',
  small_image_url:  'SmallImage/URL',
  medium_image_url: 'MediumImage/URL',
  large_image_url:  'LargeImage/URL',
  description:      'EditorialReviews/EditorialReview/Content'
}.freeze

Instance Method Summary collapse

Constructor Details

#initializeAmazonResultCache

Returns a new instance of AmazonResultCache.



30
31
32
33
# File 'lib/jekyll-amazon/amazon_tag.rb', line 30

def initialize
  @result_cache = {}
  FileUtils.mkdir_p(CACHE_DIR)
end

Instance Method Details

#item_lookup(asin) ⇒ Object

Raises:

  • (ArgumentError)


46
47
48
49
50
51
52
53
54
55
# File 'lib/jekyll-amazon/amazon_tag.rb', line 46

def item_lookup(asin)
  return @result_cache[asin] if @result_cache.key?(asin)
  return read_cache(asin) if read_cache(asin)
  item = retry_api do
    res = ::Amazon::Ecs.item_lookup(asin)
    res.first_item
  end
  raise ArgumentError unless item
  save(asin, item)
end

#setup(country) ⇒ Object



35
36
37
38
39
40
41
42
43
44
# File 'lib/jekyll-amazon/amazon_tag.rb', line 35

def setup(country)
  ::Amazon::Ecs.debug = debug?
  ::Amazon::Ecs.configure do |options|
    options[:associate_tag]     = ENV.fetch('ECS_ASSOCIATE_TAG')
    options[:AWS_access_key_id] = ENV.fetch('AWS_ACCESS_KEY_ID')
    options[:AWS_secret_key]    = ENV.fetch('AWS_SECRET_KEY')
    options[:response_group]    = RESPONSE_GROUP
    options[:country]           = country
  end
end