Module: Scraper

Defined in:
lib/check_late/scraper.rb

Constant Summary collapse

URL =
"http://mobile.viaggiatreno.it/viaggiatreno/mobile/numero/"
SEARCH_TYPE =
"numero"
LANG =
"IT"

Instance Method Summary collapse

Instance Method Details

#generate_request(train_number) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/check_late/scraper.rb', line 9

def generate_request(train_number)
  agent = Mechanize.new
  response = agent.post( URL,
                         {
                           :numeroTreno => train_number,
                           :tipoRicerca => SEARCH_TYPE,
                           :lang        => LANG
                         }
                         )
  return response.parser.xpath('//div[@class="evidenziato"]/strong')
end

#sanitize_text(text) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/check_late/scraper.rb', line 37

def sanitize_text(text)
  text = text.gsub(/\r|\n/, '')
  text = text.gsub(/\t+/, ' ')
  text = text.gsub(/ +/, ' ')
  text = text.gsub(/<!--.*-->/, '')
  text = text.gsub(/<br>/, '')
  text = text.gsub(/<strong>|<\/strong>/, '')
  text = text.strip
end

#scrape!(train_number) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/check_late/scraper.rb', line 21

def scrape!(train_number)
  xpath = generate_request(train_number)
  begin

    if xpath.to_a.first.nil?
      raise Exception
    else
      sanitize_text xpath.to_a.first.to_html
    end

  rescue Exception
    puts "Invalid Train Number"
  end

end