From 4c6ba9c89e4386539ba07a27ca1c7ba57cc0d9ae Mon Sep 17 00:00:00 2001 From: Guillaume Dott Date: Wed, 17 Dec 2014 18:12:52 +0100 Subject: [PATCH] Use Sinatra modular application --- nikoli.rb | 54 +++++++++++++++++++++++++++++------------------------- 1 file changed, 29 insertions(+), 25 deletions(-) diff --git a/nikoli.rb b/nikoli.rb index 52b2bf9..221efb3 100755 --- a/nikoli.rb +++ b/nikoli.rb @@ -1,6 +1,6 @@ #!/usr/bin/env ruby -require 'sinatra' +require 'sinatra/base' require 'sinatra/content_for' require 'sinatra/json' @@ -9,32 +9,36 @@ require 'coffee-script' require 'slim' require 'sass' -GAMES = %i{akari hitori nurikabe sudoku} +class Nikoli < Sinatra::Application + GAMES = %i{akari hitori nurikabe sudoku} -set :data_folder, File.expand_path('./data') + set :data_folder, File.expand_path('./data') -get('/application.css') { scss :application } -get('/application.js') { coffee :application } + get('/application.css') { scss :application } + get('/application.js') { coffee :application } -get('/') { slim :index, locals: {games: GAMES} } + get('/') { slim :index, locals: {games: GAMES} } -GAMES.each do |game| - get("/#{game}") { slim game } - get("/#{game}.js") { coffee game } -end - -get "/data/:game.json" do |game| - data_file = Pathname.new(File.join(settings.data_folder, game)) - halt(404) unless data_file.directory? - - 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) - - data = YAML.load_file(data_file)['data'] - json Array(data).sample + GAMES.each do |game| + get("/#{game}") { slim game } + get("/#{game}.js") { coffee game } + end + + get "/data/:game.json" do |game| + data_file = Pathname.new(File.join(settings.data_folder, game)) + halt(404) unless data_file.directory? + + 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) + + data = YAML.load_file(data_file)['data'] + json Array(data).sample + end + + run! if app_file == $0 end