-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathhits.ex
59 lines (46 loc) · 1.62 KB
/
hits.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
defmodule Hits do
@moduledoc """
Hits keeps the contexts that define your domain
and business logic.
Contexts are also responsible for managing your data, regardless
if it comes from the database, an external API or others.
"""
@doc """
svg_badge_template/0 opens the SVG template file.
the function is single-purpose so that the template is cached.
returns String of template.
"""
def svg_badge_template do
# Want to help optimse this? See: https://github.com/dwyl/hits/issues/70
File.read!("./lib/hits_web/templates/hit/badge.svg")
end
@doc """
make_badge/1 from svg template substituting the count value
## Parameters
- count: Number the view/hit count to be displayed in the badge.
Returns the badge XML with the count.
"""
def make_badge(count \\ 1) do
String.replace(svg_badge_template(), ~r/{count}/, to_string(count))
# stackoverflow.com/a/1084759
|> String.replace(~r/<!--(.*?)-->/, "")
end
@doc """
get_user_agent_string/1 extracts user-agent, IP address and browser language
from the Plug.Conn map see: https://hexdocs.pm/plug/Plug.Conn.html
> there is probably a *much* better way of doing this ... PR v. welcome!
## Parameters
- conn: Map the standard Plug.Conn info see: hexdocs.pm/plug/Plug.Conn.html
Returns String with user-agent
"""
def get_user_agent_string(conn) do
# TODO: sanitise useragent string https://github.com/dwyl/fields/issues/19
# extract user-agent from conn.req_headers:
[{_, ua}] =
Enum.filter(conn.req_headers, fn {k, _} ->
k == "user-agent"
end)
# IO.inspect(ua, label: "ua")
ua
end
end