Class: Shb::Cache

Inherits:
Object
  • Object
show all
Defined in:
lib/shb/cache.rb

Class Method Summary collapse

Class Method Details

.cache_file(uri, options = {}) ⇒ Object



23
24
25
26
27
28
29
30
31
32
# File 'lib/shb/cache.rb', line 23

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

.read(method, uri, options = {}) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/shb/cache.rb', line 15

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

.write(response, uri, options = {}) ⇒ Object



7
8
9
10
11
12
13
# File 'lib/shb/cache.rb', line 7

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