Created
February 18, 2015 14:14
-
-
Save MatthewBarker/b31666717fda611525a9 to your computer and use it in GitHub Desktop.
Make JSONP requests to the Wikipedia API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*jslint browser: true*/ | |
/*global define, exports, require, module, jsonp*/ | |
/** | |
A module for Jsonp requests to the Wikipedia API. | |
@module wikipedia-jsonp | |
@author Matt Barker | |
@requires jsonp | |
*/ | |
(function (factory) { | |
'use strict'; | |
if (typeof define === 'function' && define.amd) { | |
// AMD | |
define(['jsonp'], factory); | |
} else if (typeof exports === 'object') { | |
// CommonJS | |
module.exports = factory(require('jsonp')); | |
} else { | |
// Browser global | |
window.WikipediaJsonp = factory(jsonp); | |
} | |
}(function (jsonp) { | |
'use strict'; | |
/** | |
This callback type is used to handle JSONP responses. | |
@callback requestCallback | |
@param {Object} data - JSON response | |
*/ | |
/** | |
Initialises a new instance of the WikipediaJsonp class. | |
See the Query section of the API documentation for usage: | |
{@link http://www.mediawiki.org/wiki/API:Query } | |
@class WikipediaJsonp | |
@param {string} url - The Wikipedia site | |
@param {Object} [parameters] - Query parameters | |
@param {requestCallback} [callback] - Function to pass the returned JSON data to | |
@param {Object} [context] - Context for the callback function | |
@example | |
var url = 'http://en.wikipedia.org', | |
parameters = { | |
action: 'query', | |
list: 'geosearch', | |
gslimit: 100, | |
gsradius: 1000, | |
gscoord: '52.42350|-1.71347' | |
}; | |
function callback(data) { | |
console.log(data); | |
} | |
WikipediaJsonp(url, parameters, callback); | |
// Alternative | |
var wiki = new WikipediaJsonp(url); | |
wiki.send(parameters, callback); | |
*/ | |
function WikipediaJsonp(url, parameters, callback, context) { | |
if (!(this instanceof WikipediaJsonp)) { | |
return new WikipediaJsonp(url, parameters, callback, context); | |
} | |
this.url = url; | |
if (parameters) { | |
this.send(parameters, callback, context); | |
} | |
} | |
/** | |
Send the request to the jsonp module. | |
@param {Object} parameters - Query parameters | |
@param {requestCallback} callback - Function to pass the returned JSON data to | |
@param {Object} [context] - Context for the callback function | |
*/ | |
WikipediaJsonp.prototype.send = function (parameters, callback, context) { | |
var query = '', | |
key, | |
url; | |
for (key in parameters) { | |
if (parameters.hasOwnProperty(key)) { | |
query += '&' + key + '=' + encodeURIComponent(parameters[key]); | |
} | |
} | |
url = this.url + '/w/api.php?format=json' + query + '&callback='; | |
jsonp(url, callback, context || this); | |
}; | |
return WikipediaJsonp; | |
})); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment