Browse Source

code cleanup

jordan 3 years ago
parent
commit
1ab263a1d8

+ 13 - 7
README.md

@@ -7,12 +7,18 @@ To start your Phoenix server:
 
 Now you can visit [`localhost:4000`](http://localhost:4000) from your browser.
 
-Ready to run in production? Please [check our deployment guides](https://hexdocs.pm/phoenix/deployment.html).
+# This is a project composed by [jordyn](http://jordandashel.com)
 
-## Learn more
+It is intended to be a learning excercise with Elixir. While the code is not _bad_, 
+it is Elixir baby-talk for me, being new to the language. Please excuse any violation
+of idioms.
 
-  * Official website: https://www.phoenixframework.org/
-  * Guides: https://hexdocs.pm/phoenix/overview.html
-  * Docs: https://hexdocs.pm/phoenix
-  * Forum: https://elixirforum.com/c/phoenix-forum
-  * Source: https://github.com/phoenixframework/phoenix
+The thing this project needs is tests. It has a decent end-to-end python script that
+can be run to be sure it works as expected from an api perspective, but it by no means
+scours the corners of the code for errors.
+
+It was a lot of fun designing with the agent/supervisor model although the "generate
+a new agent based on user-provided term" is probably not the best solution, it works
+fine in this scenario and great as a learning activity.
+
+Please enjoy: [4-letters.net](http://4-letters.net)

+ 1 - 0
lib/fourletters/troll.ex

@@ -7,6 +7,7 @@ defmodule Fourletters.Troll do
   @impl true
   def init(_arg) do
     children = [
+      # initial case, not required
       %{id: :butt, start: {ABCD.Fourletters, :start_link, [["yo"]]}}
     ]
 

+ 32 - 79
lib/fourletters_web/controllers/fourletters_controller.ex

@@ -1,18 +1,27 @@
 defmodule FourlettersWeb.FourlettersController do
   use FourlettersWeb, :controller
   require Logger
-  # import Fourletters.Troll
-  # import ABCD.Fourletters
 
-  def fourletters(conn, %{"fourletters" => fourletters}) do
-    if String.length(fourletters) == 4 and fourletters =~ ~r(^[a-z!]*$) do
-
-      pid = case Supervisor.start_child(
+  defp get_pid(for_letters) do
+      case Supervisor.start_child(
           Fourletters.Troll,
-          %{id: String.to_atom(fourletters), start: {ABCD.Fourletters, :start_link, [[]]}}) do
+          %{id: String.to_atom(for_letters), start: {ABCD.Fourletters, :start_link, [[]]}}) do
         {:ok, pid} -> pid
         {:error, {:already_started, pid}} -> pid
       end
+  end
+
+  defp valid_fourletters(for_letters) do
+    String.length(for_letters) == 4 and for_letters =~ ~r(^[a-z!]*$)
+  end
+
+ def index(conn, _params) do
+   render(conn, "index.html")
+  end
+
+  def fourletters(conn, %{"fourletters" => fourletters}) do
+    if valid_fourletters(fourletters) do
+      pid = get_pid(fourletters)
       messages = ABCD.Fourletters.get(pid)
       render(conn, "four.html", fourletters: fourletters, messages: messages)
     else
@@ -21,14 +30,8 @@ defmodule FourlettersWeb.FourlettersController do
   end
 
   def getmessages(conn, %{"fourletters" => fourletters}) do
-    if String.length(fourletters) == 4 and fourletters =~ ~r(^[a-z!]*$)  do
-
-      pid = case Supervisor.start_child(
-          Fourletters.Troll,
-          %{id: String.to_atom(fourletters), start: {ABCD.Fourletters, :start_link, [[]]}}) do
-        {:ok, pid} -> pid
-        {:error, {:already_started, pid}} -> pid
-      end
+    if valid_fourletters(fourletters) do
+      pid = get_pid(fourletters)
       messages = ABCD.Fourletters.get(pid)
       json(conn, %{fourletters: fourletters, messages: messages})
     else
@@ -36,99 +39,49 @@ defmodule FourlettersWeb.FourlettersController do
     end
   end
 
-  def addletters(conn, _params) do
-    %{params: params} = conn
+  def addletters(conn, params) do
     %{"fourletters" => fourletters, "message" => message} = params
-    if String.length(fourletters) == 4 and String.length(message) < 101 and fourletters =~ ~r(^[a-z!]*$)  do
-
+    if valid_fourletters(fourletters) and String.length(message) < 101  do
       Logger.info params
-      pid = case Supervisor.start_child(
-          Fourletters.Troll,
-          %{id: String.to_atom(fourletters), start: {ABCD.Fourletters, :start_link, [[]]}}) do
-        {:ok, pid} -> pid
-        {:error, {:already_started, pid}} -> pid
-      end
-      _ = ABCD.Fourletters.put(pid, message)
-      # conn
-      # |> put_status(:ok)
-      # |> json(%{fourletters: fourletters, messages: messages})
-      # |> put_status(:ok)
-      # |> redirect(to: "/#{fourletters}")
+      pid = get_pid(fourletters)
+      ABCD.Fourletters.put(pid, message)
       redirect(conn, to: "/#{fourletters}")
-      # |> render("four.html", fourletters: fourletters, messages: messages)
     else
       redirect(conn, to: "/#{fourletters}")
     end
   end
 
-  def apiaddletters(conn, _params) do
-    %{params: params} = conn
-    %{"fourletters" => fourletters, "message" => message} = params
-    if String.length(fourletters) == 4 and fourletters =~ ~r(^[a-z!]*$)  and String.length(message) < 101 do
+  def apiaddletters(conn, params) do
+    # how to consolidate with the other addletters?
 
+    %{"fourletters" => fourletters, "message" => message} = params
+    if valid_fourletters(fourletters) and String.length(message) < 101  do
       Logger.info fn ->
         "#{DateTime.utc_now} #{inspect(params)}"
       end
-      pid = case Supervisor.start_child(
-          Fourletters.Troll,
-          %{id: String.to_atom(fourletters), start: {ABCD.Fourletters, :start_link, [[]]}}) do
-        {:ok, pid} -> pid
-        {:error, {:already_started, pid}} -> pid
-      end
-      _ = ABCD.Fourletters.put(pid, message)
+      pid = get_pid(fourletters)
+      ABCD.Fourletters.put(pid, message)
       messages = ABCD.Fourletters.get(pid)
       conn
       |> put_status(:created)
       |> json(%{fourletters: fourletters, messages: messages})
-      # |> put_status(:ok)
       |> redirect(to: "/#{fourletters}")
-      # redirect(conn, to: "/#{fourletters}")
-      # |> render("four.html", fourletters: fourletters, messages: messages)
     else
       json(conn, %{error: message})
     end
   end
 
-  def apiclearletters(conn, _params) do
-    %{params: params} = conn
+  def clearletters(conn, params) do
     %{"fourletters" => fourletters} = params
-    if String.length(fourletters) == 4 and fourletters =~ ~r(^[a-z!]*$)  do
-      pid = case Supervisor.start_child(
-          Fourletters.Troll,
-          %{id: String.to_atom(fourletters), start: {ABCD.Fourletters, :start_link, [[]]}}) do
-        {:ok, pid} -> pid
-        {:error, {:already_started, pid}} -> pid
-      end
-      ABCD.Fourletters.clear(pid)
-      # conn
-      # |> put_status(:ok)
-      # |> json(%{fourletters: fourletters, clear: true})
-      # |> redirect(to: "/#{fourletters}")
-      # redirect(conn, to: "/redirect_test")
-      redirect(conn, to: "/#{fourletters}")
-    end
-  end
-
-  def clearletters(conn, _params) do
-    %{params: params} = conn
-    %{"fourletters" => fourletters} = params
-    if String.length(fourletters) == 4 and fourletters =~ ~r(^[a-z!]*$)  do
-      pid = case Supervisor.start_child(
-          Fourletters.Troll,
-          %{id: String.to_atom(fourletters), start: {ABCD.Fourletters, :start_link, [[]]}}) do
-        {:ok, pid} -> pid
-        {:error, {:already_started, pid}} -> pid
-      end
+    if valid_fourletters(fourletters) do
+      pid = get_pid(fourletters)
       ABCD.Fourletters.clear(pid)
-      # conn
-      # |> put_status(:ok)
-      # redirect(conn, to: "/redirect_test")
       redirect(conn, to: "/#{fourletters}")
     end
   end
 
   def nothing(conn, %{"fourletters" => fourletters}) do
-    if String.length(fourletters) == 4 and fourletters =~ ~r(^[a-z!]*$)  do
+    if valid_fourletters(fourletters) do
       redirect(conn, to: "/#{fourletters}")
     else
       render(conn, "nothing.html")

+ 1 - 1
lib/fourletters_web/router.ex

@@ -29,7 +29,7 @@ defmodule FourlettersWeb.Router do
 
     post "/api/:fourletters", FourlettersController, :apiaddletters
 
-    delete "/api/:fourletters", FourlettersController, :apiclearletters
+    delete "/api/:fourletters", FourlettersController, :clearletters
 
     # for stupid html forms that dont do DELETE
     post "/api/clear/:fourletters", FourlettersController, :clearletters

+ 0 - 1
lib/fourletters_web/templates/fourletters/show.html.eex

@@ -1 +0,0 @@
-<h2><%= @messenger %> would like to speak</h2>

+ 16 - 0
test/fourletters_web/controllers/fourletters_controller_test.exs

@@ -0,0 +1,16 @@
+defmodule FourlettersWeb.FourlettersControllerTest do
+  use FourlettersWeb.ConnCase
+
+  # test "GET /", %{conn: conn} do
+  #   conn = get(conn, "/")
+  #   assert html_response(conn, 200) =~ "4-letters.net"
+  # end
+
+  test "GET /?fourletters=test", %{conn: conn} do
+    conn = get(conn, "/?fourletters=test")
+    assert html_response(conn, 302) =~ "redirected"
+    assert %{fourletters: "test"} = redirected_params(conn)
+    assert redirected_to(conn) == "/test"
+  end
+
+end

+ 4 - 4
test/fourletters_web/controllers/page_controller_test.exs

@@ -1,8 +1,8 @@
 defmodule FourlettersWeb.PageControllerTest do
   use FourlettersWeb.ConnCase
 
-  test "GET /", %{conn: conn} do
-    conn = get(conn, "/")
-    assert html_response(conn, 200) =~ "Welcome to Phoenix!"
-  end
+  # test "GET /", %{conn: conn} do
+  #   conn = get(conn, "/")
+  #   assert html_response(conn, 200) =~ "4-letters.net"
+  # end
 end