Giter Site home page Giter Site logo

hackpostcodes's Introduction

HackPostcodes

HackPostcodes is a library written in Hack that wraps around the https://postcodes.io API.

Motivation

I've been learning the Hack programming language, and the Hip Hop Virtual Machine (HHVM) - so seemed like a good excuse to make something usable.

Dependancies

  • This was written using HHVM v3.18, it's probably backwards compatible to a degree though
  • PHPUnit 5.7.* - There isn't compatibility with HHVM above this yet

Installation

You can install this library using composer:

$ composer install mfmbarber/HackPostcodes

Usage

To use the Postcodes library you first need to ensure you instantiate the CurlRequest class, and inject this into Postcodes as a constructor dependancy. This allows you to swap out the CurlRequest class with your own implementation if required. It also allows us to mock our dependancy when testing.

Once you have instantiated the Postcodes class you can then call any of the following methods off of this:

  • lookup(string $postcode) : Map Use this to lookup a single postcode and get back a map of the result

  • lookupBulk(Vector $postcodes) : Vector Use this to lookup a set of postcodes and get back a vector of maps

  • lookupLatLon(Pair<float, float> $geolocation) : Vector Use this to look up at latitude / longitude point and return a vector of maps - where each map represents a local postcodes to that position

  • lookupBulkLatLong(Vector<Pair<float, float>> $geolocations) : Vector<Vector> Use this to lookup a vector of longitude / latitude points and return a vector, of vectors, of maps. Where each first level represents a corresponding longitude / latitude point, and each sub-vector represents the local postcodes to that location

  • isValid(string $postcode) : bool Check to see if a postcode is valid (both regex and a UK postcode)

  • getNearest(string $postcode) : Vector Find the postcodes nearest to a given postcode, and return this as a Vector of Maps

  • autocomplete(string $postcode, int $limit = 10) : Vector Given part of a postcode, and the amount of results to return (limit is 100), return a Vector of potential full postcode strings

  • random() : Map Return a random postcode as a map

-getDistance(string $postcode1, string $postcode2) : num Return the shortest distance (straight line) between two postcodes, this uses the Haversine formula to calculate.

<?hh

include 'vendor/autoload.php';
use mfmbarber\HackPostcodes\Postcodes;
use mfmbarber\HackPostcodes\CurlRequest;

$postcodes = new Postcodes(new CurlRequest());

$x = $postcodes->lookup('DT51HG');
var_dump($x);

$y = $postcodes->lookupBulk(Vector {'DT51HG', 'BH122BL'});
var_dump($y);

$z = $postcodes->getDistance('DT51HG', 'BH122BL');
var_dump($z);

Tests

Tests have been written using PHPUnit, and can be run from the root of the project:

$ hhvm vendor/bin/phpunit tests/

Contributing

To contribute to this project

  • Create a fork of the project
  • Create a feature/bugfix branch off of develop
  • Commit your changes
  • Write your tests
  • Once you are happy - PR back into develop
  • Once reviewed, the PR is accepted.
  • Hurrah!

Ensure you comment your code!

License

This project is fully open sourced under MIT license

hackpostcodes's People

Contributors

mattamorphic avatar

Stargazers

 avatar  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.