Class: JobParser::Cache::MongoStore

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

Defined Under Namespace

Classes: Job

Instance Method Summary collapse

Instance Method Details

#cache_expired?(url) ⇒ Boolean

Returns:

  • (Boolean)


16
17
18
19
20
# File 'lib/jobparser/cache/mongostore.rb', line 16

def cache_expired?(url)
  job = job_for_url(url).first
  expire_time = (job.created_at + JobParser.config[:cache_expire])
  Time.now > expire_time
end

#clear_allObject



32
33
34
# File 'lib/jobparser/cache/mongostore.rb', line 32

def clear_all
  MongoStore::Job.each(&:delete)
end

#get(url) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/jobparser/cache/mongostore.rb', line 22

def get(url)
  job = job_for_url(url).first
  {}.tap do |job_obj|
    job.attributes.each do |k, v|
      job_obj[k.to_sym] = v unless %w{created_at _id updated_at}.include?(k)
    end
    job_obj[:from_cache] = true
  end
end

#has_cache_for_url?(url) ⇒ Boolean

Returns:

  • (Boolean)


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

def has_cache_for_url?(url)
  job_for_url(url).count > 0
end

#store(hash) ⇒ Object



10
11
12
13
14
# File 'lib/jobparser/cache/mongostore.rb', line 10

def store(hash)
  job_for_url(hash[:url]).delete
  hash = strip_fields_not_stored(hash)
  Job.create(hash)
end

#view_cacheObject



36
37
38
39
40
41
42
43
44
45
# File 'lib/jobparser/cache/mongostore.rb', line 36

def view_cache
  [].tap do |res|
    Job.each do |job|
      res.push({
        :url => job.url,
        :created => job.created_at
      })
    end
  end
end