Skip to content

Commit 4c2a2ce

Browse files
author
Miqueas
committed
Updated Lua and Nim examples
1 parent 93276fb commit 4c2a2ce

File tree

2 files changed

+41
-25
lines changed

2 files changed

+41
-25
lines changed

Github.lua

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ local json = require("rapidjson")
33
local argparse = require("argparse")
44

55
-- Base url for requests
6-
local API_USERS_URL = "https://api.github.com/users/"
6+
local BASE_URL = "https://api.github.com/users/"
77

88
---@class GthUser @Class for a github user basic info
99
---@field url string (private) Url to make requests, for internal usage
@@ -20,22 +20,22 @@ local GthUser = {}
2020
--- Constructor for GthUser class
2121
---@param username string The github username to get info
2222
---@return GthUser
23-
function GthUser:new(username)
23+
function GthUser:init(username)
2424
assert(
2525
type(username) == "string",
26-
"Bad argument for 'new', string expected, got " .. type(username)
26+
"Bad argument for 'init', string expected, got " .. type(username)
2727
)
2828

29-
self.url = API_USERS_URL .. username
29+
self.url = BASE_URL .. username
3030
local res = req.get(self.url)
3131
self.json = json.decode(res.text)
3232

3333
self.name = self.json['name']
3434
self.bio = self.json['bio']
3535
self.link = self.json['html_url']
3636

37-
self.repos = { count = self.json['public_repos'], arr = {} }
38-
self.gists = { count = self.json['public_gists'], arr = {} }
37+
self.repos = { count = self.json['public_repos'], arr = {} }
38+
self.gists = { count = self.json['public_gists'], arr = {} }
3939
self.followers = { count = self.json['followers'], arr = {} }
4040
self.following = { count = self.json['following'], arr = {} }
4141

@@ -48,7 +48,7 @@ end
4848
function GthUser:fetch(thing)
4949
assert(
5050
type(thing) == "string",
51-
"Bad argument for 'new', string expected, got " .. type(thing)
51+
"Bad argument for 'fetch', string expected, got " .. type(thing)
5252
)
5353

5454
local url = self.url .. '/' .. thing
@@ -77,21 +77,21 @@ function GthUser:fetch(thing)
7777
end
7878

7979
local opts = argparse({
80-
name = "Github.lua",
80+
name = "Github.lua",
8181
description = "Simple example of the Github's REST API",
82-
epilog = "Check out https://github.com/M1que4s/Github-REST-API-Example"
82+
epilog = "Check out https://github.com/M1que4s/Github-REST-API-Example"
8383
})
8484

8585
opts:argument("usernames", "One or more usernames"):args("+")
8686
opts:flag("-f --followers", "Shows user followers", false)
8787
opts:flag("-F --following", "Shows user following", false)
88-
opts:flag("-r --repos", "Shows user repos", false)
89-
opts:flag("-g --gists", "Shows user gists", false)
88+
opts:flag("-r --repos", "Shows user repos", false)
89+
opts:flag("-g --gists", "Shows user gists", false)
9090

9191
local args = opts:parse(arg)
9292

93-
for _, v in ipairs(args.usernames) do
94-
local user = GthUser:new(v)
93+
for _, user in ipairs(args.usernames) do
94+
local user = GthUser:init(user)
9595

9696
print("name: " .. user.name)
9797
print("bio: " .. user.bio)
@@ -132,4 +132,6 @@ for _, v in ipairs(args.usernames) do
132132
print("| @" .. v)
133133
end
134134
end
135+
136+
print()
135137
end

Github.nim

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,14 @@ type
2222

2323
proc init*(self: var GthUser; username: string) =
2424
self.url = BASE_URL & username
25-
26-
var res = newHttpClient().getContent(self.url)
27-
self.obj = parseJson(res)
2825

26+
let
27+
cl = newHttpClient()
28+
res = cl.getContent(self.url)
29+
30+
defer: cl.close()
31+
32+
self.obj = parseJson(res)
2933
self.name = self.obj["name"].getStr()
3034
self.bio = self.obj["bio"].getStr()
3135
self.link = self.obj["html_url"].getStr()
@@ -37,10 +41,13 @@ proc init*(self: var GthUser; username: string) =
3741

3842
proc fetch*(self: var GthUser; thing: string) =
3943
var
44+
cl = newHttpClient()
4045
url = self.url & '/' & thing
41-
res = newHttpClient().getContent(url)
46+
res = cl.getContent(url)
4247
arr = parseJson(res)
4348

49+
defer: cl.close()
50+
4451
case thing
4552
of "repos":
4653
for v in arr.items():
@@ -57,8 +64,8 @@ proc fetch*(self: var GthUser; thing: string) =
5764
else:
5865
echo "Unsupported endpoint: ", thing
5966

67+
let cmdline = commandLineParams()
6068
var
61-
cmdline = commandLineParams()
6269
args: seq[string]
6370
opts: tuple[
6471
repos: bool,
@@ -69,15 +76,22 @@ var
6976

7077
for kind, name, value in getopt(cmdline):
7178
case kind:
72-
of cmdArgument: args.add(name)
79+
of cmdArgument:
80+
args.add(name)
7381
of cmdShortOption, cmdLongOption:
7482
case name:
75-
of "r", "repos": opts.repos = true
76-
of "g", "gists": opts.gists = true
77-
of "f", "followers": opts.followers = true
78-
of "F", "following": opts.following = true
79-
else: continue
80-
of cmdEnd: break
83+
of "r", "repos":
84+
opts.repos = true
85+
of "g", "gists":
86+
opts.gists = true
87+
of "f", "followers":
88+
opts.followers = true
89+
of "F", "following":
90+
opts.following = true
91+
else:
92+
continue
93+
of cmdEnd:
94+
break
8195

8296
if args.len() == 0:
8397
echo "No arguments, nothing to do."

0 commit comments

Comments
 (0)