module WebCache::CacheOperations

Attributes

auth[R]
dir[W]
last_error[R]
pass[R]
permissions[RW]
user[R]

Public Class Methods

new(dir: 'cache', life: '1h', auth: nil, permissions: nil) click to toggle source
# File lib/webcache/cache_operations.rb, line 11
def initialize(dir: 'cache', life: '1h', auth: nil, permissions: nil)
  @dir = dir
  @life = life_to_seconds life
  @enabled = true
  @auth = convert_auth auth
  @permissions = permissions
end

Public Instance Methods

auth=(auth) click to toggle source
# File lib/webcache/cache_operations.rb, line 66
def auth=(auth)
  convert_auth auth
end
cached?(url) click to toggle source
# File lib/webcache/cache_operations.rb, line 40
def cached?(url)
  path = get_path url
  File.exist?(path) and !stale?(path)
end
clear(url) click to toggle source
# File lib/webcache/cache_operations.rb, line 57
def clear(url)
  path = get_path url
  FileUtils.rm path if File.exist? path
end
dir() click to toggle source
# File lib/webcache/cache_operations.rb, line 36
def dir
  @dir ||= 'cache'
end
disable() click to toggle source
# File lib/webcache/cache_operations.rb, line 53
def disable
  @enabled = false
end
enable() click to toggle source
# File lib/webcache/cache_operations.rb, line 49
def enable
  @enabled = true
end
enabled?() click to toggle source
# File lib/webcache/cache_operations.rb, line 45
def enabled?
  @enabled ||= (@enabled.nil? ? true : @enabled)
end
flush() click to toggle source
# File lib/webcache/cache_operations.rb, line 62
def flush
  FileUtils.rm_rf dir if Dir.exist? dir
end
get(url, force: false) click to toggle source
# File lib/webcache/cache_operations.rb, line 19
def get(url, force: false)
  return http_get url unless enabled?

  path = get_path url
  clear url if force || stale?(path)

  get! path, url
end
life() click to toggle source
# File lib/webcache/cache_operations.rb, line 28
def life
  @life ||= 3600
end
life=(new_life) click to toggle source
# File lib/webcache/cache_operations.rb, line 32
def life=(new_life)
  @life = life_to_seconds new_life
end

Private Instance Methods

basic_auth?() click to toggle source
# File lib/webcache/cache_operations.rb, line 102
def basic_auth?
  !!(user and pass)
end
convert_auth(opts) click to toggle source
# File lib/webcache/cache_operations.rb, line 132
def convert_auth(opts)
  @user = nil
  @pass = nil
  @auth = nil

  if opts.respond_to?(:has_key?) && opts.has_key?(:user) && opts.has_key?(:pass)
    @user = opts[:user]
    @pass = opts[:pass]
  else
    @auth = opts
  end
end
get!(path, url) click to toggle source
# File lib/webcache/cache_operations.rb, line 72
def get!(path, url)
  return load_file_content path if File.exist? path

  response = http_get url
  save_file_content path, response unless !response || response.error
  response
end
get_path(url) click to toggle source
# File lib/webcache/cache_operations.rb, line 80
def get_path(url)
  File.join dir, Digest::MD5.hexdigest(url)
end
http_get(url) click to toggle source
# File lib/webcache/cache_operations.rb, line 95
def http_get(url)
  Response.new http_response(url)
rescue => e
  url = URI.parse url
  Response.new error: e.message, base_uri: url, content: e.message
end
http_response(url) click to toggle source
# File lib/webcache/cache_operations.rb, line 106
def http_response(url)
  if basic_auth?
    HTTP.basic_auth(user: user, pass: pass).follow.get url
  elsif auth
    HTTP.auth(auth).follow.get url
  else
    HTTP.follow.get url
  end
end
life_to_seconds(arg) click to toggle source
# File lib/webcache/cache_operations.rb, line 120
def life_to_seconds(arg)
  arg = arg.to_s

  case arg[-1]
  when 's' then arg[0..].to_i
  when 'm' then arg[0..].to_i * 60
  when 'h' then arg[0..].to_i * 60 * 60
  when 'd' then arg[0..].to_i * 60 * 60 * 24
  else; arg.to_i
  end
end
load_file_content(path) click to toggle source
# File lib/webcache/cache_operations.rb, line 84
def load_file_content(path)
  Marshal.load File.binread(path)
end
save_file_content(path, response) click to toggle source
# File lib/webcache/cache_operations.rb, line 88
def save_file_content(path, response)
  FileUtils.mkdir_p dir
  File.open path, 'wb', permissions do |file|
    file.write Marshal.dump(response)
  end
end
stale?(path) click to toggle source
# File lib/webcache/cache_operations.rb, line 116
def stale?(path)
  life.positive? and File.exist?(path) and Time.new - File.mtime(path) >= life
end