split off episode manipulation code into its own module

This commit is contained in:
Christine Dodrill 2015-08-15 10:59:53 -07:00
parent d80166a420
commit 67c484ee19
2 changed files with 58 additions and 29 deletions

44
episode.nim Normal file
View File

@ -0,0 +1,44 @@
import json
type
Episode* = object of RootObj
## An episode of My Little Pony: Friendship is Magic
name*: string ## Episode name
air_date*: int ## Air date in unix time
season*: int ## season number of the episode
episode*: int ## the episode number in the season
is_movie*: bool ## does this record represent a movie?
proc `%`*(ep: Episode): JsonNode =
## Convert an Episode record to a JsonNode
%*
{
"name": ep.name,
"air_date": ep.air_date,
"season": ep.season,
"episode": ep.episode,
"is_movie": ep.is_movie,
}
proc `%`*(eps: seq[Episode]): JsonNode =
## Convert a sequence of episodes to a JsonNode
result = newJArray()
for ep in eps:
add result, %ep
proc newEpisodeFromNode*(data: json.JsonNode): Episode =
## Convert a json node into an episode object
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)
proc newEpisodeListFromNode*(data: json.JsonNode): seq[Episode] =
## Convert a json array into a sequence of episode objects
var ret: seq[Episode]
for item in data.items():
ret = ret & item.newEpisodeFromNode
return ret

View File

@ -1,4 +1,5 @@
import asyncdispatch import asyncdispatch
import episode
import future import future
import jester import jester
import json import json
@ -6,15 +7,6 @@ import random
import strutils import strutils
import times import times
type
Episode* = object of RootObj
## An episode of My Little Pony: Friendship is Magic
name*: string ## Episode name
air_date*: int ## Air date in unix time
season*: int ## season number of the episode
episode*: int ## the episode number in the season
is_movie*: bool ## does this record represent a movie?
var var
episodes: seq[Episode] episodes: seq[Episode]
@ -36,34 +28,27 @@ for line in lines "./fim.list":
episodes = episodes & ep episodes = episodes & ep
proc `%`(ep: Episode): JsonNode =
%*
{
"name": ep.name,
"air_date": ep.air_date,
"season": ep.season,
"episode": ep.episode,
"is_movie": ep.is_movie,
}
proc `%`(eps: seq[Episode]): JsonNode =
result = newJArray()
for ep in eps:
add result, %ep
proc `%%`(ep: Episode): JsonNode = proc `%%`(ep: Episode): JsonNode =
## Pack an episode for PonyAPI clients.
%* %*
{ {
"episode": ep, "episode": ep,
} }
proc `%%`(eps: seq[Episode]): JsonNode = proc `%%`(eps: seq[Episode]): JsonNode =
## Pack a sequence of episodes to a JsonNode for PonyAPI clients.
%* %*
{ {
"episodes": eps, "episodes": eps,
} }
proc `%!`(why: string): JsonNode =
## Make an error object
%*
{
"error": why
}
let myHeaders = { let myHeaders = {
"Content-Type": "application/json", "Content-Type": "application/json",
"X-Powered-By": "Nim and Jester", "X-Powered-By": "Nim and Jester",
@ -103,7 +88,7 @@ routes:
eps: seq[Episode] = lc[x | (x <- episodes, x.season == season), Episode] eps: seq[Episode] = lc[x | (x <- episodes, x.season == season), Episode]
if eps.len == 0: if eps.len == 0:
resp Http404, myHeaders, $ %* { "error": "No episodes found" } resp Http404, myHeaders, pretty(%!"No episodes found", 4)
else: else:
resp Http200, myHeaders, pretty(%%eps, 4) resp Http200, myHeaders, pretty(%%eps, 4)
@ -119,7 +104,7 @@ routes:
ep = episode ep = episode
if ep.air_date == 0: if ep.air_date == 0:
resp Http404, myHeaders, $ %* {"error": "No such episode"} resp Http404, myHeaders, pretty(%!"Not found", 4)
else: else:
resp Http200, myHeaders, pretty(%%ep, 4) resp Http200, myHeaders, pretty(%%ep, 4)
@ -128,14 +113,14 @@ routes:
query = @"q".toLower query = @"q".toLower
if query == "": if query == "":
halt Http406, myHeaders, $ %* { "error": "Need to specify query" } halt Http406, myHeaders, pretty(%!"Need to specify a query", 4)
var var
eps: seq[Episode] = eps: seq[Episode] =
lc[x | (x <- episodes, x.name.toLower.contains query), Episode] lc[x | (x <- episodes, x.name.toLower.contains query), Episode]
if eps.len == 0: if eps.len == 0:
resp Http404, myHeaders, $ %* { "error": "No episodes found" } resp Http404, myHeaders, pretty(%!"No episodes found", 4)
else: else:
resp Http200, myHeaders, pretty(%%eps, 4) resp Http200, myHeaders, pretty(%%eps, 4)