Class: Shb::Cache
- Inherits:
-
Object
- Object
- Shb::Cache
- Defined in:
- lib/shb/cache.rb
Class Method Summary collapse
- .cache_file(uri, options = {}) ⇒ Object
- .read(method, uri, options = {}) ⇒ Object
- .write(response, uri, options = {}) ⇒ Object
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, = {}) bits = [] bits << Rails.root if defined?(::Rails) bits << 'tmp' bits << uri.host path = uri.path == '/' ? 'ROOT' : uri.path.parameterize query = .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, = {}) file = cache_file(uri, ) 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, = {}) file = cache_file(uri, ) FileUtils.mkdir_p(File.dirname(file)) File.open(file, 'w') do |f| f.puts YAML::dump(response.response) end end |