nikoli/nikoli.rb

40 lines
980 B
Ruby
Raw Normal View History

2014-12-05 12:52:12 +01:00
#!/usr/bin/env ruby
require 'sinatra'
2014-12-05 17:27:23 +01:00
require 'sinatra/content_for'
require 'sinatra/json'
2014-12-05 17:27:23 +01:00
require 'yaml'
2014-12-05 12:52:12 +01:00
require 'coffee-script'
require 'slim'
require 'sass'
2014-12-08 17:48:22 +01:00
GAMES = %i{hitori nurikabe sudoku}
2014-12-08 12:32:01 +01:00
set :data_folder, File.expand_path('./data')
2014-12-05 12:52:12 +01:00
get('/application.css') { scss :application }
get('/application.js') { coffee :application }
2014-12-05 12:52:12 +01:00
2014-12-08 12:32:01 +01:00
get('/') { slim :index, locals: {games: GAMES} }
2014-12-05 12:52:12 +01:00
2014-12-08 12:32:01 +01:00
GAMES.each do |game|
2014-12-05 12:52:12 +01:00
get("/#{game}") { slim game }
get("/#{game}.js") { coffee game }
end
2014-12-09 12:38:12 +01:00
get "/data/:game.json" do |game|
data_file = Pathname.new(File.join(settings.data_folder, game))
halt(404) unless data_file.directory?
2014-12-09 12:50:52 +01:00
json data_file.children(false).select { |path| path.to_s.end_with?('.yml') }
.map { |path| path.to_s.sub(/.yml$/, '') }
2014-12-09 12:38:12 +01:00
end
get "/data/:game/:file.json" do |game, file|
2014-12-09 12:38:12 +01:00
data_file = File.expand_path(File.join(settings.data_folder, game, "#{file}.yml"))
halt(404) unless File.exist?(data_file)
2014-12-09 12:38:12 +01:00
json YAML.load_file(data_file)['data'].sample
end