class JobParser::Cache::TextFile

Public Instance Methods

cache_expired?(url) click to toggle source
# File lib/jobparser/cache/textfile.rb, line 26
def cache_expired?(url)
  time = File.mtime(path_for_url(url))
  expire_time = time + JobParser.config[:cache_expire]
  Time.now > expire_time
end
clear_all() click to toggle source
# File lib/jobparser/cache/textfile.rb, line 22
def clear_all
  cache_files.each { |f| File.delete(f) }
end
get(url) click to toggle source
# File lib/jobparser/cache/textfile.rb, line 14
def get(url)
  path = path_for_url(url)
  obj = JSON.parse(IO.read(path))
  sym_obj = make_object_keys_symbols(obj)
  sym_obj[:from_cache] = true
  sym_obj
end
has_cache_for_url?(url) click to toggle source
# File lib/jobparser/cache/textfile.rb, line 6
def has_cache_for_url?(url)
  File.exist?(path_for_url(url))
end
store(job_hash) click to toggle source
# File lib/jobparser/cache/textfile.rb, line 10
def store(job_hash)
  write_to_file(path_for_url(job_hash[:url]), job_hash.to_json)
end
view_cache() click to toggle source
# File lib/jobparser/cache/textfile.rb, line 32
def view_cache
  [].tap do |res|
    cache_files.each do |f|
      contents = JSON.parse(IO.read(f))
      res.push({
        :url => contents["url"],
        :created => File.mtime(f)
      })
    end
  end
end

Private Instance Methods

cache_files() click to toggle source
# File lib/jobparser/cache/textfile.rb, line 52
def cache_files
  Dir[File.join(JobParser.config[:cache_location], "*.txt")]
end
make_object_keys_symbols(obj) click to toggle source
# File lib/jobparser/cache/textfile.rb, line 46
def make_object_keys_symbols(obj)
  {}.tap do |sym_obj|
    obj.each { |k, v| sym_obj[k.to_sym] = v }
  end
end
md5_url(url) click to toggle source
# File lib/jobparser/cache/textfile.rb, line 65
def md5_url(url)
  "#{Digest::MD5.hexdigest(url)}.txt"
end
path_for_url(url) click to toggle source
# File lib/jobparser/cache/textfile.rb, line 60
def path_for_url(url)
  cache_dir = JobParser.config[:cache_location]
  File.join(cache_dir, md5_url(url))
end
write_to_file(path, contents) click to toggle source
# File lib/jobparser/cache/textfile.rb, line 56
def write_to_file(path, contents)
  File.open(path, "w") { |f| f.puts(contents) }
end