Class: RubbishCollection::SouthwarkAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/rubbish_collection/southwark.rb

Constant Summary collapse

DAYS =
%w( Sunday Monday Tuesday Wednesday Thursday Friday Saturday ).map(&:freeze).freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(local_authority) ⇒ SouthwarkAdapter

Returns a new instance of SouthwarkAdapter.



9
10
# File 'lib/rubbish_collection/southwark.rb', line 9

def initialize local_authority
end

Class Method Details

.loadObject



5
6
7
# File 'lib/rubbish_collection/southwark.rb', line 5

def self.load
  require 'nokogiri'
end

Instance Method Details

#collection_times_at(address) ⇒ Object



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
# File 'lib/rubbish_collection/southwark.rb', line 12

def collection_times_at address
  Net::HTTP.start "wasteservices.southwark.gov.uk", 80 do |http|
    req = Net::HTTP::Get.new "/findAddress.asp?pc=#{address.postcode.gsub(/\s+/, '')}"
    response = http.request req
    addresses = Nokogiri::HTML.fragment response.body
    first_address = addresses.xpath(".//option").detect { |o| o['value'].to_s.strip != '' }
    uprn = first_address['value'].to_s
    req = Net::HTTP::Get.new "/findSummary.asp?uprn=#{uprn}"
    response = http.request req
    times = []
    response.body.split(/<br><br><br><br>/).each do |fragment|
      fragment_doc = Nokogiri::HTML.fragment fragment
      link = fragment_doc.at_xpath('.//a')
      link_text = link.inner_text.to_s.strip
      collection_type = case link_text
      when /refuse bin/
        DOMESTIC_RUBBISH
      when /communal recycling bins/
        COMMUNAL_RECYCLING
      when /mixed recycling bag/
        DOMESTIC_RECYCLING
      else
        next
      end
      DAYS.inject([]) { |a,e|
        a << e if fragment =~ /#{e}/i
        a
      }.each do |d|
        s = Schedule.new Resolution::DAY
        s.add_rule Schedule.day_of_week(d)
        c = Collection.new collection_type, s
        times << c
      end
    end
    times
  end
end