Class: REDCap::Cache

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

Class Method Summary collapse

Class Method Details

.fetch(url, &block) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/red_cap/cache.rb', line 3

def self.fetch url, &block
  key = Digest::MD5.hexdigest(url)

  dir = "tmp/redcap_cache"
  path = dir + "/#{key}"

  if REDCap.cache && ::File.exists?(path)
    ::File.read(path)
  else
    raw = block.call

    if REDCap.cache
      FileUtils.mkdir_p dir
      ::File.write(path, raw)
    end
    raw
  end
end