class ChronopostFuelAdjustmentCoefficients

Attributes

time_period[R]

Public Class Methods

new() click to toggle source
# File lib/chronopost_fuel_adjustment_coefficients.rb, line 9
def initialize
  @time_period, @routier, @aerien = extracted_content
end

Public Instance Methods

air_multiplier() click to toggle source
# File lib/chronopost_fuel_adjustment_coefficients.rb, line 13
def air_multiplier
  format_multiplier @aerien
end
road_multiplier() click to toggle source
# File lib/chronopost_fuel_adjustment_coefficients.rb, line 17
def road_multiplier
  format_multiplier @routier
end
url() click to toggle source
# File lib/chronopost_fuel_adjustment_coefficients.rb, line 21
def url
  'https://www.chronopost.fr/fr/surcharge-carburant'
end

Private Instance Methods

extracted_content() click to toggle source
# File lib/chronopost_fuel_adjustment_coefficients.rb, line 33
def extracted_content
  html = response.to_s.delete("\n").gsub(/\s+/, " ")

  return [] unless tables = html.split("<table ")[1]
  return [] unless table  = tables.split("<h2>Principe</h2>").first
  return [] unless head   = table.scan(%r{<thead>.*</thead>}).first
  return [] unless period = head.match(%r{<p>(?<content>.*)</p>})
  return [] unless body   = table.scan(%r{<tbody><tr>.*</tr></tbody>}).first

  time_period  = period[:content].sub("<br />", "")
  adjustements = body.split("</tr><tr>").map do |line|
    line.to_s.rpartition("</td>").first.rpartition(">").last
  end

  [time_period, adjustements[0], adjustements[1]]
end
format_multiplier(string) click to toggle source
# File lib/chronopost_fuel_adjustment_coefficients.rb, line 50
def format_multiplier(string)
  return unless string

  number = (string.to_f / 100 + 1).round(4)
  BigDecimal(number.to_s)
end
response() click to toggle source
# File lib/chronopost_fuel_adjustment_coefficients.rb, line 27
def response
  ::HTTP.timeout(10).get(url)
rescue HTTP::Error
  ''
end