Add nim documentation to nim module
This commit is contained in:
parent
87d10f89a5
commit
fc0c98e4d2
|
@ -2,25 +2,30 @@ import cgi
|
||||||
import httpclient
|
import httpclient
|
||||||
import json
|
import json
|
||||||
|
|
||||||
|
## A bunch of glue for Nim applications to query PonyAPI.
|
||||||
|
|
||||||
type
|
type
|
||||||
Episode* = object of RootObj
|
Episode* = object of RootObj
|
||||||
name*: string
|
## An episode of My Little Pony: Friendship is Magic
|
||||||
air_date*: int
|
name*: string ## Episode name
|
||||||
season*: int
|
air_date*: int ## Air date in unix time
|
||||||
episode*: int
|
season*: int ## season number of the episode
|
||||||
is_movie*: bool
|
episode*: int ## the episode number in the season
|
||||||
|
is_movie*: bool ## does this record represent a movie?
|
||||||
|
|
||||||
const
|
const
|
||||||
API_ENDPOINT: string = "http://ponyapi.apps.xeserv.us"
|
API_ENDPOINT: string = "http://ponyapi.apps.xeserv.us"
|
||||||
|
|
||||||
proc getJson(endpoint: string): json.JsonNode =
|
proc getJson(endpoint: string): json.JsonNode =
|
||||||
|
## get the HTTP body for the API base endpoint catted with the specific endpoint
|
||||||
|
## requested.
|
||||||
var
|
var
|
||||||
data = httpclient.getContent(API_ENDPOINT & "/" & endpoint)
|
data = httpclient.getContent(API_ENDPOINT & "/" & endpoint)
|
||||||
jsonTable = json.parseJson data
|
|
||||||
|
|
||||||
return jsonTable
|
data.parseJson
|
||||||
|
|
||||||
proc newEpisodeFromNode(data: json.JsonNode): Episode =
|
proc newEpisodeFromNode(data: json.JsonNode): Episode =
|
||||||
|
## Convert a json node into an episode object
|
||||||
Episode(name: data["name"].getStr,
|
Episode(name: data["name"].getStr,
|
||||||
air_date: data["air_date"].getNum.int,
|
air_date: data["air_date"].getNum.int,
|
||||||
season: data["season"].getNum.int,
|
season: data["season"].getNum.int,
|
||||||
|
@ -28,6 +33,7 @@ proc newEpisodeFromNode(data: json.JsonNode): Episode =
|
||||||
is_movie: data["is_movie"].getBVal)
|
is_movie: data["is_movie"].getBVal)
|
||||||
|
|
||||||
proc newEpisodeListFromNode(data: json.JsonNode): seq[Episode] =
|
proc newEpisodeListFromNode(data: json.JsonNode): seq[Episode] =
|
||||||
|
## Convert a json array into a sequence of episode objects
|
||||||
var ret: seq[Episode]
|
var ret: seq[Episode]
|
||||||
for item in data.items():
|
for item in data.items():
|
||||||
ret = ret & item.newEpisodeFromNode
|
ret = ret & item.newEpisodeFromNode
|
||||||
|
@ -35,21 +41,27 @@ proc newEpisodeListFromNode(data: json.JsonNode): seq[Episode] =
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
proc newest*(): Episode =
|
proc newest*(): Episode =
|
||||||
|
## returns information on the newest episode of My Little Pony: Friendship is Magic.
|
||||||
getJson("/newest")["episode"].newEpisodeFromNode
|
getJson("/newest")["episode"].newEpisodeFromNode
|
||||||
|
|
||||||
proc random*(): Episode =
|
proc random*(): Episode =
|
||||||
|
## returns information on a random episode.
|
||||||
getJson("/random")["episode"].newEpisodeFromNode
|
getJson("/random")["episode"].newEpisodeFromNode
|
||||||
|
|
||||||
proc get_episode*(season, episode: int): Episode =
|
proc get_episode*(season, episode: int): Episode =
|
||||||
|
## return an arbitrary episode by season, episode pair.
|
||||||
getJson("/season/" & $season & "/episode/" & $episode)["episode"].newEpisodeFromNode
|
getJson("/season/" & $season & "/episode/" & $episode)["episode"].newEpisodeFromNode
|
||||||
|
|
||||||
proc all_episodes*(): seq[Episode] =
|
proc all_episodes*(): seq[Episode] =
|
||||||
|
## return all information on all episodes.
|
||||||
getJson("/all")["episodes"].newEpisodeListFromNode
|
getJson("/all")["episodes"].newEpisodeListFromNode
|
||||||
|
|
||||||
proc get_season*(season: int): seq[Episode] =
|
proc get_season*(season: int): seq[Episode] =
|
||||||
|
## return all information on a single season.
|
||||||
getJson("/season/" & $season)["episodes"].newEpisodeListFromNode
|
getJson("/season/" & $season)["episodes"].newEpisodeListFromNode
|
||||||
|
|
||||||
proc search*(term: string): seq[Episode] =
|
proc search*(term: string): seq[Episode] =
|
||||||
|
## searches for episodes by the given query term.
|
||||||
getJson("/search?q=" & term.encodeURL())["episodes"].newEpisodeListFromNode
|
getJson("/search?q=" & term.encodeURL())["episodes"].newEpisodeListFromNode
|
||||||
|
|
||||||
when isMainModule:
|
when isMainModule:
|
||||||
|
|
Loading…
Reference in New Issue