Class: Traffic::Scraper

Inherits:
Object
  • Object
show all
Defined in:
lib/traffic-scraper.rb,
lib/traffic-scraper/version.rb

Constant Summary collapse

TRAFFIC_PAGE_URL =
"http://www.cetsp.com.br/"
VERSION =
"0.0.2"

Class Method Summary collapse

Class Method Details

.overall_trafficInteger

Returns the amount of overall traffic we currently have

Returns:

  • (Integer)

    the amount of traffic



13
14
15
16
17
# File 'lib/traffic-scraper.rb', line 13

def overall_traffic
  zone_traffic.values.inject(:+)
rescue
  raise CouldNotRetrievePageError
end

.zone_trafficHash

Returns the amount of overall traffic we currently have

Returns:

  • (Hash)

    containing all the traffic information for all zones



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/traffic-scraper.rb', line 22

def zone_traffic
  page = Nokogiri::HTML(open(TRAFFIC_PAGE_URL))

  result_hash = {}

  { "norte" => :north ,
    "sul" => :south,
    "leste" => :east,
    "oeste" => :west,
    "centro" => :downtown
  }.each do |zone, zone_translation|
    match = /^(\d+)\s*km$/.match(page.css(".info.#{zone} h4").inner_html)

    raise CouldNotRetrievePageError unless match
    result_hash[zone_translation] = match[1].to_i
  end

  result_hash
rescue
  raise CouldNotRetrievePageError
end