class JobParser::Cache::MongoStore

Public Instance Methods

cache_expired?(url) click to toggle source
# 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_all() click to toggle source
# File lib/jobparser/cache/mongostore.rb, line 32
def clear_all
  MongoStore::Job.each(&:delete)
end
get(url) click to toggle source
# 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) click to toggle source
# File lib/jobparser/cache/mongostore.rb, line 6
def has_cache_for_url?(url)
  job_for_url(url).count > 0
end
store(hash) click to toggle source
# 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_cache() click to toggle source
# 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

Private Instance Methods

job_for_url(url) click to toggle source
# File lib/jobparser/cache/mongostore.rb, line 73
def job_for_url(url)
  Job.where(:url => url)
end
strip_fields_not_stored(hash) click to toggle source
# File lib/jobparser/cache/mongostore.rb, line 77
def strip_fields_not_stored(hash)
  {}.tap do |new_hash|
    excluded_fields = [:from_cache]
    hash.each { |k, v| new_hash[k] = v unless excluded_fields.include?(k) }
  end
end