module TottoriOpenDataCatalog::Net

Public Class Methods

get(url, cache:true) click to toggle source
# File lib/tottori-opendata-catalog/net.rb, line 8
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.exist?(path)
      @memo[path] ||= open(path, &:read)
    else
      content = open(url, &:read)
      open(path, 'w') { |io| io.write(content) }
      @memo[path] ||= content
    end
  end
end