Sha256: 72343255f2c165e322e66c44bb3c41a642d2315e6073256618305f16d837ad9d

Contents?: true

Size: 1.21 KB

Versions: 133

Compression:

Stored size: 1.21 KB

Contents

require "http/client"

class RemoteDataFile
  BASE_URL  = "https://raw.githubusercontent.com/exercism/problem-specifications/master/exercises"
  DATA_FILE = "canonical-data.json"

  def initialize(@test_name : String); end

  def path
    if File.exists?(cache_file)
      cache_file
    else
      remote_file
    end
  end

  private def remote_file
    File.write(cache_file, body)
    cache_file
  end

  private def cache_file
    File.join(cache_dir, DATA_FILE)
  end

  private def cache_dir
    dirpath = File.expand_path(File.join("cache", @test_name), generator_root)

    unless Dir.exists?(dirpath)
      Dir.mkdir_p(dirpath)
    end

    dirpath
  end

  private def generator_root
    File.expand_path(File.join("..", ".."), __FILE__)
  end

  private def body
    response = HTTP::Client.get(url)
    case response.status_code
    when 200
      response.body
    when 404
      raise "A #{DATA_FILE} doesn't exist for #{@test_name} in problem-specifications. " +
            "Go make one!"
    else
      raise "Error while requesting the #{@test_name} data file from GitHub... " +
            "Status was #{response.status_code}"
    end
  end

  private def url
    [BASE_URL, @test_name, DATA_FILE].join("/")
  end
end

Version data entries

133 entries across 133 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/crystal/generator/src/remote_data_file.cr
trackler-2.2.1.179 tracks/crystal/generator/src/remote_data_file.cr
trackler-2.2.1.178 tracks/crystal/generator/src/remote_data_file.cr
trackler-2.2.1.177 tracks/crystal/generator/src/remote_data_file.cr
trackler-2.2.1.176 tracks/crystal/generator/src/remote_data_file.cr
trackler-2.2.1.175 tracks/crystal/generator/src/remote_data_file.cr
trackler-2.2.1.174 tracks/crystal/generator/src/remote_data_file.cr
trackler-2.2.1.173 tracks/crystal/generator/src/remote_data_file.cr
trackler-2.2.1.172 tracks/crystal/generator/src/remote_data_file.cr
trackler-2.2.1.171 tracks/crystal/generator/src/remote_data_file.cr
trackler-2.2.1.170 tracks/crystal/generator/src/remote_data_file.cr
trackler-2.2.1.169 tracks/crystal/generator/src/remote_data_file.cr
trackler-2.2.1.167 tracks/crystal/generator/src/remote_data_file.cr
trackler-2.2.1.166 tracks/crystal/generator/src/remote_data_file.cr
trackler-2.2.1.165 tracks/crystal/generator/src/remote_data_file.cr
trackler-2.2.1.164 tracks/crystal/generator/src/remote_data_file.cr
trackler-2.2.1.163 tracks/crystal/generator/src/remote_data_file.cr
trackler-2.2.1.162 tracks/crystal/generator/src/remote_data_file.cr
trackler-2.2.1.161 tracks/crystal/generator/src/remote_data_file.cr
trackler-2.2.1.160 tracks/crystal/generator/src/remote_data_file.cr