Skip to content

Commit

Permalink
Add punkapi-javascript-wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
brettdewoody committed Mar 19, 2017
0 parents commit 2dd2b55
Show file tree
Hide file tree
Showing 11 changed files with 483 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"presets": ["es2015"]
}
14 changes: 14 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module.exports = {
"extends": "standard",
"plugins": [
"standard",
"promise"
],
"globals": {
"fetch": false,
"qa": false
},
"rules": {
"no-extra-bind": 0
}
};
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
dist
node_modules
11 changes: 11 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Contributing

## Issues

Open an issue for any problem you have with the wrapper. Please include any and all relevant information needed to reproduce the issue.

## Contributions

All new features and bug fixes should be submitted as pull requests, so the community can review and discuss them.

When you submit a pull request, @mention me (@brettdewoody) so I'm notified and can review. Once reviewed and approved the pull request can be merged.
22 changes: 22 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Copyright (c) 2017 Brett DeWoody

MIT License

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
247 changes: 247 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,247 @@
# PunkAPI Javascript Wrapper

A JS wrapper for the [BrewDog Punk API V2](https://punkapi.com/documentation/v2) - an API for retrieving data about [BrewDog's beers](https://www.brewdog.com/).

## Installation

There are 3 ways to install the PunkAPI Javascript Wrapper.

#### Clone
Clone this repo into your project with:

git clone https://github.com/brettdewoody/punkapi-javascript-wrapper.git

#### NPM
Install via `npm` with:

npm install --save punkapi-javascript-wrapper

#### Bower
Install via `bower` with:

bower install punkapi-javascript-wrapper

## Usage

There are several ways the wrapper can be used. The wrapper is available as a library script ([option #1](#library-option-1)), can be imported with [Webpack](https://webpack.github.io/) into a bundled script for the browser ([option #2](#webpack-for-the-browser-option-2)), or can be imported into Node ([option #3](#webpack-for-node-option-3)).

**Library (Option #1)**

Include the compiled library script on your page with:

<script type="text/javascript" src="/path/to/punkapi-javascript-wrapper/dist/punkapi-javascript-wrapper.js"></script>

Then create a new instance of the `PunkAPIWrapper` and get to work:

const punkAPI = new PunkAPIWrapper()

**Webpack for the Browser (Option #2)**

Import `PunkAPIWrapper` in your `entry` file with

const PunkAPIWrapper = require('/path/to/punkapi-javascript-wrapper/src/punkapi-javascript-wrapper.js')

Then create a new instance of `PunkAPIWrapper` and get to work:

const punkAPI = new PunkAPIWrapper()

Finally `webpack` your code using your Webpack config.

**Node (Option #3)**

The same implementation as Option #2.

Import `PunkAPIWrapper` in your `entry` file with

const PunkAPIWrapper = require(punkapi-javascript-wrapper)

Then create a new instance of `PunkAPIWrapper` and get to work:

const punkAPI = new PunkAPIWrapper()

Here's a [Glitch demo](https://glitch.com/edit/#!/punkapi-javascript-wrapper-demo) showing how to use the wrapper in Node and the results:

* A random beer - [https://punkapi-javascript-wrapper-demo.glitch.me/beer/random](https://punkapi-javascript-wrapper-demo.glitch.me/beer/random)
* A specific beer - [https://punkapi-javascript-wrapper-demo.glitch.me/beer/1](https://punkapi-javascript-wrapper-demo.glitch.me/beer/1)
* Strong beers (using a URL param of ?abv_gt=8) - [https://punkapi-javascript-wrapper-demo.glitch.me/beer?abv_gt=8](https://punkapi-javascript-wrapper-demo.glitch.me/beer?abv_gt=8)

## Methods

The wrapper provides 5 methods for retrieving all the BrewDog-related info you want:

* [`punkAPI.getBeer(:id)`](#getbeerid) - a specific beer
* [`punkAPI.getBeers(:options)`]()- beers matching the options
* [`punkAPI.getRandom()`]() - a single random beer
* [`punkAPI.getRateLimit()`]() - total number of requests allowed to the PunkAPI in an hour
* [`punkAPI.getRateLimitRemaining()`]() - number of remaining requests allowed to the PunkAPI in the hour

**Note:** All methods query the BrewDog Punk API using an asynchronous `fetch` request and return a [Promise](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise). This means you'll need to use `.then()` to wait for the response and provide a callback for handling the returned data.

const punkAPI = new PunkAPIWrapper()

const randomBeer = PunkAPI.getRandom()

randomBeer.then(beer => {
alert(beer[0].name)
})

#### `getBeer(:id)`

Returns an array of length 1 with the beer matching an ID of `:id`. `:id` should be a number corresponding to the ID of the desired beer.

**Example:** const beer1 = punkAPI.getBeer(1)

#### `getBeers(:options)`

Returns an array with beers matching the `:options`. `:options` is an object consisting of available filters (below).

**Example:** const strongBeers = punkAPI.getBeers({'abv_gt': 8})

<table>
<thead>
<tr><td class="ttu pa2 br bb b--light-silver bg-light-gray">Param</td>
<td class="ttu pa2 br bb b--light-silver bg-light-gray">Type</td>
<td class="ttu pa2 bb b--light-silver bg-light-gray">Details</td>
</tr></thead>
<tbody>
<tr>
<td>abv_gt</td>
<td>number</td>
<td>Returns all beers with ABV greater than the supplied number</td>
</tr>

<tr>
<td>abv_lt</td>
<td>number</td>
<td>Returns all beers with ABV less than the supplied number</td>
</tr>

<tr>
<td>ibu_gt</td>
<td>number</td>
<td>Returns all beers with IBU greater than the supplied number</td>
</tr>

<tr>
<td>ibu_lt</td>
<td>number</td>
<td>Returns all beers with IBU less than the supplied number</td>
</tr>

<tr>
<td>ebc_gt</td>
<td>number</td>
<td>Returns all beers with EBC greater than the supplied number</td>
</tr>

<tr>
<td>ebc_lt</td>
<td>number</td>
<td>Returns all beers with EBC less than the supplied number</td>
</tr>

<tr>
<td>beer_name</td>
<td>string</td>
<td>Returns all beers matching the supplied name (this will match partial strings as well so e.g punk will return Punk IPA), if you need to add spaces just add an underscore (_).</td>
</tr>

<tr>
<td>yeast</td>
<td>string</td>
<td>Returns all beers matching the supplied yeast name, this performs a fuzzy match, if you need to add spaces just add an underscore (_).</td>
</tr>

<tr>
<td>brewed_before</td>
<td>date</td>
<td>Returns all beers brewed before this date, the date format is mm-yyyy e.g 10-2011</td>
</tr>

<tr>
<td>brewed_after</td>
<td>date</td>
<td>Returns all beers brewed after this date, the date format is mm-yyyy e.g 10-2011</td>
</tr>

<tr>
<td>hops</td>
<td>string</td>
<td>Returns all beers matching the supplied hops name, this performs a fuzzy match, if you need to add spaces just add an underscore (_).</td>
</tr>

<tr>
<td>malt</td>
<td>string</td>
<td>Returns all beers matching the supplied malt name, this performs a fuzzy match, if you need to add spaces just add an underscore (_).</td>
</tr>

<tr>
<td>food</td>
<td>string</td>
<td>Returns all beers matching the supplied food string, this performs a fuzzy match, if you need to add spaces just add an underscore (_).</td>
</tr>

<tr>
<td>ids</td>
<td>string (id|id|...)</td>
<td>Returns all beers matching the supplied ID's. You can pass in multiple ID's by separating them with a | symbol.</td>
</tr>
</tbody>
</table>

For the most up-to-date filters please see the [PunkAPI docs](https://punkapi.com/documentation/v2).

#### `getRandom()`

Returns an array of length 1 with a random beer

**Example:** const randomBeer = punkAPI.getRandom()

#### `getRateLimit()`

Returns a number displaying the rate limit. This is currently set to 3600 requests per hour per IP address.

**Example:** const rateLimit= punkAPI.getRateLimit()

#### `getRateLimitRemaining()`

Returns a number displaying the available number of requests remaining for this IP address.

**Example:** const rateLimitRemaining= punkAPI.getRateLimitRemaining()

## Examples

**Get a random beer**

const randomBeer = PunkAPI.getRandom()

randomBeer.then(beer => {
alert(beer[0].name)
})


**Get strong beers**

const strongBeers = PunkAPI.getBeers({'abv_gt': 8})

strongBeers.then(beers => {
beers.forEach(beer => {
alert(beer.name)
})
})

**Get remaining request limit**
const remainingRequests = PunkAPI.getRateLimitRemaining()

remainingRequests.then(requests => {
alert(requests)
})

## Contributing

Please see the [Contributing Guide](/blob/develop/Contributing.md).

## License

This project is licensed under the terms of the MIT license.
17 changes: 17 additions & 0 deletions bower.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "punkapi-javascript-wrapper",
"description": "A Javascript wrapper for the PunkAPI",
"main": "dist/punkapi-javascript-wrapper.js",
"authors": [
"Brett DeWoody <[email protected]> (http://brettdewoody.com)"
],
"license": "MIT",
"keywords": [
"punkapi",
"beer",
"javascript",
"wrapper",
"api"
],
"homepage": "https://github.com/brettdewoody/punkapi-javascript-wrapper"
}
37 changes: 37 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"name": "punkapi-javascript-wrapper",
"version": "1.0.0",
"description": "A Javascript wrapper for the PunkAPI",
"main": "src/punkapi-javascript-wrapper.js",
"scripts": {
"test": "test/punkapi-javascript-wrapper.spec.js"
},
"keywords": [
"punkapi",
"beer",
"javascript",
"wrapper",
"api"
],
"author": "Brett DeWoody <[email protected]> (http://brettdewoody.com)",
"license": "MIT",
"devDependencies": {
"babel-core": "^6.24.0",
"babel-loader": "^6.4.0",
"babel-preset-env": "^1.2.1",
"babel-preset-es2015": "^6.24.0",
"chai": "^3.5.0",
"eslint": "^3.17.1",
"eslint-config-standard": "^7.0.1",
"eslint-plugin-promise": "^3.5.0",
"eslint-plugin-standard": "^2.1.1",
"mocha": "^3.2.0",
"should": "^11.2.1",
"webpack": "^2.2.1"
},
"dependencies": {
"es6-promise": "^4.1.0",
"isomorphic-fetch": "^2.2.1",
"qs": "^6.4.0"
}
}
Loading

0 comments on commit 2dd2b55

Please sign in to comment.