-
Notifications
You must be signed in to change notification settings - Fork 53
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 #513 from dictu-lang/develop
Release 0.24.0
- Loading branch information
Showing
37 changed files
with
910 additions
and
100 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
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
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
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
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,49 @@ | ||
/** | ||
* Example of how to use the HTTP module and JSON modules to interact with the | ||
* OpenWeatherMap REST API. This can serve as a base to extend with more features. | ||
*/ | ||
|
||
import Env; | ||
import HTTP; | ||
import JSON; | ||
import System; | ||
|
||
const BASE_URL = "http://api.openweathermap.org/data/2.5/weather?"; | ||
|
||
class Client { | ||
private token; | ||
|
||
init(units, lang, token) { | ||
this.units = units; | ||
this.lang = lang; | ||
this.token = token; | ||
} | ||
|
||
current(name) { | ||
var url = BASE_URL; | ||
url = url + "appid=" + this.token; | ||
url = url + "&q=" + name; | ||
url = url + "&units=" + this.units; | ||
url = url + "&lang=" + this.lang; | ||
|
||
const res = HTTP.get(url); | ||
return JSON.parse(res.unwrap()["content"]).unwrap(); | ||
} | ||
} | ||
|
||
// main | ||
{ | ||
const token = Env.get("OWM_TOKEN"); | ||
if (not token) { | ||
print("error: OWM_TOKEN required to be set in the environment"); | ||
System.exit(1); | ||
} | ||
|
||
const location = "Phoenix"; | ||
|
||
const client = Client("F", "en", token); | ||
const current = client.current(location); | ||
|
||
print("Current Weather Data for: {} \n\n{}\n".format(location, current)); | ||
} | ||
|
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
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,82 @@ | ||
/** | ||
* Module logger provides a structured logger. The structured | ||
* format is JSON. | ||
*/ | ||
|
||
import JSON; | ||
import System; | ||
|
||
/** | ||
* class Log implements a simple structured logger | ||
* that outputs to either stdout or a given file. | ||
*/ | ||
class Log { | ||
private output; | ||
|
||
init(output="") { | ||
this.output = ""; | ||
if (output.len() > 1) { | ||
this.output = output; | ||
} | ||
} | ||
|
||
private set_level(level) { | ||
this.entry = {"level": level}; | ||
} | ||
|
||
private run(msg, fields) { | ||
this.entry["timestamp"] = System.time(); | ||
|
||
if (fields) { | ||
fields.keys().forEach( | ||
def(entry) => this.entry[entry] = fields[entry] | ||
); | ||
} | ||
this.entry["msg"] = msg; | ||
|
||
const j = JSON.parse(this.entry.toString()); | ||
|
||
if (this.output.len() > 1) { | ||
with(this.output, "a") { | ||
file.writeLine(j.unwrap().toString()); | ||
} | ||
} else { | ||
print(j.unwrap()); | ||
} | ||
} | ||
|
||
info(msg, fields=nil) { | ||
this.set_level("info"); | ||
this.run(msg, fields); | ||
} | ||
|
||
warn(msg, fields=nil) { | ||
this.set_level("warn"); | ||
this.run(msg, fields); | ||
} | ||
|
||
error(msg, fields=nil) { | ||
this.set_level("error"); | ||
this.run(msg, fields); | ||
} | ||
|
||
debug(msg, fields=nil) { | ||
this.set_level("debug"); | ||
this.run(msg, fields); | ||
} | ||
|
||
trace(msg, fields=nil) { | ||
this.set_level("trace"); | ||
this.run(msg, fields); | ||
} | ||
} | ||
|
||
// main | ||
{ | ||
const log = Log(); | ||
log.info("starting application"); | ||
|
||
System.sleep(2); | ||
|
||
log.info("shutting down application", {"extra-field-1": "more data"}); | ||
} |
Oops, something went wrong.