Skip to content
This repository has been archived by the owner on Jan 2, 2020. It is now read-only.

Fix chronopost time period extract #27

Merged
merged 2 commits into from
Feb 8, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions lib/fuel_surcharge/chronopost.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ def url
private

def source_html
HTTPRequest.new(url)
.response
.to_s
.delete("\n")
.gsub(/\s+/, " ")
@source_html ||= HTTPRequest.new(url)
.response
.to_s
.delete("\n")
.gsub(/\s+/, " ")
end

def tables
Expand All @@ -56,7 +56,8 @@ def head
def period
return unless head

head.match(%r{<p>(?<content>.*)</p>})
text = head.split(%r{</th>\s*<th}).last
text.match(%r{<p>(?<content>.*)</p>})
end

def body
Expand All @@ -69,8 +70,8 @@ def extract_content
return unless period
return unless body

@time_period = period[:content].sub("<br />", "")
@road_percentage, @air_percentage = body.split("</tr><tr>").map do |line|
@time_period = period[:content].sub(%r{<br\s*/>}, "")
@road_percentage, @air_percentage = body.split(%r{</tr>\s*<tr}).map do |line|
line.to_s.rpartition("</td>").first.rpartition(">").last
end
end
Expand Down