<commit_before>require "tdiary_amazon_plugin_wrapper/version"
require 'erb'
if defined?(::Rails)
  require "tdiary_amazon_plugin_wrapper/engine"
end

class TdiaryAmazonPluginWrapper
  include ERB::Util

  autoload :AmazonHelper, "tdiary_amazon_plugin_wrapper/amazon_helper"

  DEFAULT_CONF = {
    'amazon.imgsize' => 1, # Medium
  }

  attr_reader :conf, :options

  def initialize(conf={}, options={})
    conf = DEFAULT_CONF.merge(conf)
    @conf = DummyConf.new.merge(conf)
    @options = options
    @cache_path = self.class.cache_path
    @mode = 'day'
    @cgi = DummyCGI.new
    # load plugins
    %w"ja/amazon.rb amazon.rb".each do |plugin_path|
      path = File.expand_path("../../misc/plugin/#{plugin_path}", __FILE__)
      eval(File.read(path), binding, path, 1)
    end
  end

  def add_conf_proc(key, label, genre=nil, &block)
    # ignore
  end

  @@cache_path = nil

  def self.cache_path=(cache_path)
    @@cache_path = cache_path
  end

  def self.cache_path
    return @@cache_path if @@cache_path
    if defined?(Rails)
      @@cache_path = Rails.root + 'tmp'
    else
      require 'tmpdir'
      @@cache_path = Dir.tmpdir
    end
  end

  class DummyConf < Hash
    def secure
      false
    end

    # use UTF-8 only, so do nothing
    def to_native(str, charset=nil)
      str
    end
  end

  class DummyCGI
    def smartphone?
      false
    end
  end
end<commit_msg>use cache unless file store<commit_after>require "tdiary_amazon_plugin_wrapper/version"
require 'erb'
if defined?(::Rails)
  require "tdiary_amazon_plugin_wrapper/engine"
end

class TdiaryAmazonPluginWrapper
  include ERB::Util

  autoload :AmazonHelper, "tdiary_amazon_plugin_wrapper/amazon_helper"

  DEFAULT_CONF = {
    'amazon.imgsize' => 1, # Medium
  }

  attr_reader :conf, :options

  def initialize(conf={}, options={})
    conf = DEFAULT_CONF.merge(conf)
    @conf = DummyConf.new.merge(conf)
    @options = options
    @cache_path = self.class.cache_path
    @mode = 'day'
    @cgi = DummyCGI.new
    # load plugins
    %w"ja/amazon.rb amazon.rb".each do |plugin_path|
      path = File.expand_path("../../misc/plugin/#{plugin_path}", __FILE__)
      eval(File.read(path), binding, path, 1)
    end
    if defined?(Rails) && !Rails.cache.is_a?(ActiveSupport::Cache::FileStore)
      class << self
        alias_method_chain :amazon_call_ecs, :rails_cache
      end
    end
  end

  def add_conf_proc(key, label, genre=nil, &block)
    # ignore
  end

  @@cache_path = nil

  def self.cache_path=(cache_path)
    @@cache_path = cache_path
  end

  def self.cache_path
    return @@cache_path if @@cache_path
    if defined?(Rails)
      @@cache_path = Rails.root + 'tmp'
    else
      require 'tmpdir'
      @@cache_path = Dir.tmpdir
    end
  end

  class DummyConf < Hash
    def secure
      false
    end

    # use UTF-8 only, so do nothing
    def to_native(str, charset=nil)
      str
    end
  end

  class DummyCGI
    def smartphone?
      false
    end
  end

  def amazon_call_ecs_with_rails_cache(asin, id_type, country)
    value = Rails.cache.fetch("amazon/#{country}#{asin}") do
      amazon_call_ecs_without_rails_cache(asin, id_type, country)
    end
  end
end
{'fraction_non_alphanumeric': 0.10856604957640414, 'fraction_numerical': 0.0018826482585503608, 'mean_word_length': 3.066326530612245, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3380375', 'n_tokens_mistral': 1162, 'n_tokens_neox': 1101, 'n_words': 249}