Skip to content

Commit 1bb902c

Browse files
author
Miqueas Martinez
committed
Use composition in the Nim example
1 parent 26374d4 commit 1bb902c

File tree

1 file changed

+7
-9
lines changed

1 file changed

+7
-9
lines changed

Github.nim

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ from std/os import commandLineParams
77
const BASE_URL = "https://api.github.com/users/"
88

99
type
10-
GthUserItem* = ref object
10+
GthUserItem* = object
1111
count*: int
1212
arr*: seq[string]
1313

14-
GthUser* = ref object
14+
GthUser* = object
1515
# Private
1616
obj: JsonNode
1717
url: string
@@ -24,10 +24,9 @@ type
2424
followers*: GthUserItem
2525
following*: GthUserItem
2626

27-
proc newGthUser*(username: string): GthUser =
28-
var self = GthUser()
27+
proc init*(self: var GthUser; username: string) =
2928
self.url = BASE_URL & username
30-
29+
3130
var res = newHttpClient().getContent(self.url)
3231
self.obj = parseJson(res)
3332

@@ -40,9 +39,7 @@ proc newGthUser*(username: string): GthUser =
4039
self.followers = GthUserItem(count: self.obj["followers"].getInt())
4140
self.following = GthUserItem(count: self.obj["following"].getInt())
4241

43-
return self
44-
45-
method fetch*(self: GthUser, thing: string): void {.base.} =
42+
proc fetch*(self: var GthUser; thing: string) =
4643
var
4744
url = self.url & '/' & thing
4845
res = newHttpClient().getContent(url)
@@ -90,7 +87,8 @@ if args.len() == 0:
9087
echo "No arguments, nothing to do."
9188
else:
9289
for username in args:
93-
var user = newGthUser(username)
90+
var user = GthUser()
91+
user.init(username)
9492

9593
echo "Name: ", user.name
9694
echo "Bio: ", user.bio

0 commit comments

Comments
 (0)