class Jekyll::Amazon::AmazonResultCache
Constants
- CACHE_DIR
- ITEM_HASH
- RESPONSE_GROUP
Public Class Methods
new()
click to toggle source
# File lib/jekyll-amazon/amazon_tag.rb, line 30 def initialize @result_cache = {} FileUtils.mkdir_p(CACHE_DIR) end
Public Instance Methods
item_lookup(asin)
click to toggle source
# 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)
click to toggle source
# 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
Private Instance Methods
create_data(item)
click to toggle source
# File lib/jekyll-amazon/amazon_tag.rb, line 91 def create_data(item) return unless item ITEM_HASH.each_with_object({}) do |(key, value), hash| hash[key] = item.get(value).to_s end end
debug?()
click to toggle source
# File lib/jekyll-amazon/amazon_tag.rb, line 66 def debug? ENV.fetch('JEKYLL_AMAZON_DEBUG', 'false') == 'true' end
read_cache(asin)
click to toggle source
# File lib/jekyll-amazon/amazon_tag.rb, line 70 def read_cache(asin) path = File.join(CACHE_DIR, asin) return nil unless File.exist?(path) File.open(path, 'r') { |f| Marshal.load(f.read) } end
retry_api() { || ... }
click to toggle source
# File lib/jekyll-amazon/amazon_tag.rb, line 81 def retry_api yield rescue retry_count ||= 0 retry_count += 1 sleep retry_count retry if retry_count <= 5 raise end
save(asin, item)
click to toggle source
# File lib/jekyll-amazon/amazon_tag.rb, line 59 def save(asin, item) data = create_data(item) write_cache(asin, data) @result_cache[asin] = data @result_cache[asin] end
write_cache(asin, obj)
click to toggle source
# File lib/jekyll-amazon/amazon_tag.rb, line 76 def write_cache(asin, obj) path = File.join(CACHE_DIR, asin) File.open(path, 'w') { |f| f.write(Marshal.dump(obj)) } end