Use Sinatra modular application

master
Guillaume Dott 2014-12-17 18:12:52 +01:00
parent 0d1dc82a7e
commit 4c6ba9c89e
1 changed files with 29 additions and 25 deletions

View File

@ -1,6 +1,6 @@
#!/usr/bin/env ruby #!/usr/bin/env ruby
require 'sinatra' require 'sinatra/base'
require 'sinatra/content_for' require 'sinatra/content_for'
require 'sinatra/json' require 'sinatra/json'
@ -9,32 +9,36 @@ require 'coffee-script'
require 'slim' require 'slim'
require 'sass' 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.css') { scss :application }
get('/application.js') { coffee :application } get('/application.js') { coffee :application }
get('/') { slim :index, locals: {games: GAMES} } get('/') { slim :index, locals: {games: GAMES} }
GAMES.each do |game| GAMES.each do |game|
get("/#{game}") { slim game } get("/#{game}") { slim game }
get("/#{game}.js") { coffee game } get("/#{game}.js") { coffee game }
end end
get "/data/:game.json" do |game| get "/data/:game.json" do |game|
data_file = Pathname.new(File.join(settings.data_folder, game)) data_file = Pathname.new(File.join(settings.data_folder, game))
halt(404) unless data_file.directory? halt(404) unless data_file.directory?
json data_file.children(false).select { |path| path.to_s.end_with?('.yml') } json data_file.children(false).select { |path| path.to_s.end_with?('.yml') }
.map { |path| path.to_s.sub(/.yml$/, '') } .map { |path| path.to_s.sub(/.yml$/, '') }
end end
get "/data/:game/:file.json" do |game, file| get "/data/:game/:file.json" do |game, file|
data_file = File.expand_path(File.join(settings.data_folder, game, "#{file}.yml")) data_file = File.expand_path(File.join(settings.data_folder, game, "#{file}.yml"))
halt(404) unless File.exist?(data_file) halt(404) unless File.exist?(data_file)
data = YAML.load_file(data_file)['data'] data = YAML.load_file(data_file)['data']
json Array(data).sample json Array(data).sample
end
run! if app_file == $0
end end