Skip to content

Commit 22a9a8b

Browse files
committed
Added argument support for /jokes, /challenges, /ranking
1 parent 36cd041 commit 22a9a8b

File tree

3 files changed

+60
-14
lines changed

3 files changed

+60
-14
lines changed

config/config.exs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use Mix.Config
22

33
config :app,
4-
bot_name: "lambdinha_bot"
4+
bot_name: ""
55

66
config :nadia,
7-
token: "eh segredo"
7+
token: ""
88

99
import_config "#{Mix.env}.exs"

lib/app/commands.ex

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,16 @@ defmodule App.Commands do
1717

1818
command ["desafios", "desafio", "challenges", "challenge"] do
1919
Logger.log :info, "Command /desafios | /desafio | /challenges | /challenge"
20-
App.Tools.get_challenges
20+
Logger.log :info, update.message.text
21+
App.Tools.get_args(update.message.text)
22+
|> App.Tools.get_challenges
2123
|> send_message
2224
end
2325

2426
command "ranking" do
2527
Logger.log :info, "Command /ranking"
26-
App.Tools.get_ranking
28+
App.Tools.get_args(update.message.text)
29+
|> App.Tools.get_ranking
2730
|> send_message
2831
end
2932

@@ -34,7 +37,8 @@ defmodule App.Commands do
3437

3538
command "joke" do
3639
Logger.log :info, "Command /joke"
37-
App.Tools.get_joke
40+
App.Tools.get_args(update.message.text)
41+
|> App.Tools.get_joke
3842
|> send_message
3943
end
4044
end

lib/app/tools.ex

Lines changed: 51 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,19 @@ defmodule App.Tools do
77
end
88
end
99

10+
# returns array of args as
11+
# "/command 1 2 3" -> "1 2 3"
12+
def get_args(text) do
13+
[_c | args] = String.split(text, " ")
14+
Enum.join(args, " ")
15+
end
16+
17+
# returns only the first digit as an integer
18+
def digit_to_int(digit) do
19+
{number, _rest} = Integer.parse(digit)
20+
number
21+
end
22+
1023
def handle_response({:ok, %{status_code: 200, body: body}}) do
1124
{:ok, Poison.Parser.parse!(body)}
1225
end
@@ -15,41 +28,70 @@ defmodule App.Tools do
1528
{:ok, "Error"}
1629
end
1730

18-
def extract_challenges(map) do
31+
32+
# Challenge extraction
33+
34+
def extract_challenges(map, _number = "") do
1935
{:ok, body} = map
2036
Enum.reduce body, [], fn (item, res) ->
2137
res ++ ['#{item["name"]} - soluções: #{item["solutions"]} - #{item["link"]} \n']
2238
end
2339
end
2440

25-
def extract_ranking(map) do
41+
def extract_challenges(map, number) do
42+
{:ok, body} = map
43+
item = Enum.at body, digit_to_int number
44+
'#{item["name"]} - soluções: #{item["solutions"]} - #{item["link"]} \n'
45+
end
46+
47+
# Ranking extraction
48+
49+
def extract_ranking(map, _top = "") do
2650
{:ok, body} = map
2751
Enum.reduce body, [], fn (item, res) ->
2852
res ++ ['#{item["ranking"]} - #{item["user"]} - #{item["pontuation"]} \n']
2953
end
3054
end
3155

32-
def extract_joke(map) do
56+
def extract_ranking(map, top) do
57+
{:ok, body} = map
58+
Enum.reduce Enum.slice(body, 0, digit_to_int top), [], fn (item, res) ->
59+
res ++ ['#{item["ranking"]} - #{item["user"]} - #{item["pontuation"]} \n']
60+
end
61+
end
62+
63+
# Joke extraction
64+
65+
def extract_joke(map, _number="") do
3366
{:ok, body} = map
3467
item = Enum.random body
3568
'#{item["joke"]}'
3669
end
3770

38-
def get_ranking do
71+
def extract_joke(map, number) do
72+
{:ok, body} = map
73+
item = Enum.at(body, digit_to_int number)
74+
'#{item["joke"]}'
75+
end
76+
77+
78+
# Command functions
79+
80+
def get_ranking(top \\ "") do
3981
HTTPoison.get("https://raw.githubusercontent.com/lambda-study-group/desafios/master/ranking.json")
4082
|> handle_response
41-
|> extract_ranking
83+
|> extract_ranking(top)
4284
end
4385

44-
def get_challenges do
86+
def get_challenges(number \\ "") do
4587
HTTPoison.get("https://raw.githubusercontent.com/lambda-study-group/desafios/master/challenges.json")
4688
|> handle_response
47-
|> extract_challenges
89+
|> extract_challenges(number)
4890
end
4991

50-
def get_joke do
92+
def get_joke(number \\ "") do
5193
HTTPoison.get("https://raw.githubusercontent.com/lambda-study-group/lambdinha-bot/master/jokes.json")
5294
|> handle_response
53-
|> extract_joke
95+
|> extract_joke(number)
5496
end
5597
end

0 commit comments

Comments
 (0)