Class: JobParser::Cache::TextFile

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

Instance Method Summary collapse

Instance Method Details

#cache_expired?(url) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
29
30
# 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_allObject



22
23
24
# File 'lib/jobparser/cache/textfile.rb', line 22

def clear_all
  cache_files.each { |f| File.delete(f) }
end

#get(url) ⇒ Object



14
15
16
17
18
19
20
# 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) ⇒ Boolean

Returns:

  • (Boolean)


6
7
8
# File 'lib/jobparser/cache/textfile.rb', line 6

def has_cache_for_url?(url)
  File.exist?(path_for_url(url))
end

#store(job_hash) ⇒ Object



10
11
12
# 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_cacheObject



32
33
34
35
36
37
38
39
40
41
42
# 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