Skip to content

Commit

Permalink
Add additional examples, bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
auralia committed Nov 16, 2016
1 parent 19626f0 commit 063795e
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 43 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
## 0.1.6 ##

* Bug fixes
* Fix dependencies
* Add additional examples

## 0.1.5 ##

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ it to the console.
var nsapi = require("nsapi");

// TODO: Replace the user agent with your own
var api = new nsapi.NsApi("<user agent>");
var api = new nsapi.NsApi("Your nation's name");
return api.nationRequest("Auralia", ["fullname"])
.then(function(data) {
console.log(data["fullname"]);
Expand Down
4 changes: 2 additions & 2 deletions docs/classes/nsapi.html
Original file line number Diff line number Diff line change
Expand Up @@ -916,7 +916,7 @@ <h4 class="tsd-returns-title">Returns <span class="tsd-signature-type">Promise</
<a name="authenticaterequest" class="tsd-anchor"></a>
<h3>authenticate<wbr>Request</h3>
<ul class="tsd-signatures tsd-kind-method tsd-parent-kind-class">
<li class="tsd-signature tsd-kind-icon">authenticate<wbr>Request<span class="tsd-signature-symbol">(</span>nation<span class="tsd-signature-symbol">: </span><span class="tsd-signature-type">string</span>, checksum<span class="tsd-signature-symbol">: </span><span class="tsd-signature-type">string</span>, token<span class="tsd-signature-symbol">: </span><span class="tsd-signature-type">string</span><span class="tsd-signature-symbol"> | </span><span class="tsd-signature-type">undefined</span><span class="tsd-signature-symbol">)</span><span class="tsd-signature-symbol">: </span><span class="tsd-signature-type">Promise</span><span class="tsd-signature-symbol">&lt;</span><span class="tsd-signature-type">boolean</span><span class="tsd-signature-symbol">&gt;</span></li>
<li class="tsd-signature tsd-kind-icon">authenticate<wbr>Request<span class="tsd-signature-symbol">(</span>nation<span class="tsd-signature-symbol">: </span><span class="tsd-signature-type">string</span>, checksum<span class="tsd-signature-symbol">: </span><span class="tsd-signature-type">string</span>, token<span class="tsd-signature-symbol">?: </span><span class="tsd-signature-type">string</span><span class="tsd-signature-symbol">)</span><span class="tsd-signature-symbol">: </span><span class="tsd-signature-type">Promise</span><span class="tsd-signature-symbol">&lt;</span><span class="tsd-signature-type">boolean</span><span class="tsd-signature-symbol">&gt;</span></li>
</ul>
<ul class="tsd-descriptions">
<li class="tsd-description">
Expand Down Expand Up @@ -947,7 +947,7 @@ <h5>checksum: <span class="tsd-signature-type">string</span></h5>
</div>
</li>
<li>
<h5>token: <span class="tsd-signature-type">string</span><span class="tsd-signature-symbol"> | </span><span class="tsd-signature-type">undefined</span></h5>
<h5><span class="tsd-flag ts-flagOptional">Optional</span> token: <span class="tsd-signature-type">string</span></h5>
<div class="tsd-comment tsd-typography">
<p>Site-specific token. No token will be specified if this
value is left undefined.</p>
Expand Down
2 changes: 1 addition & 1 deletion docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ <h2 id="examples">Examples</h2>
<pre><code class="lang-js"><span class="hljs-keyword">var</span> nsapi = <span class="hljs-built_in">require</span>(<span class="hljs-string">"nsapi"</span>);

<span class="hljs-comment">// <span class="hljs-doctag">TODO:</span> Replace the user agent with your own</span>
<span class="hljs-keyword">var</span> api = <span class="hljs-keyword">new</span> nsapi.NsApi(<span class="hljs-string">"&lt;user agent&gt;"</span>);
<span class="hljs-keyword">var</span> api = <span class="hljs-keyword">new</span> nsapi.NsApi(<span class="hljs-string">"Your nation's name"</span>);
<span class="hljs-keyword">return</span> api.nationRequest(<span class="hljs-string">"Auralia"</span>, [<span class="hljs-string">"fullname"</span>])
.then(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">data</span>) </span>{
<span class="hljs-built_in">console</span>.log(data[<span class="hljs-string">"fullname"</span>]);
Expand Down
113 changes: 80 additions & 33 deletions examples/example.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
var nsapi = require("../lib/api.js");

// TODO: Replace the user agent with your own
var api = new nsapi.NsApi("<user agent>");
var api = new nsapi.NsApi("Your nation's name");

// The following is a simple example that retrieves the nation Auralia's full
// name and prints it to the console.
function example1() {
function nationApiExample() {
return api.nationRequest("Auralia", ["fullname"])
.then(function(data) {
console.log(data["fullname"]);
Expand All @@ -30,44 +30,52 @@ function example1() {

// The following is the same example as example 1, but notice how it completes
// much faster because the previous request was cached!
function example2() {
return api.nationRequest("Auralia", ["fullname"])
.then(function(data) {
console.log(data["fullname"]);
});
function cacheExample() {
return nationApiExample();
}

// The following example retrieves the delegate, founder, and list of nations
// in the region of Catholic
function example3() {
// in the region of Catholic.
function regionApiExample() {
return api.regionRequest("Catholic", ["nations", "delegate", "founder"])
.then(function(data) {
console.log("Region of Catholic");
console.log();
console.log("Delegate: " + data["delegate"]);
console.log("Founder: " + data["founder"]);
console.log("Nations: " + data["nations"].split(":"));
})
}

// The following example retrieves the last 5 founding happening entries.
function example4() {
function worldApiExample() {
return api.worldRequest(["happenings"],
{filter: "founding", limit: "5"})
.then(function(data) {
for (var i = 0; i < data["happenings"]["event"].length; i++) {
var event = data["happenings"]["event"][i];
console.log();
console.log("Event ID: " + event["id"]);
console.log("Event Timestamp: " + event["timestamp"]);
console.log("Event Text: " + event["text"]);
console.log();
}
})
}

// The following example retrieves information about the last Security Council
// resolution at vote.
function worldAssemblyApiExample() {
return api.worldAssemblyRequest(nsapi.WorldAssemblyCouncil.SecurityCouncil,
["lastresolution"])
.then(function(data) {
console.log(data["lastresolution"]);
});
}

// The following is a complex example that retrieves and sorts the list of
// nations in the region of Catholic by their influence score, then prints the
// list to the console.
function example5() {
function complexExample() {
return api.regionRequest("Catholic", ["nations"])
.then(function(data) {
var nations = data["nations"].split(":");
Expand Down Expand Up @@ -108,54 +116,93 @@ function example5() {
}
}

// The following example uses private shards to retrieve the notices associated
// with a nation from the last 24 hours and print them to the console, along
// with the PIN required for future private shard requests.
function example6() {
function telegramExample() {
// TODO: Replace telegram details with your own
var clientKey = "";
var telegramId = "";
var telegramSecretKey = "";

return api.telegramRequest(clientKey, telegramId, telegramSecretKey,
"Auralia", nsapi.TelegramType.NonRecruitment)
.then(() => console.log("Telegram sent"))
.catch(err => console.log("Telegram was not sent: " + err));
}

function authenticationExample() {
// TODO: Replace nation name and checksum with your own
var nation = "";
var checksum = "";

return api.authenticateRequest(nation, checksum)
.then(success => {
if (success) {
console.log("Authentication succeeded");
} else {
console.log("Authentication failed");
}
});
}

// The following example uses private shards to retrieve the next issue time
// and print it to the console, along with the PIN required for future private
// shard requests.
function privateShardsExample() {
// TODO: Replace the nation name and password with your own
var nationName = "<nation name>";
var nationPassword = "<nation password>";
var nationName = "";
var nationPassword = "";

let auth = {
password: nationPassword,
updatePin: true
};
return api.nationRequest(
nationName,
["notices"],
{"from": String(Math.floor(Date.now() / 1000) - (60 * 60 * 24))},
["nextissuetime"],
undefined,
auth)
.then(function(data) {
console.log(data["notices"]);
console.log("Next issue time: "+ data["nextissuetime"]);
console.log("PIN: " + auth.pin);
});
}

// The following code executes each example.
Promise.resolve()
.then(function() {
console.log("Example 1:\n");
return example1();
console.log("Nation API example:\n");
return nationApiExample();
})
.then(function() {
console.log("\nCache example:\n");
return cacheExample();
})
.then(function() {
console.log("\nRegion API example:\n");
return regionApiExample();
})
.then(function() {
console.log("\nWorld API example:");
return worldApiExample();
})
.then(function() {
console.log("\nExample 2:\n");
return example2();
console.log("\nWorld Assembly API example:\n");
return worldAssemblyApiExample();
})
.then(function() {
console.log("\nExample 3:\n");
return example3();
console.log("\nComplex example:\n");
return complexExample();
})
.then(function() {
console.log("\nExample 4:\n");
return example4();
console.log("\nTelegram example:\n");
return telegramExample();
})
.then(function() {
console.log("\nExample 5:\n");
return example5();
console.log("\nAuthentication example:\n");
return authenticationExample();
})
.then(function() {
console.log("\nExample 6:\n");
return example6();
console.log("\nPrivate shards example:\n");
return privateShardsExample();
})
.catch(function(err) {
console.log(err);
Expand Down
12 changes: 6 additions & 6 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,7 @@ export class NsApi {
params += "&client=" + encodeURIComponent(clientKey);
params += "&tgid=" + encodeURIComponent(tgId);
params += "&key=" + encodeURIComponent(tgKey);
params += "&to=" + encodeURIComponent(recipient);
params += "&to=" + encodeURIComponent(NsApi.toId(recipient));

return this.apiRequest(this.apiPath(params), type, undefined)
.then((data: string) => {
Expand Down Expand Up @@ -609,11 +609,11 @@ export class NsApi {
* authenticated.
*/
public authenticateRequest(nation: string, checksum: string,
token: string | undefined): Promise<boolean>
token?: string): Promise<boolean>
{
return Promise.resolve().then(() => {
let params = "a=verify";
params += "&nation=" + encodeURIComponent(nation);
params += "&nation=" + encodeURIComponent(NsApi.toId(nation));
params += "&checksum=" + encodeURIComponent(checksum);
if (token) {
params += "&token=" + encodeURIComponent(token);
Expand Down Expand Up @@ -707,9 +707,9 @@ export class NsApi {
* Creates a NationStates API path from a set of parameters.
*/
private apiPath(params: string): string {
let path = "/cgi-bin/api.cgi?";
path += params;
path += "&userAgent=" + encodeURIComponent(this.userAgent);
let path = "/cgi-bin/api.cgi?userAgent="
+ encodeURIComponent(this.userAgent);
path += "&" + params;
return path;
}

Expand Down

0 comments on commit 063795e

Please sign in to comment.