Class: RubbishCollection::RushmoorAdapter

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(local_authority) ⇒ RushmoorAdapter

Returns a new instance of RushmoorAdapter.



10
11
# File 'lib/rubbish_collection/rushmoor.rb', line 10

def initialize local_authority
end

Class Method Details

.loadObject



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

def self.load
  require 'nokogiri'
  require 'date'
end

Instance Method Details

#collection_times_at(address) ⇒ Object



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

def collection_times_at address
  Net::HTTP.start "www.rushmoor.gov.uk", 80 do |http|
    postcode = address.postcode.gsub("\s","") # Rushmoor don't like post codes with spaces
    req = Net::HTTP::Get.new "/article/1589/Address-search?housenumber=#{address.house_number}&addressdetail=#{postcode}"
    response = http.request req
    if response.code == "302"
      response = Net::HTTP.get_response(URI.parse(response.header['location']))
    end
    doc = Nokogiri::HTML response.body
    info = doc.xpath("//*[@id='inmyarea']")
    container = info.xpath("//*[@class='ima_block']").first
    times = []
    container.xpath("p").each do |rubbish_div|
      lines = rubbish_div.text.split("\n")
      day_name = lines[2].to_s.strip.split(" ")[0]
      s = Schedule.new Resolution::DAY
      collection = case lines[1]
      when /rubbish bin collection/
        s.add_rule Schedule.day_of_week day_name
        Collection.new DOMESTIC_RUBBISH, s
      when /recycling collection/
        nc = Time.parse lines[2]
        start_date = Runt::PDate.day nc.year, nc.month, nc.day
        s.add_rule Runt::EveryTE.new(start_date, 2, Runt::DPrecision::WEEK)
        s.add_rule Schedule.day_of_week day_name
        Collection.new DOMESTIC_RECYCLING, s
      else
        next
      end
      times << collection
    end
    times
  end
end