Fuel Prices Europe
This Ruby gem scrapes the average European fuel prices from fuel-prices-europe.info.
The following data can be found for each country:
- Price of unleaded 95 RON in euros (and in local currency if available)
- Price of diesel in euros (and in local currency if available)
- Price of LPG in euros (and in local currency if available)
- LPG nozzle type
- Number of LPG stations
- Recording date of the data
See Usage for all functions.
Installation
Add this line to your application's Gemfile:
gem 'fuel_prices_europe'
And then execute:
$ bundle
Or install it yourself as:
$ gem install fuel_prices_europe
Configuration
A configuration is optional, however I strongly recommend using the cache option to avoid making too much requests to the source website.
Example configuration with explanation in the comments:
FuelPricesEurope.configure do |config|
config.cache = Rails.cache
config.cache_expires_in = 24.hours
end
If you are using Rails, add the configuration to a file (fuel_prices_europe.rb
) in your config/initializers
folder.
Usage
Find data by country
You can find by country. There are three methods you can use:
- Find by alpha2 country code:
find_by_country_code(country_code)
(alias for: find_by_country_alpha2
) - Find by alpha3 country code:
find_by_country_alpha3(country_code)
- Find by country name:
find_by_country_name(country_name)
Important: If possible, use the method 1 for the best performance.
Methods 2 and 3 will convert the input argument to an alpha2 code using the Countries gem.
Methods 2 and 3 should be used if you do not have the alpha2 country code.
All the above methods will return a CountryData
object. The CountryData
methods are shown in the examples below.
Examples
Example #1: Uses a country which has all data except local prices.
fuel_prices = FuelPricesEurope::FuelPrices.new
country_data = fuel_prices.find_by_country_code('NL')
country_data.country
country_data.record_date
country_data.lpg_stations
country_data.lpg_nozzle
country_data.lpg_price
country_data.lpg_price_local
country_data.diesel_price
country_data.diesel_price_local
country_data.unleaded_95_ron_price
country_data.unleaded_95_ron_price_local
Example #2: This country has local prices:
fuel_prices = FuelPricesEurope::FuelPrices.new
country_data = fuel_prices.find_by_country_name('Lebanon')
country_data.diesel_price_local
country_data.lpg_price_local
country_data.unleaded_95_ron_price_local
country_data.lpg_stations
country_data.lpg_nozzle
The country
method will return a Country object. This Country object comes from the Countries gem.
Find data by multiple countries
You can use the find_by_countries(countries, type = :alpha2)
method to find for multiple countries.
By default you have to pass in an array of alpha2 country codes. Using alpha2 codes is recommended.
But you can also by specifying a type with the second argument, valid types are :alpha2
, :alpha3
and :name
An example:
fuel_prices = FuelPricesEurope::FuelPrices.new
countries_data = fuel_prices.find_by_countries(['NL', 'BE'])
countries_data['NL'].gasoline_price
The method returns a hash with CountryData
objects with the input countries as keys.
Notes
All countries have prices in euros. If you need the local prices, you can use the _local
methods. Only countries that have a local currency will return data.
The price and currency will be return. See example #2
Thera are three possible nozzle types: bayonet
, acme
and dish
nil
will be returned if the source website has no information. The applies to the following:
- If
lpg_nozzle
is unknown. - If
lpg_stations
is unknown. - If
lpg_price_local
has no value. - If
unleaded_96_ron_price_local
has no value. - If
diesel_price_local
has no value.
0.0
will be returned if a the euro price for lpg_price
, unleaded_95_ron
and diesel_price
is not found.
Contributing
- Fork it ( https://github.com/[my-github-username]/fuel_prices_europe/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request