35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
# File 'lib/pactrac/carrier/dhl.rb', line 35
def parse_tracking_data(response)
doc = Nokogiri::HTML(response.raw.body)
table = doc.css('.clpt_tracking_results table').first
if table.nil?
return Err.new(false, 'unable to find tracking data table')
end
tracking_data = { :updates => [] }
origin_node = table.css('#orginURL4').first
if origin_node.nil?
return Err.new(false, 'unable to find origin')
end
tracking_data[:origin] = origin_node.content
destination_node = table.css('#destinationURL4').first
if destination_node.nil?
return Err.new(false, 'unable to find destination')
end
tracking_data[:destination] = destination_node.content
current_date = nil
table.children.each do |section|
next unless ['thead', 'tbody'].include?(section.name.to_s)
next if section.attribute('class').to_s == 'tophead'
section.css('tr').each do |row|
cells = row.css('td, th')
first_cell = cells.first
case
when first_cell.name == 'th' if first_cell.content.empty?
current_date = Date.today
else
current_date = Date.parse(first_cell.content)
end
when first_cell.attribute('class').to_s != 'emptyRow'
next if current_date.nil?
tracking_data[:updates] << {
:message => cells[1].content.strip,
:location => cells[2].content.strip,
:at => DateTime.parse(
"#{current_date.to_s}T#{cells[3].content.to_s}"),
}
end
end
end
return Err.new(true), tracking_data
end
|