-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Handle invalid options #8
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
e5334ca
:sparkles: Implement Split.Parser and some helper modules
tteerawat c1259c7
:recycle: Refactor Router module
tteerawat 1c7a091
:art: Apply Parser to Split
tteerawat e27bc4b
:fire: Remove unused functions from Split module
tteerawat dbd41b7
:recycle: Change apply_multiplier/2 to public function with doc test
tteerawat 9580a72
:recycle: Refactor OptionTransformer
tteerawat 1fc3f10
:art: Edit InputTransformer to handle invalid inputs
tteerawat e79647d
:recycle: Refactor InputTransformer.transform_to_orders/3
tteerawat e1ebbe0
:fire: Remove todo
tteerawat bd37f5c
:fire: Remove parsed() type in Parser module
tteerawat 377bc2a
Merge branch 'master' into validate-options
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,7 +12,7 @@ defmodule Chopperbot.Router do | |
|
||
post "/split" do | ||
input = conn.body_params["text"] | ||
response = Character.happy_talk() <> "\n\n" <> Split.run(input) | ||
response = build_response(input) | ||
|
||
body = %{ | ||
"response_type" => "in_channel", | ||
|
@@ -34,21 +34,15 @@ defmodule Chopperbot.Router do | |
] = conn.params["events"] | ||
|
||
response = | ||
if text |> String.downcase() |> String.starts_with?("split") do | ||
["", input] = text |> String.downcase() |> String.split("split ") | ||
Character.happy_talk() <> "\n\n" <> Split.run(input) | ||
else | ||
[ | ||
"Now I can help you split the bill 💸! Just type `split` following by orders. For example...", | ||
"", | ||
"1️⃣", | ||
"split alice 100 alice 250 bob 200 +vat +service", | ||
"2️⃣", | ||
"split alice 100 bob 200 +v", | ||
"3️⃣", | ||
"split alice 100 bob 200 share 100", | ||
] | ||
|> Enum.join("\n") | ||
text | ||
|> String.trim() | ||
|> String.downcase() | ||
|> case do | ||
"split " <> input -> | ||
build_response(input) | ||
|
||
_ -> | ||
build_line_suggestion_response() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice for returning help here. |
||
end | ||
|
||
Linex.Message.reply(response, reply_token) | ||
|
@@ -61,4 +55,28 @@ defmodule Chopperbot.Router do | |
match _ do | ||
send_resp(conn, 404, "not found") | ||
end | ||
|
||
defp build_response(input_text) do | ||
case Split.run(input_text) do | ||
{:ok, ok_msg} -> | ||
Character.happy_talk() <> "\n\n" <> ok_msg | ||
|
||
{:error, error_msg} -> | ||
Character.confused_talk() <> "\n\n" <> error_msg | ||
end | ||
end | ||
|
||
defp build_line_suggestion_response do | ||
[ | ||
"Now I can help you split the bill 💸! Just type `split` following by orders. For example...", | ||
"", | ||
"1️⃣", | ||
"split alice 100 alice 250 bob 200 +vat +service", | ||
"2️⃣", | ||
"split alice 100 bob 200 +v", | ||
"3️⃣", | ||
"split alice 100 bob 200 share 100" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, out of the scope, but maybe we can add the discount use case as well. (maybe later) |
||
] | ||
|> Enum.join("\n") | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
defmodule Chopperbot.Split.InputTransformer do | ||
alias Chopperbot.Split.Order | ||
|
||
@doc """ | ||
Transform inputs to a list of orders. | ||
|
||
## Examples | ||
iex> transform(["turbo", "10", "kendo", "200"]) | ||
{:ok, [{"turbo", 10.0}, {"kendo", 200.0}]} | ||
|
||
iex> transform(["ant", "200", "pipe", "100", "share", "-30"]) | ||
{:ok, [{"ant", 200.0}, {"pipe", 100.0}, {"share", -30.0}]} | ||
|
||
iex> transform(["Satoshi", "10.9", "Takeshi", "390.13", "satoshi", "112.50"]) | ||
{:ok, [{"satoshi", 10.9}, {"takeshi", 390.13}, {"satoshi", 112.5}]} | ||
|
||
iex> transform([]) | ||
{:ok, []} | ||
|
||
iex> transform(["turbo", "ten", "kendo", "twenty"]) | ||
{:error, :invalid_input, ["ten", "twenty"]} | ||
|
||
iex> transform(["turbo", "100", "kendo", "200", "chopper"]) | ||
{:error, :invalid_input, ["chopper"]} | ||
|
||
iex> transform(["turbo", "ten", "kendo", "200", "chopper"]) | ||
{:error, :invalid_input, ["ten", "chopper"]} | ||
""" | ||
@spec transform([String.t()]) :: {:ok, [Order.t()]} | {:error, :invalid_input, [String.t()]} | ||
def transform(inputs) do | ||
input_pairs = Enum.chunk_every(inputs, 2) | ||
|
||
case transform_to_orders(input_pairs) do | ||
{orders, []} -> | ||
{:ok, orders} | ||
|
||
{_, invalid_inputs} -> | ||
{:error, :invalid_input, invalid_inputs} | ||
end | ||
end | ||
|
||
defp transform_to_orders(input_pairs, orders \\ [], invalid_inputs \\ []) | ||
|
||
defp transform_to_orders([input_pair | rest_input_pairs], orders, invalid_inputs) do | ||
with {:ok, name, amount} <- validate_input_pair(input_pair), | ||
{:ok, float_amount} <- validate_amount_string(amount) do | ||
order = {String.downcase(name), float_amount} | ||
|
||
transform_to_orders( | ||
rest_input_pairs, | ||
[order | orders], | ||
invalid_inputs | ||
) | ||
else | ||
{:error, invalid_input} -> | ||
transform_to_orders( | ||
rest_input_pairs, | ||
orders, | ||
[invalid_input | invalid_inputs] | ||
) | ||
end | ||
end | ||
|
||
defp transform_to_orders([], orders, invalid_inputs) do | ||
{Enum.reverse(orders), Enum.reverse(invalid_inputs)} | ||
end | ||
|
||
defp validate_input_pair(input_pair) do | ||
case input_pair do | ||
[name, amount] -> {:ok, name, amount} | ||
[invalid_input] -> {:error, invalid_input} | ||
end | ||
end | ||
|
||
defp validate_amount_string(string) do | ||
case Float.parse(string) do | ||
{float_number, ""} -> {:ok, float_number} | ||
_ -> {:error, string} | ||
end | ||
end | ||
end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like this way of matching 👍