Skip to content

Commit

Permalink
Fix /v1/onset
Browse files Browse the repository at this point in the history
  • Loading branch information
ysakasin committed Jul 4, 2017
1 parent 9968442 commit 6bf609c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 8 deletions.
16 changes: 8 additions & 8 deletions server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ def diceroll(system, command)
result, secret = bcdice.dice_command
dices = bcdice.getRandResults.map {|dice| {faces: dice[1], value: dice[0]}}

if result.nil?
raise CommandError
end

return result, secret, dices
end
end
Expand Down Expand Up @@ -59,9 +63,6 @@ def diceroll(system, command)

get "/v1/diceroll" do
result, secret, dices = diceroll(params[:system], params[:command])
if result.nil?
raise CommandError
end

jsonp ok: true, result: result, secret: secret, dices: dices
end
Expand All @@ -71,12 +72,11 @@ def diceroll(system, command)
return BCDice::SYSTEMS.join("\n")
end

result, secret, dices = diceroll(params[:sys] || "DiceBot", params[:text])

if result.nil?
"error"
else
begin
result, secret, dices = diceroll(params[:sys] || "DiceBot", params[:text])
"onset" + result
rescue UnsupportedDicebot, CommandError
"error"
end
end

Expand Down
30 changes: 30 additions & 0 deletions test/test_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,36 @@ def test_no_command
assert_equal json["reason"], "unsupported command"
end

def test_onset_list
get "/v1/onset?list=1"

list = last_response.body.split("\n")

assert last_response.ok?
assert_include list, "Amadeus"
end

def test_onset_diceroll
get "/v1/onset?sys=Cthulhu&text=1d20"

assert last_response.ok?
assert last_response.body.start_with?("onset: (1D20)")
end

def test_onset_unexpected_dicebot
get "/v1/onset?sys=AwesomeDicebot&text=1d20"

assert last_response.ok?
assert_equal last_response.body, "error"
end

def test_onset_unexpected_command
get "/v1/onset?sys=DiceBot&text=a"

assert last_response.ok?
assert_equal last_response.body, "error"
end

def test_not_found
get "/hogehoge"
json = JSON.parse(last_response.body)
Expand Down

0 comments on commit 6bf609c

Please sign in to comment.