Sha256: 69aa63fe98b8ce251488971c8f45f3cdb3a160ae96c4b8287229ce6d3f8d68d7

Contents?: true

Size: 1.23 KB

Versions: 2

Compression:

Stored size: 1.23 KB

Contents

require 'hermes/scraper/version'

require 'pp'
require 'mechanize'
require 'date'

module Hermes
  class Scraper
    def initialize(barcode)
      @page = Mechanize.new.get(url(barcode))
    end

    def scrape
      rows = []
      tracking_text_rows.each do |tracking_text_row|
        elements = tracking_text_elements(tracking_text_row)
        validate(elements)
        rows << parse_row(elements)
      end
      rows
    end

    private

    def tracking_text_elements(tracking_text_row)
      tracking_text_row.search('td/text()').map do |text_element|
        text_element.text.tr(' ', ' ').strip # Unicode - beware!
      end
    end

    def validate(elements)
      raise 'Hermes tracking page format appers to have changed 8-(' unless elements.size == 3
    end

    def tracking_text_rows
      @page.search("//tr[@class='trackingText']")
    end

    def parse_row(elements)
      {
        datetime: parse_datetime(elements[0], elements[1]),
        status: elements[2]
      }
    end

    def parse_datetime(date, time)
      DateTime.strptime("#{date} #{time}", '%d/%m/%Y %H:%M')
    end

    def url(barcode)
      "http://www.hermes-europe.co.uk/customerparceltrackingservice/trackingDetailsHermes.jsp?barcode=#{barcode}"
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
hermes-scraper-0.0.6 lib/hermes/scraper.rb
hermes-scraper-0.0.5 lib/hermes/scraper.rb