about summary refs log tree commit diff
path: root/users/wpcarro/assessments/semiprimes/server/lib/router.ex
blob: cb55520920de17588eeb87a1e1c23463a764881e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
defmodule Router do
  use Plug.Router
  use Plug.Debugger
  require Logger

  plug(Plug.Logger, log: :debug)
  plug(Plug.Parsers, parsers: [:urlencoded])
  plug(:match)
  plug(:dispatch)

  @usage """
  Usage: Try querying some of the following endpoints...
    GET /
    GET /help
    GET /semiprime?number=<integer>
    GET /semiprimes?numbers=<comma-separated-integers>
  """

  get "/" do
    send_resp(conn, 200, "Welcome to Semiprimes Service!\n\n#{@usage}")
  end

  get "/help" do
    send_resp(conn, 200, @usage)
  end

  get "/semiprime" do
    case conn |> Map.get(:query_params) |> Map.get("number") do
      nil ->
        send_resp(conn, 400, "You must pass an integer as a query parameter. #{@usage}")

      val ->
        case Integer.parse(val) do
          {n, ""} ->
            send_resp(conn, 200, semiprime_response(n))

          _ ->
            send_resp(conn, 400, "We could not parse the number you provided.\n\n#{@usage}")
        end
    end
  end

  get "/semiprimes" do
    case conn |> Map.get(:query_params) |> Map.get("numbers") do
      nil ->
        send_resp(
          conn,
          400,
          "You must pass a comma-separated list of integers as a query parameter.\n\n#{@usage}"
        )

      xs ->
        response =
          xs
          |> String.split(",")
          |> Stream.map(&Integer.parse/1)
          |> Stream.filter(fn
            {n, ""} -> true
            _ -> false
          end)
          |> Stream.map(fn {n, ""} -> semiprime_response(n) end)
          |> Enum.join("\n")

        send_resp(conn, 200, response)
    end
  end

  match _ do
    send_resp(conn, 404, "Not found.")
  end

  ################################################################################
  # Utils
  ################################################################################

  defp semiprime_response(n) do
    case Server.semiprime(n) do
      nil ->
        "#{n} is not a semiprime. Try another number!"

      {hit_or_miss, factors} ->
        response = "#{n} is a semiprime! Its factors are #{Enum.join(factors, " and ")}."
        "Cache #{Atom.to_string(hit_or_miss)} - #{response}"
    end
  end
end