rubocop and improved lang support

This commit is contained in:
Ole-Morten Duesund 2025-05-24 20:27:14 +02:00
commit 4c33688b19
7 changed files with 492 additions and 452 deletions

81
app.rb
View file

@ -1,3 +1,4 @@
# frozen_string_literal: true
require 'sinatra'
require 'json'
@ -7,43 +8,43 @@ require 'time'
use Rack::Accept
NO_RESPONSES = {
"en" => [
"No.", "Nope.", "Nah.", "Nuh-uh.", "Negative.", "Nay.",
"Hell no.", "Not in a million years.", "Get lost.",
"Not happening, buddy.", "Why don't you try asking someone else?",
"Are you kidding me?", "Absolutely not, go away.",
"Im terribly sorry, but no.", "I regret to inform you that I cannot comply.",
"Unfortunately, that will not be possible at this time.",
"I must respectfully decline your request.", "I wish I could, but sadly I cannot.",
"It would be my pleasure to help, but I must regretfully decline.",
"With the greatest respect, I must say no.",
"I apologize, but I must kindly refuse your request."
'en' => [
'No.', 'Nope.', 'Nah.', 'Nuh-uh.', 'Negative.', 'Nay.',
'Hell no.', 'Not in a million years.', 'Get lost.',
'Not happening, buddy.', "Why don't you try asking someone else?",
'Are you kidding me?', 'Absolutely not, go away.',
'Im terribly sorry, but no.', 'I regret to inform you that I cannot comply.',
'Unfortunately, that will not be possible at this time.',
'I must respectfully decline your request.', 'I wish I could, but sadly I cannot.',
'It would be my pleasure to help, but I must regretfully decline.',
'With the greatest respect, I must say no.',
'I apologize, but I must kindly refuse your request.'
],
"de" => ["Nein.", "Keineswegs."],
"fr" => ["Non.", "Jamais."],
"ja" => ["いいえ", "ダメです。"],
"ru" => ["Нет", "Никак нет"],
"pt" => ["Não.", "De jeito nenhum."],
"es" => ["No.", "Para nada."],
"pl" => ["Nie.", "W żadnym wypadku."],
"fi" => ["Ei.", "Ei todellakaan."],
"tr" => ["Hayır.", "Bu değil."],
"he" => ["לא"],
"hi" => ["नहीं", "बिलकुल नहीं"],
"no" => ["Nei.", "Absolutt ikke.", "Ikke tale om."],
"sv" => ["Nej.", "Inte alls."],
"da" => ["Nej.", "Ikke en chance."],
"it" => ["No.", "Assolutamente no."],
"nl" => ["Nee.", "Absoluut niet."],
"cs" => ["Ne.", "Rozhodně ne."],
"ko" => ["아니요.", "절대 안 돼요."],
"zh" => ["不。", "绝对不行。"]
}
'de' => ['Nein.', 'Keineswegs.'],
'fr' => ['Non.', 'Jamais.'],
'ja' => ['いいえ', 'ダメです。'],
'ru' => ['Нет', 'Никак нет'],
'pt' => ['Não.', 'De jeito nenhum.'],
'es' => ['No.', 'Para nada.'],
'pl' => ['Nie.', 'W żadnym wypadku.'],
'fi' => ['Ei.', 'Ei todellakaan.'],
'tr' => ['Hayır.', 'Bu değil.'],
'he' => ['לא'],
'hi' => ['नहीं', 'बिलकुल नहीं'],
'no' => ['Nei.', 'Absolutt ikke.', 'Ikke tale om.'],
'sv' => ['Nej.', 'Inte alls.'],
'da' => ['Nej.', 'Ikke en chance.'],
'it' => ['No.', 'Assolutamente no.'],
'nl' => ['Nee.', 'Absoluut niet.'],
'cs' => ['Ne.', 'Rozhodně ne.'],
'ko' => ['아니요.', '절대 안 돼요.'],
'zh' => ['不。', '绝对不行。']
}.freeze
RATE_LIMIT = {
window: 60, # seconds
limit: 60 # requests per IP per window
}
}.freeze
$requests = {}
@ -53,8 +54,8 @@ helpers do
end
def log_to_journald(message)
IO.popen(["/usr/bin/systemd-cat", "--identifier=noaas"], "w") { |io| io.puts message }
rescue
IO.popen(['/usr/bin/systemd-cat', '--identifier=noaas'], 'w') { |io| io.puts message }
rescue StandardError
puts message # fallback to stdout
end
@ -87,9 +88,9 @@ helpers do
end
def preferred_language
params["lang"] || request.env["HTTP_ACCEPT_LANGUAGE"].to_s.scan(/[a-z]{2}/).find do |lang|
NO_RESPONSES.key?(lang)
end || "en"
[params['lang'], request.env['HTTP_ACCEPT_LANGUAGE'].to_s.scan(/[a-z]{2}/).first, 'en'].each do |lang|
return lang if NO_RESPONSES.key?(lang)
end || 'en'
end
def no_message(lang)
@ -110,9 +111,9 @@ end
get '/' do
if rate_limited?
response = [ { status: 402, message: "I'm not paid enough for this." },
{ status: 418, message: "I'm just a teapot."},
{ status: 429, message: "Not so fast!"} ].sample
response = [{ status: 402, message: "I'm not paid enough for this." },
{ status: 418, message: "I'm just a teapot." },
{ status: 429, message: 'Not so fast!' }].sample
status response[:status]
content_type :json