Sha256: b206ed188ce6091ee473934ad028ad6f4f312e2f04e26680553350797424c500

Contents?: true

Size: 667 Bytes

Versions: 1

Compression:

Stored size: 667 Bytes

Contents

require 'faraday'
require 'api_cache'

module FaradayAPICache
  class Middleware < Faraday::Middleware
    def initialize(app, options = {})
      super(app)
      @options = options.to_hash
    end

    def call(env)
      if env[:method] == :get
        APICache.get(env[:url].to_s, @options) do
          APICache.logger.debug 'FaradayAPICache cache miss... Caching.'
          @app.call(env)
        end
      else
        APICache.logger.debug 'FaradayAPICache cache hit.'
        @app.call(env)
      end
    end

    def self.clear
      self.store = APICache::MemoryStore.new
    end

    def self.store=(store)
      APICache.store = store
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
faraday_api_cache-0.0.1 lib/faraday_api_cache/middleware.rb