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
ECS_ASSOCIATE_TAG =
ENV['ECS_ASSOCIATE_TAG'] || ''
AWS_ACCESS_KEY_ID =
ENV['AWS_ACCESS_KEY_ID'] || ''
AWS_SECRET_KEY =
ENV['AWS_SECRET_KEY'] || ''

Instance Method Summary collapse

Constructor Details

#initializeAmazonResultCache

Returns a new instance of AmazonResultCache.



37
38
39
40
# File 'lib/jekyll-amazon/amazon_tag.rb', line 37

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

Instance Method Details

#item_lookup(asin) ⇒ Object

Raises:

  • (ArgumentError)


65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/jekyll-amazon/amazon_tag.rb', line 65

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
  data = create_data(item)
  write_cache(asin, data)
  @result_cache[asin] = data
  @result_cache[asin]
end

#setup(context) ⇒ Object



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

def setup(context)
  site = context.registers[:site]
  # ::Amazon::Ecs.debug = true
  ::Amazon::Ecs.configure do |options|
    options[:associate_tag]     = ECS_ASSOCIATE_TAG
    options[:AWS_access_key_id] = AWS_ACCESS_KEY_ID
    options[:AWS_secret_key]    = AWS_SECRET_KEY
    options[:response_group]    = RESPONSE_GROUP
    options[:country]           = ENV['ECS_COUNTRY'] || 'jp'
  end

  setup_i18n(site)
end

#setup_i18n(site) ⇒ Object



56
57
58
59
60
61
62
63
# File 'lib/jekyll-amazon/amazon_tag.rb', line 56

def setup_i18n(site)
  locale = 'ja'
  locale = site.config['amazon_locale'] if site.config['amazon_locale']
  I18n.enforce_available_locales = false
  I18n.locale = locale.to_sym
  dir = File.expand_path(File.dirname(__FILE__))
  I18n.load_path = [dir + "/../../locales/#{locale}.yml"]
end