# -*- encoding : utf-8 -*-
require 'fileutils'

def getOrCache(tmp_file, url)
  response_body = ""
  if !File.exist?tmp_file
    response_body = RestClient.get(url, :cache => false) { |response, request, result, &block|
      case response.code
      when 404
          response
      else
        response.return!(&block)
      end
    }.body;
    File.open(tmp_file, 'w') do |out|
      out.write response_body
    end
  else
    response_body = File.read(tmp_file)
  end
  return response_body
end

def getOrCacheJSON(tmp_file, json_url)
  json = {}
  if !File.exist?tmp_file
    response_body = RestClient.get(json_url, :cache => false, :accept => 'application/json') { |response, request, result, &block|
      case response.code
      when 404
          response
      else
        response.return!(&block)
      end
    }.body;
    if (response_body.match(/^(\{|\[)/))
      json = JSON.parse response_body
    end
    File.open(tmp_file, 'w') do |out|
      out.write JSON.pretty_generate json
    end
  else
    begin
      json = JSON.parse File.read(tmp_file)
    rescue
      json = {}
    end
  end
  return json
end

def tmp(parent, child)
  tmp_dir = File.join(parent, child)
  if !File.exist? tmp_dir
    FileUtils.mkdir_p tmp_dir
  end
  return tmp_dir
end
