Sha256: f72150f4ee0762a2d419dc4f2a80cf464f84770c27eb5d3ce30f039a0ae2d72b

Contents?: true

Size: 974 Bytes

Versions: 8

Compression:

Stored size: 974 Bytes

Contents

require 'fileutils'

module Shb
  class Cache
    class << self

      def write(response, uri, options = {})
        file = cache_file(uri, options)
        FileUtils.mkdir_p(File.dirname(file))
        File.open(file, 'w') do |f|
          f.puts YAML::dump(response.response)
        end
      end

      def read(method, uri, options = {})
        file = cache_file(uri, options)
        return nil unless File.exist?(file)
        r = YAML::load_file(file)
        r.content_type = 'text/plain' if r.content_type.nil?
        r
      end

      def cache_file(uri, options = {})
        bits = []
        bits << Rails.root if defined?(::Rails)
        bits << 'tmp'
        bits << uri.host
        path = uri.path == '/' ? 'ROOT' : uri.path.parameterize
        query = options.empty? ? nil : "?#{HashConversions.to_params(options)}"
        bits << Digest::MD5.hexdigest([path,uri.fragment,uri.query,query].join)
        File.join(bits)
      end

    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
shb-0.1.4 lib/shb/cache.rb
shb-0.1.3 lib/shb/cache.rb
shb-0.1.2 lib/shb/cache.rb
shb-0.1.1 lib/shb/cache.rb
shb-0.1.0 lib/shb/cache.rb
shb-0.0.4 lib/shb/cache.rb
shb-0.0.3 lib/shb/cache.rb
shb-0.0.2 lib/shb/cache.rb