This repository has been archived by the owner on Oct 15, 2022. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from pardocz/master
Initial spice implementation for Expatistan
- Loading branch information
Showing
3 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<html> | ||
<head> | ||
|
||
<!-- | ||
This is the function you define -- see its details in the repository. | ||
//--> | ||
<script type="text/javascript" src="/spice/expatistan/spice.js"></script> | ||
|
||
<!-- | ||
This is a stub function that takes what you pass it | ||
and creates the Zero-click Info that gets shoved | ||
into the body of the page in the div marked "a". | ||
//--> | ||
<script type="text/javascript"> | ||
function nra(items) { | ||
document.getElementById('a').innerHTML = items[0]['a']; | ||
} | ||
</script> | ||
|
||
</head> | ||
|
||
<body> | ||
|
||
<!-- | ||
This is a placeholder div for the Zero-click Info that nra() populates | ||
//--> | ||
<div id="a"></div> | ||
|
||
<script type="text/javascript" src="http://www.expatistan.com/api/spice?q=cost+of+living+in+london&api_key=wideopen"></script> | ||
<script type="text/javascript" src="http://www.expatistan.com/api/spice?q=cost+of+living+london+vs+melbourne&api_key=wideopen"></script> | ||
|
||
</body> | ||
</html> |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
|
||
|
||
/* | ||
This is the function you define. | ||
To see a live example of what this function produces, check out: | ||
http://duckduckgo.com/?q=is+it+raining+in+philadelphia | ||
Here is the flow (as laid out in the ../example.html file | ||
1) The external API is called through our servers, e.g. | ||
http://duckduckgo.com/ir/Phialadelphia | ||
2) The API call returns JSON wrapped in a callback function, i.e. JSONP. | ||
3) That callback function is defined below. It's name corresponds | ||
to the internal API endpoint, i.e. /ir/ becomes /nrir/. | ||
Your project will get its own unique endpoint, and thus | ||
function name. | ||
4) The callback function receives the JSON object as its argument. | ||
There is no need to parse the string returned by the API; it is | ||
already a JavaScript object -- that's the beauty of JSONP. | ||
You can check out what it looks like by using Firebug or | ||
a browser extention like JSONView | ||
5) The function should do some validity checks to make sure | ||
what it got back is appropriate for display. | ||
6) If so, then you create the return object and send it to nra(), | ||
which displays it on the page. | ||
*/ | ||
|
||
function nrexp(ir) { | ||
var snippet = ''; | ||
|
||
// For debugging. | ||
// console.log(ir); | ||
|
||
// Validity check. | ||
if (ir['status'] == 'OK') { | ||
|
||
// Snippet that gets shown in the 0-click box. | ||
snippet = ir['abstract']; | ||
|
||
items = new Array(); | ||
items[0] = new Array(); | ||
items[0]['a'] = snippet; | ||
|
||
items[0]['h'] = ''; | ||
|
||
// Source name and url for the More at X link. | ||
items[0]['s'] = 'Expatistan'; | ||
items[0]['u'] = ir['source_url']; | ||
|
||
nra(items); | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
$q_check =~ /cost of living/i |