ctucx.git: nimhafas

[nimlang] hafas-client library

1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
26 
27 
28 
29 
30 
31 
32 
33 
34 
35 
36 
37 
38 
import ../types
import json
import leg
import options

proc mkParseJourney*(common: CommonData): proc =
  proc parseJourney(j: JsonNode): Journey =
    var common = common
    common.dateStr = j{"date"}.getStr()

    if j{"trfRes"}{"statusCode"}.getStr == "OK":
      result.price = some(Price(
        amount: j["trfRes"]["fareSetL"][0]["fareL"][0]["prc"].getInt / 100,
        currency: some("Euro"),
      ))

    result.refreshToken = j{"ctxRecon"}.getStr()
    result.legs = j{"secL"}.getElems().map(mkParseLeg(common))
    result.lastUpdated = common.timeStamp

    # combine walking legs
    var i = -1
    var firstWalking = -1
    while true:
      inc(i)
      if i >= len(result.legs): break
      if result.legs[i].isWalking:
        if firstWalking == -1:
          firstWalking = i
        else:
          result.legs[firstWalking].arrival = result.legs[i].arrival
          result.legs[firstWalking].distance.get += result.legs[i].distance.get
          result.legs.delete(i)
          dec(i)
      else:
        firstWalking = -1

  return parseJourney