From 4ad4b4f9972748181e1e823a88f9de5aa24cc673 Mon Sep 17 00:00:00 2001 From: Christine Dodrill Date: Wed, 12 Aug 2015 11:34:49 -0700 Subject: [PATCH] begin nim client --- client/nim/.gitignore | 2 ++ client/nim/ponyapi.nim | 51 ++++++++++++++++++++++++++++++++++++++++++ ponyapi.nimble | 10 +++++++++ 3 files changed, 63 insertions(+) create mode 100644 client/nim/.gitignore create mode 100644 client/nim/ponyapi.nim create mode 100644 ponyapi.nimble diff --git a/client/nim/.gitignore b/client/nim/.gitignore new file mode 100644 index 0000000..82c4fa6 --- /dev/null +++ b/client/nim/.gitignore @@ -0,0 +1,2 @@ +ponyapi +nimcache diff --git a/client/nim/ponyapi.nim b/client/nim/ponyapi.nim new file mode 100644 index 0000000..c953a51 --- /dev/null +++ b/client/nim/ponyapi.nim @@ -0,0 +1,51 @@ +import httpclient +import json + +type + Episode* = object of RootObj + name: string + air_date: int + season: int + episode: int + is_movie: bool + +const + API_ENDPOINT: string = "http://ponyapi.apps.xeserv.us" + +proc getJson(endpoint: string): json.JsonNode = + var + data = httpclient.getContent(API_ENDPOINT & "/" & endpoint) + jsonTable = json.parseJson data + + return jsonTable + +proc newest*(): Episode = + var + data = getJson("/newest")["episode"] + ep = Episode(name: data["name"].getStr, + air_date: data["air_date"].getNum.int, + season: data["season"].getNum.int, + episode: data["episode"].getNum.int, + is_movie: data["is_movie"].getBVal) + + return ep + +proc `$`*(ep: Episode): string = + return "Episode:: name:" & ep.name & " season:" & $ep.season & " episode:" & $ep.episode & " air_date:" & $ep.air_date & " is_movie:" & $ep.is_movie + +when isMainModule: + import unittest + + suite "ponyapi tests": + var ep: Episode + + test "Newest episode lookup": + try: + ep = newest() + + except: + echo getCurrentExceptionMsg() + fail + + test "stringify episode": + echo ep diff --git a/ponyapi.nimble b/ponyapi.nimble new file mode 100644 index 0000000..83b9ad6 --- /dev/null +++ b/ponyapi.nimble @@ -0,0 +1,10 @@ +[Package] +name = "ponyapi" +version = "0.1.0" +author = "Christine Dodrill " +description = "PonyAPI client https://github.com/Xe/PonyAPI" +license = "MIT" +srcDir = "client/nim" + +[Deps] +Requires: "nim >= 0.10.0"