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

Commit

Permalink
Merge pull request #13 from levups/prepare_for_v0.2.0
Browse files Browse the repository at this point in the history
Replace httparty with HTTP.rb, unify code and add tests
  • Loading branch information
bobmaerten authored Nov 14, 2018
2 parents e04b6c5 + 89b4a79 commit 7393b32
Show file tree
Hide file tree
Showing 16 changed files with 986 additions and 87 deletions.
1 change: 0 additions & 1 deletion .github_changelog_generator

This file was deleted.

1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
*.gem
/.github_changelog_generator
2 changes: 2 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Style/Documentation:
Enabled: false
6 changes: 6 additions & 0 deletions .travis.yml
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
11 changes: 5 additions & 6 deletions Gemfile
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
51 changes: 37 additions & 14 deletions Gemfile.lock
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
11 changes: 11 additions & 0 deletions Rakefile
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
11 changes: 11 additions & 0 deletions bin/chronopost_fuel_adjustment_coefficients
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}"
9 changes: 0 additions & 9 deletions bin/chronopost_fuel_multiplier

This file was deleted.

22 changes: 22 additions & 0 deletions chronopost_fuel_adjustment_coefficients.gemspec
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
19 changes: 0 additions & 19 deletions chronopost_fuel_multiplier.gemspec

This file was deleted.

49 changes: 49 additions & 0 deletions lib/chronopost_fuel_adjustment_coefficients.rb
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
30 changes: 0 additions & 30 deletions lib/chronopost_fuel_multiplier.rb

This file was deleted.

8 changes: 0 additions & 8 deletions lib/plain_http_client.rb

This file was deleted.

54 changes: 54 additions & 0 deletions test/chronopost_fuel_adjustment_coefficients_test.rb
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
Loading

0 comments on commit 7393b32

Please sign in to comment.