Git Product home page Git Product logo

spain-gas's Introduction

Spain Gas

Header Packagist Version Total Downloads

Spain Gas is a PHP client that act as a wrapper around Spain's Geoportal Gasolineras API. It has the following main features:

  • Obtain data about all gas stations in Spain: addresses, geolocation, provided services, schedules and much more!
  • Exact prices of fuel for every gas station in the country, updated multiple times every day.
  • Beautifully designed interface using design patterns like the builder pattern.
  • Statically typed with the latest PHP typing features.
  • Properly tested with coverage higher than 80%.

Requirements

  • You need composer to be installed, either globally or locally in your project.
  • Compatible with PHP 7.4 or higher.
  • PHP's extensions needed: ext-json and ext-simplexml.

Installation

To install this package be sure to have composer installed and run the following command in your project's directory:

composer require flerex/spain-gas

Usage

All the API's endpoints are available by using the GasApi class. Currently, there are only two endpoints for the API:

  • gasStations: This is the most useful endpoint. With it, you can find gas stations by different filters such as province or availability.
  • towns: Returns a list of Town objects. Useful to obtain the ID of a specific town to use as filter in other endpoints.
  • locateGasStations: This endpoint provides the geographical location of gas stations. The only difference with the previous endpoint is that provides a ranking of the gas station according to the price of the fuel (if specified as a filter).

NOTE: All the aforementioned endpoints will provide the price of the fuel if specified by using the query filter fuel. Otherwise, it will always return null.

Every endpoint returns an instance of a Builder class, which provides methods to filter the request to be made. Once you selected your filtering parameters, a call to method get is required to actually run the request and retrieve the data.

In case you need more information about what filters are available check the Documentation.

Endpoints

Here is a list of all available endpoints, and the differences in the data retrieved.

Endpoint Documentation Ranking Prices Geographical Location Station Information Town ID
gasStations Wiki ✖️ ✔️ * ✔️ ✔️ ✖️
locateGasStations Wiki ✔️ * ✔️ * ✔️ ✖️ ✖️
towns Wiki ✖️ ✖️ ✖️ ✖️ ✔️

* Only returned when filtering by fuel.

Example

Obtain Stations by Fuel

Here is an example where we are retrieving all CNG gas stations in Spain:

use Flerex\SpainGas\Enums\Fuel;
use Flerex\SpainGas\GasApi;

$stations = GasApi::gasStations()
    ->fuel(Fuel::CNG())
    ->get();

Get specific town ID

Let us suppose we needed to obtain all gas stations in A Coruña (city). To be able to filter by town in the gasStations endpoint, we will first need to retrieve the ID of the town.

use Flerex\SpainGas\Dtos\Town;
use Flerex\SpainGas\Enums\Province;
use Flerex\SpainGas\GasApi;

$towns = GasApi::towns()
    ->province(Province::A_CORUNHA())
    ->get();
    
$townId = array_filter($towns, fn(Town $town) => stripos($town->name, 'Coruña') !== false)[0];

// Everything up to this point could be done once and then be replaced with the obtained ID to avoid unnecessary API calls. 

$stations = GasApi::gasStations()
    ->town($townId)
    ->get();

Get the price ranking

Price rankings are used to categorize the price of some fuel in a gas station comparing it to their competitors. The algorithm used by the API is unknown.

Unfortunately, rankings are only available in the locateGasStations endpoints.

In the following example we are going to retrieve the cheapest self-service gas stations.

use Flerex\SpainGas\Dtos\GasStationLocation;
use Flerex\SpainGas\Enums\Rank;
use Flerex\SpainGas\Enums\Fuel;
use Flerex\SpainGas\Enums\ServiceType;
use Flerex\SpainGas\GasApi;

$stations = GasApi::locateGasStations()
    ->serviceType(ServiceType::SELF_SERVICE())
    ->fuel(Fuel::DIESEL_A())
    ->get();

$cheapest = array_filter($stations, fn(GasStationLocation $station) => $station->rank->equals(Rank::CHEAP()));

NOTE: We need to specify a fuel so that rankings are provided in return.

Changelog

Changelog can be found in releases.

Copyright and License

Spain Gas was written by Flerex and is released under the MIT License.

© Flerex 2020

spain-gas's People

Contributors

flerex avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.