Module: TottoriOpenDataCatalog::Net

Defined in:
lib/tottori-opendata-catalog/net.rb

Class Method Summary collapse

Class Method Details

.get(url, cache: true) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/tottori-opendata-catalog/net.rb', line 11

def get(url, cache:true)
  return open(url, &:read) unless cache
  path = File.join(Dir.tmpdir, Digest::SHA1.hexdigest(url))
  @memo ||= {}
  if @memo.include?(path)
    @memo[path]
  else
    if File.exists?(path)
      @memo[path] ||= open(path, &:read)
    else
      content = open(url, &:read)
      open(path, 'w'){ |io| io.write(content) }
      @memo[path] ||= content
    end
  end
end