Class: RubbishCollection::RedbridgeAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/rubbish_collection/redbridge.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) ⇒ RedbridgeAdapter

Returns a new instance of RedbridgeAdapter.



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

def initialize local_authority
end

Class Method Details

.loadObject



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

def collection_times_at address
  Net::HTTP.start "www.redbridge.gov.uk", 80 do |http|
    req = Net::HTTP::Get.new '/RecycleRefuse'
    req['Cookie'] = "RedbridgeIV3LivePref=postcode=#{address.postcode}"
    response = http.request req
    doc = Nokogiri::HTML response.body
    info = doc.xpath "//*[@id='RegularCollectionDay']"
    instructions = info.xpath ".//*[@class='instructions']/text()"
    hour, modifier = instructions.to_s.strip.scan(/(\d+)(am|pm)/)[0]
    hour = hour.to_i
    hour += 12 if modifier == "pm"
    time = hour * 100
    day = info.xpath ".//*[@class='day']/text()"
    day_index = DAYS.index day.to_s.strip
    [ CollectionTime.new(day_index, time) ]
  end
end