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

Commit

Permalink
Merge pull request #48 from levups/reliable_tnt_extraction_and_tests
Browse files Browse the repository at this point in the history
Rewrite TNT class to stabilize it at any time of the month
  • Loading branch information
czj authored Apr 23, 2019
2 parents 352ac0b + 5aeff17 commit c176411
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 19 deletions.
54 changes: 41 additions & 13 deletions lib/fuel_surcharge/tnt.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,22 @@ module FuelSurcharge
class TNT
using StringFormatter

def initialize
latest_road_values_position = 0
latest_air_values_position = extracted_values.size.div(2)
@road_values = extracted_values[latest_road_values_position]
@air_values = extracted_values[latest_air_values_position]
end
attr_accessor :current_month

def url
"https://www.tnt.com/express/fr_fr/site/home/comment-expedier/facturation/surcharges/baremes-et-historiques.html"
def initialize(current_month: Date.today.month)
@current_month = current_month
end

def time_period
@road_values&.first&.to_s
road_value&.first&.to_s
end

def road_percentage
@road_values&.last&.to_s
road_value&.last&.to_s
end

def air_percentage
@air_values&.last&.to_s
air_value&.last&.to_s
end

def road_multiplier
Expand All @@ -41,6 +36,7 @@ def air_multiplier
private

VALUES_REGEX = /Surcharge d[e\' ]+(.*) : (.*%)</.freeze
# Sample obtained structure :
# [
# [" novembre 2018 ", "12,10%"],
# ["octobre 2018 ", "11,95%"],
Expand All @@ -50,11 +46,43 @@ def air_multiplier
# [" septembre 2018 ", "17,50%"]
# ]
def extracted_values
@extracted_values ||= response.to_s.scan(VALUES_REGEX)
response.to_s.scan(VALUES_REGEX)
end

def current_month_values
extracted_values.select { |month, value| month.include? current_month_name }
end

def air_value
current_month_values.last
end

def road_value
current_month_values.first
end

URL = "https://www.tnt.com/express/fr_fr/site/home/comment-expedier/facturation/surcharges/baremes-et-historiques.html"
def response
@response ||= HTTPRequest.new(url).response
@response ||= HTTPRequest.new(URL).response
end

FRENCH_MONTHS_NAMES = %w[
janvier
février
mars
avril
mai
juin
juillet
août
septembre
octobre
novembre
décembre
].freeze

def current_month_name
FRENCH_MONTHS_NAMES[current_month - 1]
end
end
end
9 changes: 3 additions & 6 deletions test/fuel_surcharge/tnt_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,13 @@ def test_road
end
end

FRENCH_MONTHS = %w[janvier février mars avril mai juin juillet août
septembre octobre novembre décembre].freeze

def test_live_values
skip if ENV["SKIP_LIVE_TESTS"]

@tnt = TNT.new

time_period = @tnt.time_period
current_month = FRENCH_MONTHS[Date.today.month - 1]
current_month = FuelSurcharge::TNT::FRENCH_MONTHS_NAMES[Date.today.month - 1]

assert_kind_of String, time_period
assert time_period.downcase.start_with?(current_month)
Expand All @@ -69,14 +66,14 @@ def test_live_values
def nominal_case
sample_response = File.read("test/fixtures/tnt_sample_response.html")
HTTPRequest.stub_any_instance :response, sample_response do
@tnt = TNT.new
@tnt = TNT.new(current_month: 11)
yield
end
end

def failing_case
HTTPRequest.stub_any_instance :response, "" do
@tnt = TNT.new
@tnt = TNT.new(current_month: 11)
yield
end
end
Expand Down

0 comments on commit c176411

Please sign in to comment.