This repository has been archived by the owner on Jan 4, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from levups/prepare_for_v0.2.0
Replace httparty with HTTP.rb, unify code and add tests
- Loading branch information
Showing
16 changed files
with
986 additions
and
87 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
*.gem | ||
/.github_changelog_generator |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Style/Documentation: | ||
Enabled: false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
language: ruby | ||
rvm: | ||
- 2.3 | ||
- 2.4 | ||
- 2.5 | ||
- 2.6 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
# This syntax restricts all gems' source to this secure one | ||
source 'https://rubygems.org' do | ||
gem 'addressable' # Replacement for standard ruby URI implementation, conforms slightly better to RFCs and international URIs | ||
gem 'httparty' | ||
gem 'nokogiri', '~> 1.8.5' | ||
end | ||
# frozen_string_literal: true | ||
|
||
source 'https://rubygems.org' | ||
|
||
gemspec |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,50 @@ | ||
PATH | ||
remote: . | ||
specs: | ||
chronopost_fuel_adjustment_coefficients (0.2.0) | ||
http (~> 4.0) | ||
oga (~> 2.10) | ||
|
||
GEM | ||
remote: https://rubygems.org/ | ||
specs: | ||
addressable (2.5.2) | ||
public_suffix (>= 2.0.2, < 4.0) | ||
httparty (0.16.3) | ||
mime-types (~> 3.0) | ||
multi_xml (>= 0.5.2) | ||
mime-types (3.2.2) | ||
mime-types-data (~> 3.2015) | ||
mime-types-data (3.2018.0812) | ||
mini_portile2 (2.3.0) | ||
multi_xml (0.6.0) | ||
nokogiri (1.8.5) | ||
mini_portile2 (~> 2.3.0) | ||
ansi (1.5.0) | ||
ast (2.4.0) | ||
domain_name (0.5.20180417) | ||
unf (>= 0.0.5, < 1.0.0) | ||
http (4.0.0) | ||
addressable (~> 2.3) | ||
http-cookie (~> 1.0) | ||
http-form_data (~> 2.0) | ||
http_parser.rb (~> 0.6.0) | ||
http-cookie (1.0.3) | ||
domain_name (~> 0.5) | ||
http-form_data (2.1.1) | ||
http_parser.rb (0.6.0) | ||
minitest (5.11.3) | ||
minitest-stub_any_instance (1.0.2) | ||
oga (2.15) | ||
ast | ||
ruby-ll (~> 2.1) | ||
public_suffix (3.0.3) | ||
rake (12.3.1) | ||
ruby-ll (2.1.2) | ||
ansi | ||
ast | ||
unf (0.1.4) | ||
unf_ext | ||
unf_ext (0.0.7.5) | ||
|
||
PLATFORMS | ||
ruby | ||
|
||
DEPENDENCIES | ||
addressable! | ||
httparty! | ||
nokogiri (~> 1.8.5)! | ||
chronopost_fuel_adjustment_coefficients! | ||
minitest (~> 5.1) | ||
minitest-stub_any_instance (~> 1.0) | ||
rake (~> 12.3) | ||
|
||
BUNDLED WITH | ||
1.16.2 | ||
1.17.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rake/testtask' | ||
|
||
Rake::TestTask.new do |t| | ||
t.libs << 'test' | ||
t.pattern = 'test/*_test.rb' | ||
t.verbose = true | ||
end | ||
|
||
task default: :test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#!/usr/bin/env ruby | ||
# frozen_string_literal: true | ||
|
||
require 'chronopost_fuel_adjustment_coefficients' | ||
|
||
ch = ChronopostFuelAdjustmentCoefficients.new | ||
puts "# Fuel multipliers for #{ch.time_period}" | ||
puts "# Fetched from #{ch.url}" | ||
puts | ||
puts "AIR_FUEL_MULTIPLIER = #{format '%.4f', ch.air_multiplier}" | ||
puts "ROAD_FUEL_MULTIPLIER = #{format '%.4f', ch.road_multiplier}" |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
Gem::Specification.new do |s| | ||
s.name = 'chronopost_fuel_adjustment_coefficients' | ||
s.version = '0.2.0' | ||
s.date = '2018-11-14' | ||
s.summary = 'Retrieve current month\'s Chronopost\'s fuel surcharges' | ||
s.description = 'A simple gem to ease/automate getting this data every month.' | ||
s.authors = ['Bob Maerten', 'Clément Joubert'] | ||
s.files = Dir['lib/*.rb'] + Dir['bin/*'] | ||
s.email = ['[email protected]', '[email protected]'] | ||
s.homepage = 'https://rubygems.org/gems/chronopost_fuel_adjustment_coefficients' | ||
s.license = 'MIT' | ||
|
||
s.bindir = 'bin' | ||
s.executables = 'chronopost_fuel_adjustment_coefficients' | ||
|
||
s.add_dependency 'http', '~> 4.0' | ||
s.add_dependency 'oga', '~> 2.10' | ||
|
||
s.add_development_dependency 'minitest', '~> 5.1' | ||
s.add_development_dependency 'minitest-stub_any_instance', '~> 1.0' | ||
s.add_development_dependency 'rake', '~> 12.3' | ||
end |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'bigdecimal' | ||
require 'http' | ||
require 'oga' | ||
|
||
class ChronopostFuelAdjustmentCoefficients | ||
def initialize | ||
@document = ::Oga.parse_html response.to_s | ||
end | ||
|
||
def time_period | ||
el = @document.at_css('table.ch-table thead tr th:last-child') | ||
return unless el | ||
|
||
el.text.strip.gsub(/\s+/, ' ') | ||
end | ||
|
||
def air_multiplier | ||
el = @document.at_css('table.ch-table tbody tr:last-child td:last-child') | ||
return unless el | ||
|
||
format_multiplier el.text | ||
end | ||
|
||
def road_multiplier | ||
el = @document.at_css('table.ch-table tbody tr td:last-child') | ||
return unless el | ||
|
||
format_multiplier el.text | ||
end | ||
|
||
def url | ||
'https://www.chronopost.fr/fr/surcharge-carburant' | ||
end | ||
|
||
private | ||
|
||
def response | ||
::HTTP.timeout(10).get(url) | ||
rescue HTTP::Error | ||
'' | ||
end | ||
|
||
def format_multiplier(string) | ||
number = (string.to_f / 100 + 1).round(4) | ||
BigDecimal(number.to_s) | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'minitest/autorun' | ||
require 'minitest/stub_any_instance' | ||
require 'chronopost_fuel_adjustment_coefficients' | ||
|
||
class ChronopostFuelAdjustmentCoefficientsTest < Minitest::Test | ||
def test_time_period | ||
nominal_case do | ||
assert_equal 'Novembre 2018', @adjustement_coefficient.time_period | ||
end | ||
|
||
failing_case do | ||
assert_nil @adjustement_coefficient.time_period | ||
end | ||
end | ||
|
||
def test_air_multiplier | ||
nominal_case do | ||
assert_equal 1.2000, @adjustement_coefficient.air_multiplier | ||
end | ||
|
||
failing_case do | ||
assert_nil @adjustement_coefficient.air_multiplier | ||
end | ||
end | ||
|
||
def test_road_multiplier | ||
nominal_case do | ||
assert_equal 1.1400, @adjustement_coefficient.road_multiplier | ||
end | ||
|
||
failing_case do | ||
assert_nil @adjustement_coefficient.road_multiplier | ||
end | ||
end | ||
|
||
private | ||
|
||
def nominal_case | ||
res = File.read('test/sample_response.html') | ||
ChronopostFuelAdjustmentCoefficients.stub_any_instance :response, res do | ||
@adjustement_coefficient = ChronopostFuelAdjustmentCoefficients.new | ||
yield | ||
end | ||
end | ||
|
||
def failing_case | ||
ChronopostFuelAdjustmentCoefficients.stub_any_instance :response, '' do | ||
@adjustement_coefficient = ChronopostFuelAdjustmentCoefficients.new | ||
yield | ||
end | ||
end | ||
end |
Oops, something went wrong.