aboutsummaryrefslogtreecommitdiff
path: root/cgi/bins.cgi
blob: fb959f17a8d12c8d349d92c8382c3bef595b85ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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") { "&#x21d3;" }
    } +
    $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: