diff options
Diffstat (limited to 'cgi/bins.cgi')
-rw-r--r-- | cgi/bins.cgi | 96 |
1 files changed, 96 insertions, 0 deletions
diff --git a/cgi/bins.cgi b/cgi/bins.cgi new file mode 100644 index 0000000..fb959f1 --- /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.h2 { "Our Next Bin Collections" } + + next_collection + } + +# vim: set filetype=ruby: |