-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathviewer_plug.ex
69 lines (54 loc) · 1.58 KB
/
viewer_plug.ex
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
defmodule Augur.ViewerPlug do
@moduledoc """
View cached messages in a familiar interface
Forward requests to this plug from your router:
```
if Mix.env() == :dev do
forward("/sms/sent", Augur.ViewerPlug)
end
```
"""
use Plug.Router
require EEx
alias Augur.Config
alias Augur.Cache
index_template = Path.join(__DIR__, "templates/index.html.eex")
EEx.function_from_file(:defp, :index, index_template, [:assigns])
thread_template = Path.join(__DIR__, "templates/thread.html.eex")
EEx.function_from_file(:defp, :thread, thread_template, [:assigns])
plug(:match)
plug(:dispatch)
get("/") do
config = Config.get()
threads = Cache.threads(config.cache)
assigns = %{
conn: conn,
base_path: base_path(conn),
threads: threads
}
conn
|> Plug.Conn.put_resp_content_type("text/html")
|> send_resp(200, index(assigns))
end
get("/threads/:id") do
config = Config.get()
thread = Cache.thread(config.cache, conn.params["id"])
assigns = %{
conn: conn,
base_path: base_path(conn),
thread: thread
}
conn
|> Plug.Conn.put_resp_content_type("text/html")
|> send_resp(200, thread(assigns))
end
defp base_path(%{script_name: script_name}) do
"/" <> Enum.join(script_name, "/")
end
@doc false
def format_phone_number(phone_number) do
regex = ~r/^(\+1)?(?<area_code>\d{3})(?<exchange_code>\d{3})(?<line_number>\d{4})/
captures = Regex.named_captures(regex, phone_number)
"(#{captures["area_code"]}) #{captures["exchange_code"]}-#{captures["line_number"]}"
end
end