Skip to content

Commit

Permalink
Adds "new" query parameter to subscription endpoint.
Browse files Browse the repository at this point in the history
Since Slack webhooks will always hit the API via POST HTTP requests, a
REST API approach is more complicated. To achieve good seggregation the
API will use query parameters, in this case, to add subscriptions "new"
will be used.

Furthermore, a bug has been fixed where the Plug.Conn wasn't being added
the json headers appropiately.
  • Loading branch information
manzanit0 committed May 24, 2019
1 parent 768303c commit ef14bb5
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 10 deletions.
6 changes: 3 additions & 3 deletions lib/web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ defmodule Londibot.Router do
post("/disruptions", do: send_resp(conn, 200, Londibot.Controller.report_all(:disruptions)))

post "/subscription" do
message =
conn =
conn
|> Plug.Conn.fetch_query_params()
|> Util.with_json_headers()
|> SubscriptionHandler.handle()

send_resp(conn, 200, message)
send_resp(conn, 200, SubscriptionHandler.handle(conn))
end

match(_, do: send_resp(conn, 404, "Nothing found here!"))
Expand Down
15 changes: 11 additions & 4 deletions lib/web/subscription_handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ defmodule Londibot.Web.SubscriptionHandler do

@subscription_store Application.get_env(:londibot, :subscription_store)

def handle(conn = %Plug.Conn{body_params: bp}), do: handle(bp)
def handle(body_params) do
def handle(conn = %Plug.Conn{body_params: bp, query_params: qp}), do: handle(bp, qp)
def handle(bp = %{"channel_id" => c, "text" => t}, %{"q" => "new"}), do: process_subscription(bp)
def handle(%{}, %{}), do: reprompt_message()

def process_subscription(body_params) do
body_params
|> to_subscription()
|> @subscription_store.save()
Expand All @@ -20,8 +23,12 @@ defmodule Londibot.Web.SubscriptionHandler do
%Subscription{channel_id: c, tfl_lines: lines}
end

def subscription_saved_message do
%{text: "Subscription saved!", response_type: "in_channel"}
def subscription_saved_message, do: to_payload("Subscription saved!")

def reprompt_message, do: to_payload("error: empty request")

defp to_payload(message) do
%{text: message, response_type: "in_channel"}
|> Poison.encode!()
end
end
4 changes: 2 additions & 2 deletions test/web/router_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ defmodule Londibot.RouterTest do
|> World.create()

conn =
conn(:post, "/subscription", %{channel_id: "123", text: "aaa"})
conn(:post, "/subscription?q=new", %{channel_id: "123", text: "aaa"})
|> Router.call(@opts)

assert conn.state == :sent
assert conn.status == 200

expected = {
200,
[{"cache-control", "max-age=0, private, must-revalidate"}],
[{"cache-control", "max-age=0, private, must-revalidate"}, {"content-type", "application/json; charset=utf-8"}],
"{\"text\":\"Subscription saved!\",\"response_type\":\"in_channel\"}"}
assert expected == Plug.Test.sent_resp(conn)
end
Expand Down
3 changes: 2 additions & 1 deletion test/web/subscription_handler_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ defmodule Londibot.Web.SubscriptionHandlerTest do
|> World.create()

message =
conn(:post, "/", %{"channel_id" => "123", "text" => "victoria"})
conn(:post, "/subscription?q=new", %{"channel_id" => "123", "text" => "victoria"})
|> Plug.Conn.fetch_query_params()
|> SubscriptionHandler.handle()

assert message == SubscriptionHandler.subscription_saved_message()
Expand Down

0 comments on commit ef14bb5

Please sign in to comment.