Skip to content

Commit

Permalink
Added NPM package with Ionic Angular example
Browse files Browse the repository at this point in the history
  • Loading branch information
airportcodes committed Jun 9, 2018
1 parent c68f9aa commit 3e9701d
Show file tree
Hide file tree
Showing 11 changed files with 447 additions and 3 deletions.
2 changes: 2 additions & 0 deletions .gitignore
@@ -1 +1,3 @@
_precompiled
/config.codekit3
.DS_Store
44 changes: 44 additions & 0 deletions README.html
@@ -0,0 +1,44 @@
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
<meta charset="utf-8"/>
</head>
<body>

<h1 id="air-port-codesapisdk">Air-port-codes API SDK</h1>

<p>The SDK for accessing all 9000+ airport codes and airport data from <a href="https://www.air-port-codes.com">Air-port-codes</a> API data feed.</p>

<h2 id="installation">Installation</h2>

<ol>
<li>First login to your air-port-codes.com account and update your account settings by adding any domains that might need to access the API.</li>
<li>Make a note of your API Key as you will need it for all requests to the API.</li>
<li>Consult the language specific documentation inside each language specific directory.</li>
</ol>

<h2 id="integration">Integration</h2>

<ol>
<li>Node.js: <a href="https://github.com/airportcodes/API-SDK/tree/master/javascript/node">NPM Package</a></li>
<li>Client side: <a href="https://github.com/airportcodes/API-SDK/tree/master/javascript">Javascript</a></li>
<li>Server side: <a href="https://github.com/airportcodes/API-SDK/tree/master/php">PHP</a></li>
</ol>

<h2 id="contributing">Contributing</h2>

<ol>
<li>Fork it!</li>
<li>Create your feature branch: <code>git checkout -b my-new-feature</code></li>
<li>Commit your changes: <code>git commit -am 'Add some feature'</code></li>
<li>Push to the branch: <code>git push origin my-new-feature</code></li>
<li>Submit a pull request :D</li>
</ol>

<h2 id="license">License</h2>

<p>MIT license | (c) 2016 AIR-PORT-CODES | Air-port-codes.com/legal/terms-of-use</p>

</body>
</html>

5 changes: 3 additions & 2 deletions README.md
Expand Up @@ -9,8 +9,9 @@ The SDK for accessing all 9000+ airport codes and airport data from [Air-port-co
3. Consult the language specific documentation inside each language specific directory.

## Integration
1. Client side: [Javascript](https://github.com/airportcodes/API-SDK/tree/master/javascript)
2. Server side: [PHP](https://github.com/airportcodes/API-SDK/tree/master/php)
1. Node.js: [NPM Package](https://github.com/airportcodes/API-SDK/tree/master/javascript/node)
2. Client side: [Javascript](https://github.com/airportcodes/API-SDK/tree/master/javascript)
3. Server side: [PHP](https://github.com/airportcodes/API-SDK/tree/master/php)

## Contributing

Expand Down
2 changes: 1 addition & 1 deletion javascript/air-port-codes-api-min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
98 changes: 98 additions & 0 deletions javascript/examples/ionic-angular-autocomplete.ts
@@ -0,0 +1,98 @@
import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { autocomplete } from 'air-port-codes-node';

// declare const apc:any;

@Component({
selector: 'page-airport-codes',
templateUrl: 'airportCodes.html'
})
export class AirportCodes {
private apca;
public airports = [];

constructor(public navCtrl: NavController) {
// instantiate the air-port-codes API
// see teh index.html for the included air-port-codes js library
this.apca = autocomplete({
key : 'xxxxxxxxxx',
secret : 'xxxxxxxxxxxxxxx', // Your API Secret Key: use this if you are not connecting from a web server
limit : 15
});
}

/**
* This is triggered on every keypress of the search field
* @param ev the event object
*/
onNewSearchTerm (ev: any) {
const term:string = ev.target.value;

if (term.length >= 3) {
// Make the API request
this.apca.request(term);

// SUCCESS we found some airports
this.apca.onSuccess = (data) => {
this.airports = this.buildAirportList(data);
};

// FAIL no airports found
this.apca.onError = (data) => {
console.log('onError', data.message);
this.airports = [];
};
} else {
this.airports = [];
}
}

/**
* This is fired whenever an airport is selected
* @param airport the selected airport object
*/
onSelectAirport (airport:any) {
// do something with the data
console.log('airport', airport)
}

/**
* This updates the response to add an appropriate label to each airport item
* @param data the data we get back from the API
*/
buildAirportList (data:any) {
let listAry = [],
thisAirport;

if (data.status) { // success
for (var i = 0, len = data.airports.length; i < len; i++) {
thisAirport = data.airports[i];
listAry.push(this.addAirportLabel(thisAirport));
if (thisAirport.children) {
for (var j = 0, jLen = thisAirport.children.length; j < jLen; j++) {
listAry.push(this.addAirportLabel(thisAirport.children[j], true));
}
}
}
}
return listAry;
}

/**
* Creates the appropriate label. If it is a child it will add an indent arrow.
* @param airport the object of a single airport
* @param isChild a boolean letting us know if this airport is a child of another
*/
addAirportLabel (airport:any, isChild?:boolean) {
let label;
if (isChild) { // format children labels
label = '&rdsh;' + airport.iata + ' - ' + airport.name;
} else { // format labels
label = airport.iata + ' - ' + airport.name;
}
airport.label = label;

return airport;
}
}
33 changes: 33 additions & 0 deletions javascript/node/README.md
@@ -0,0 +1,33 @@
# Air-port-codes API SDK

The SDK for accessing all 9000+ airport codes and airport data from [Air-port-codes](https://www.air-port-codes.com) API data feed.

## Installation

1. First login to your air-port-codes.com account and update your account settings by adding any domains that might need to access the API.
2. Make a note of your API Key and your API Secret as you will need them for all requests to the API.
3. Install the npm library

```
npm install air-port-codes-node
```

## Integration
Consult the [documentation](https://www.air-port-codes.com/airport-codes-api/overview/) for the appropriate config settings to use.

Also, you can view the Ionic Angular example TypeScript file showing it in practice.
```
import { autocomplete } from 'air-port-codes-node';
this.apca = autocomplete({
key : 'xxxxxxxxxxx',
secret : 'xxxxxxxxxxxxxxx', // Your API Secret Key: use this if you are not connecting from a web server
limit : 15
});
```



## License

MIT license | (c) 2016 AIR-PORT-CODES | Air-port-codes.com/legal/terms-of-use
150 changes: 150 additions & 0 deletions javascript/node/air-port-codes-api-node.js
@@ -0,0 +1,150 @@
'use strict';
/* global ActiveXObject */

/*!
* The main vanilla javascript library for accessing the Air-port-codes API
*
* @license MIT license
* apc npm library v1.2.0 | (c) 2018 AIR-PORT-CODES | Air-port-codes.com/terms-of-use
*/

/**
* The main library handling the requests to the Air-port-codes API service
* @param {string} type autocomplete | multi | single | countries | states - determines the desired api to use
* @param {array} config allows you to set initial configuration
* @return {object} the public access points
*/

const main = ( type, config ) => {
const reqObj = {};
const uri = 'https://www.air-port-codes.com/api/v1/';
let ajax;

const scope = {
request: ( data ) => {
scope[type](data);
},

init: () => {
for(const item in config) {
if (item !== 'key' && item !== 'secret') {
reqObj[item] = config[item];
}
}
},

autocomplete: (term) => {
reqObj.term = term;
scope.doAjax();
},

multi: (term) => {
reqObj.term = term;
scope.doAjax();
},

single: (iataCode) => {
reqObj.iata = iataCode;
scope.doAjax();
},

countries: () => {
scope.doAjax();
},

states: () => {
scope.doAjax();
},

doAjax: () => {
ajax.post(uri + type, reqObj, ( data ) => {
data = JSON.parse(data);
if (data.status) { // success
scope.onSuccess(data);
} else { // no results
scope.onError(data);
}
});
}
};

ajax = {
x : () => {
if (typeof XMLHttpRequest !== 'undefined') {
return new XMLHttpRequest();
}
var versions = [
"MSXML2.XmlHttp.6.0",
"MSXML2.XmlHttp.5.0",
"MSXML2.XmlHttp.4.0",
"MSXML2.XmlHttp.3.0",
"MSXML2.XmlHttp.2.0",
"Microsoft.XmlHttp"
];

let xhr;
for (var i = 0; i < versions.length; i++) {
try {
xhr = new ActiveXObject(versions[i]);
break;
} catch (e) {
}
}
return xhr;
},

send : function (url, callback, method, data, async) {
if (async === undefined) {
async = true;
}
const x = ajax.x();
x.open(method, url, async);
x.onreadystatechange = () => {
if (x.readyState === 4) {
callback(x.responseText);
}
};
if (method === 'POST') {
x.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
x.setRequestHeader('APC-Auth', config.key);
if (config.secret) {
x.setRequestHeader('APC-Auth-Secret', config.secret);
}
}
x.send(data);
},

post : (url, data, callback, async) => {
let query = [];
for (const key in data) {
query.push(encodeURIComponent(key) + '=' + encodeURIComponent(data[key]));
}
ajax.send(url, callback, 'POST', query.join('&'), async);
}
};

scope.init();

return scope;
};

const autocomplete = (config) => main('autocomplete', config);
const multi = (config) => main('multi', config);
const single = (config) => main('single', config);
const countries = (config) => main('countries', config);
const states = (config) => main('states', config);

export default main;
export { autocomplete, multi, single, countries, states }

/*
Usage...
import { autocomplete } from 'air-port-codes-node';
this.apca = autocomplete({
key : 'xxxxxxxxxxx',
secret : 'xxxxxxxxxxxxxxx', // Your API Secret Key: use this if you are not connecting from a web server
limit : 15
});
*/
26 changes: 26 additions & 0 deletions javascript/node/package.json
@@ -0,0 +1,26 @@
{
"name": "air-port-codes-node",
"version": "1.3.0",
"description": "The main javascript library for accessing the Air-port-codes API",
"main": "air-port-codes-api-node.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/airportcodes/API-SDK.git"
},
"keywords": [
"airport",
"codes",
"iata",
"air-port",
"api"
],
"author": "Air-port-codes <jesse@air-port-codes.com>",
"license": "ISC",
"bugs": {
"url": "https://github.com/airportcodes/API-SDK/issues"
},
"homepage": "https://github.com/airportcodes/API-SDK#readme"
}

0 comments on commit 3e9701d

Please sign in to comment.