nikoli/nikoli.rb

45 lines
1.1 KiB
Ruby
Raw Normal View History

2014-12-05 12:52:12 +01:00
#!/usr/bin/env ruby
2014-12-17 18:12:52 +01:00
require 'sinatra/base'
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-17 18:12:52 +01:00
class Nikoli < Sinatra::Application
GAMES = %i{akari hitori nurikabe sudoku}
2014-12-08 12:32:01 +01:00
2014-12-17 18:12:52 +01:00
set :data_folder, File.expand_path('./data')
2014-12-17 18:12:52 +01:00
get('/application.css') { scss :application }
get('/application.js') { coffee :application }
2014-12-05 12:52:12 +01:00
2014-12-17 18:12:52 +01:00
get('/') { slim :index, locals: {games: GAMES} }
2014-12-05 12:52:12 +01:00
2014-12-17 18:12:52 +01:00
GAMES.each do |game|
get("/#{game}") { slim game }
get("/#{game}.js") { coffee game }
end
2014-12-17 18:12:52 +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:38:12 +01:00
2014-12-17 18:12:52 +01:00
json data_file.children(false).select { |path| path.to_s.end_with?('.yml') }
.map { |path| path.to_s.sub(/.yml$/, '') }
end
get "/data/:game/:file.json" do |game, file|
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
2014-12-17 18:12:52 +01:00
data = YAML.load_file(data_file)['data']
json Array(data).sample
end
2014-12-17 18:12:52 +01:00
run! if app_file == $0
end