From 8bb5989fc5a77cd86573a7bda51896fd147f3312 Mon Sep 17 00:00:00 2001 From: Nat Lasseter Date: Tue, 24 Jun 2025 21:43:00 +0100 Subject: [bins] add bins page --- cgi/bins.cgi | 96 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ cgi/home.cgi | 93 ---------------------------------------------------------- cgi/user.cgi | 3 ++ 3 files changed, 99 insertions(+), 93 deletions(-) create mode 100644 cgi/bins.cgi (limited to 'cgi') diff --git a/cgi/bins.cgi b/cgi/bins.cgi new file mode 100644 index 0000000..018ed1e --- /dev/null +++ b/cgi/bins.cgi @@ -0,0 +1,96 @@ +require "date" +require "json" +require "net/http" +require "time" + +def next_collection + uprn = File.read("../uprn").strip + uri = URI("https://waste-api.york.gov.uk/api/Collections/GetBinCollectionDataForUprn/#{uprn}") + resp = JSON.parse(Net::HTTP.get(uri))["services"].select { |c| Time.parse(c["nextCollection"]) > Time.now }.sort { |a, b| Time.parse(a["nextCollection"]) <=> Time.parse(b["nextCollection"]) } + + words_collection(resp) + + graphic_collection(resp) +end + +def words_collection(resp) + out = "" + resp.each { |coll| + cdate = Time.parse(coll["nextCollection"]) + days = ((cdate - Time.now) / (60*60*24)).floor + + dayswords = case days + when 0; "today" + when 1; "tomorrow" + else "in #{days} days" + end + + dateword = Date::DAYNAMES[cdate.wday] + dateord = case cdate.day + when 1,21,31; "st" + when 2,22; "nd" + when 3,23; "rd" + else "th" + end + + tout = "" + tout += "Then " unless out.empty? + tout += case coll["service"] + when "REFUSE"; $cgi.span(class: "waste-refuse") { "General Domestic Waste" } + when "RECYCLING"; $cgi.span(class: "waste-recycling") { "Recycling" } + when "GARDEN"; $cgi.span(class: "waste-garden") { "Garden Waste" } + end + tout += " needs to go out #{dayswords} for collection" if out.empty? + tout += " on #{dateword} the #{cdate.day}#{dateord}." + tout += $cgi.br + out += tout + } + $cgi.p { out } +end + +def graphic_collection(resp) + events = Array.new(30) { "empty" } + + resp.each { |coll| + days = ((Time.parse(coll["nextCollection"]) - Time.now) / (60*60*24)).ceil + events[days*2] = coll["service"].downcase + events[days*2 - 1] = coll["service"].downcase + } + + $cgi.table(class: "waste-table") { + $cgi.tr { + (Time.now.hour > 12 ? $cgi.td : "") + + $cgi.td(class: "arrow") { "⇓" } + } + + $cgi.tr { + tout = "" + (0..29).step(2).each { |i| + tout += $cgi.td(class: "waste-#{events[i]} morning") + tout += $cgi.td(class: "waste-#{events[i+1]} evening") + } + tout + } + + $cgi.tr { + tout = "" + wdays = %w(Su Mo Tu We Th Fr Sa) + (0..14).each { |i| + tout += $cgi.td(colspan: 2) { wdays[(Time.now + (i * 60*60*24)).wday] } + } + tout + } + + $cgi.tr { + tout = "" + (0..14).each { |i| + tout += $cgi.td(colspan: 2) { (Time.now + (i * 60*60*24)).day } + } + tout + } + } +end + +$page = + $cgi.div { + $cgi.h1 { "Our Next Bin Collections" } + + next_collection + } + +# vim: set filetype=ruby: diff --git a/cgi/home.cgi b/cgi/home.cgi index f5ad153..b8caad9 100644 --- a/cgi/home.cgi +++ b/cgi/home.cgi @@ -1,98 +1,5 @@ -require "date" -require "json" -require "net/http" -require "time" - -def next_collection - uprn = File.read("../uprn").strip - uri = URI("https://waste-api.york.gov.uk/api/Collections/GetBinCollectionDataForUprn/#{uprn}") - resp = JSON.parse(Net::HTTP.get(uri))["services"].select { |c| Time.parse(c["nextCollection"]) > Time.now }.sort { |a, b| Time.parse(a["nextCollection"]) <=> Time.parse(b["nextCollection"]) } - - words_collection(resp) + - graphic_collection(resp) -end - -def words_collection(resp) - out = "" - resp.each { |coll| - cdate = Time.parse(coll["nextCollection"]) - days = ((cdate - Time.now) / (60*60*24)).floor - - dayswords = case days - when 0; "today" - when 1; "tomorrow" - else "in #{days} days" - end - - dateword = Date::DAYNAMES[cdate.wday] - dateord = case cdate.day - when 1,21,31; "st" - when 2,22; "nd" - when 3,23; "rd" - else "th" - end - - tout = "" - tout += "Then " unless out.empty? - tout += case coll["service"] - when "REFUSE"; $cgi.span(class: "waste-refuse") { "General Domestic Waste" } - when "RECYCLING"; $cgi.span(class: "waste-recycling") { "Recycling" } - when "GARDEN"; $cgi.span(class: "waste-garden") { "Garden Waste" } - end - tout += " needs to go out #{dayswords} for collection" if out.empty? - tout += " on #{dateword} the #{cdate.day}#{dateord}." - tout += $cgi.br - out += tout - } - $cgi.p { out } -end - -def graphic_collection(resp) - events = Array.new(30) { "empty" } - - resp.each { |coll| - days = ((Time.parse(coll["nextCollection"]) - Time.now) / (60*60*24)).ceil - events[days*2] = coll["service"].downcase - events[days*2 - 1] = coll["service"].downcase - } - - $cgi.table(class: "waste-table") { - $cgi.tr { - (Time.now.hour > 12 ? $cgi.td : "") + - $cgi.td(class: "arrow") { "⇓" } - } + - $cgi.tr { - tout = "" - (0..29).step(2).each { |i| - tout += $cgi.td(class: "waste-#{events[i]} morning") - tout += $cgi.td(class: "waste-#{events[i+1]} evening") - } - tout - } + - $cgi.tr { - tout = "" - wdays = %w(Su Mo Tu We Th Fr Sa) - (0..14).each { |i| - tout += $cgi.td(colspan: 2) { wdays[(Time.now + (i * 60*60*24)).wday] } - } - tout - } + - $cgi.tr { - tout = "" - (0..14).each { |i| - tout += $cgi.td(colspan: 2) { (Time.now + (i * 60*60*24)).day } - } - tout - } - } -end - $page = $cgi.div { $cgi.h1 { "Welcome!" } } + - $cgi.div { - $cgi.h2 { "Our Next Bin Collections" } + - next_collection - } + $cgi.div { $cgi.h2 { "Speak Gently Over the Telephone" } + $cgi.blockquote { diff --git a/cgi/user.cgi b/cgi/user.cgi index d6e55d9..1357d65 100755 --- a/cgi/user.cgi +++ b/cgi/user.cgi @@ -14,6 +14,7 @@ def toplinks $cgi.table(class: "toplinks") { $cgi.tr { $cgi.td { link("/" , "Home" ) } + + $cgi.td { link("/bins" , "Bins" ) } + $cgi.td { link("/eamit.html" , "eamit" , true) } + $cgi.td { link("https://git.4574.co.uk/", "Git" , true) } + $cgi.td { link("/stuff-i-want" , "Stuff I Want" ) } @@ -89,6 +90,8 @@ $cgi = CGI.new("html5") case $cgi.path_info when "/" page("Home", "User_4574's Lair") +when "/bins" + page("Bins") when /^\/stuff-i-want/ page("Stuff I Want") when /^\/bv/ -- cgit v1.2.3