Websites/app/models/e621_status.rb

96 lines
2.1 KiB
Ruby
Raw Normal View History

2024-05-03 03:04:43 +00:00
# frozen_string_literal: true
class E621Status < ApplicationRecord
NOTES = {
0 => "Some internal issue happened while contacting e621.",
1 => "E621 is currently in maintenance mode.",
403 => "E621 is likely experiencing some kind of attack right now, so api endpoints may be returning challenges.",
}.freeze
STATES = {
0 => "error",
1 => "maintenance",
403 => "partially down",
}.freeze
STATUS_MESSAGES = {
0 => "Internal Error",
1 => "Maintenance",
2 => "No Status",
200 => "OK",
204 => "No Content",
400 => "Bad Request",
401 => "Unauthorized",
403 => "Forbidden",
404 => "Not Found",
405 => "Method Not Allowed",
406 => "Not Acceptable",
408 => "Request Timeout",
410 => "Gone",
429 => "Too Many Requests",
500 => "Internal Server Error",
502 => "Bad Gateway",
503 => "Service Unavailable",
504 => "Gateway Timeout",
520 => "Unknown Cloudflare Error",
521 => "Web Server Is Down",
522 => "Connection Timed Out",
523 => "Origin Is Unreachable",
524 => "A Timeout Occurred",
525 => "SSL Handshake Failed",
526 => "Invalid SSL Certificate",
527 => "Railgun Error",
530 => "Site Is Frozen",
}.freeze
after_create :send_notifications
def available
status >= 200 && status <= 299
end
def state
(STATES[status] || available ? "up" : "down").gsub(/ /, "-")
end
def status_message
STATUS_MESSAGES[status] || "Unknown (#{status})"
end
def note
NOTES[status]
end
module ApiMethods
def method_attributes
super + %i[available state status_message note]
end
end
module SearchMethods
def current
order(id: :desc).first || new(status: 2, created_at: Time.now)
end
def history(limit = 100)
order(id: :desc).limit(limit)
end
def combined(limit = 100)
{
current: current,
history: history(limit)[1..],
}
end
end
include ApiMethods
extend SearchMethods
def send_notifications
E621Webhook.find_each do |webhook|
webhook.send_update(self)
end
end
end