Skip to content

Commit 695f2f3

Browse files
committed
First commit
0 parents  commit 695f2f3

File tree

10 files changed

+278
-0
lines changed

10 files changed

+278
-0
lines changed

.formatter.exs

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[
2+
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"],
3+
locals_without_parens: [plug: 1, plug: 2]
4+
]

.gitignore

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# The directory Mix will write compiled artifacts to.
2+
/_build/
3+
4+
# If you run "mix test --cover", coverage assets end up here.
5+
/cover/
6+
7+
# The directory Mix downloads your dependencies sources to.
8+
/deps/
9+
10+
# Where third-party dependencies like ExDoc output generated docs.
11+
/doc/
12+
13+
# Ignore .fetch files in case you like to edit your project deps locally.
14+
/.fetch
15+
16+
# If the VM crashes, it generates a dump, let's ignore it too.
17+
erl_crash.dump
18+
19+
# Also ignore archive artifacts (built via "mix archive.build").
20+
*.ez
21+
22+
# Ignore package tarball (built via "mix hex.build").
23+
posthog-*.tar
24+
25+
config

LICENSE

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Copyright © 2020 White Paper Clip, Inc.
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4+
5+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6+
7+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Posthog
2+
3+
This library provides an HTTP client for Posthog.
4+
5+
## Installation
6+
7+
The package can be installed by adding `posthog` to your list of dependencies in `mix.exs`:
8+
9+
```elixir
10+
def deps do
11+
[
12+
{:posthog, "~> 0.1"}
13+
]
14+
end
15+
```
16+
17+
## Configuration
18+
19+
```elixir
20+
config :posthog,
21+
api_url: "http://posthog.example.com",
22+
api_key: "..."
23+
```
24+
25+
Optionally, you can pass in a `:json_library` key. The default JSON parser is Jason.
26+
27+
## Usage
28+
29+
Capturing events:
30+
31+
```elixir
32+
Posthog.capture("login", distinct_id: user.id)
33+
```
34+
35+
Capturing multiple events:
36+
37+
```elixir
38+
Posthog.batch([{"login", [distinct_id: user.id], nil}])
39+
```

lib/posthog.ex

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
defmodule Posthog do
2+
@moduledoc """
3+
This module provides an Elixir HTTP client for Posthog.
4+
5+
Example config:
6+
7+
config :posthog,
8+
api_url: "http://posthog.example.com",
9+
api_key: "..."
10+
11+
Optionally, you can pass in a `:json_library` key. The default JSON parser
12+
is Jason.
13+
"""
14+
15+
@doc """
16+
Sends a capture event. `distinct_id` is the only required parameter.
17+
18+
## Examples
19+
20+
iex> Posthog.capture("login", distinct_id: user.id)
21+
:ok
22+
iex> Posthog.capture("login", [distinct_id: user.id], DateTime.utc_now())
23+
:ok
24+
25+
"""
26+
@typep result() :: {:ok, term()} | {:error, term()}
27+
@typep timestamp() :: DateTime.t() | NaiveDateTime.t() | String.t() | nil
28+
29+
@spec capture(atom() | String.t(), keyword() | map(), timestamp()) :: result()
30+
defdelegate capture(event, params, timestamp \\ nil), to: Posthog.Client
31+
32+
@spec batch(list(tuple())) :: result()
33+
defdelegate batch(events), to: Posthog.Client
34+
end

lib/posthog/client.ex

+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
defmodule Posthog.Client do
2+
@moduledoc false
3+
4+
def capture(event, params, timestamp) when is_bitstring(event) or is_atom(event) do
5+
body = build_event(event, params, timestamp)
6+
7+
post!("/capture", body)
8+
end
9+
10+
def batch(events) when is_list(events) do
11+
body =
12+
for {event, params, timestamp} <- events do
13+
build_event(event, params, timestamp)
14+
end
15+
16+
body = %{batch: body}
17+
18+
post!("/capture", body)
19+
end
20+
21+
defp build_event(event, properties, timestamp) do
22+
%{event: to_string(event), properties: Map.new(properties), timestamp: timestamp}
23+
end
24+
25+
defp post!(path, %{} = body) do
26+
body =
27+
body
28+
|> Map.put(:api_key, api_key())
29+
|> json_library().encode!()
30+
31+
api_url()
32+
|> URI.merge(path)
33+
|> URI.to_string()
34+
|> :hackney.post([{"Content-Type", "application/json"}], body)
35+
|> handle()
36+
end
37+
38+
@spec handle(tuple()) :: {:ok, term()} | {:error, term()}
39+
defp handle({:ok, status, _headers, _ref} = resp) when div(status, 100) == 2 do
40+
{:ok, to_response(resp)}
41+
end
42+
43+
defp handle({:ok, _status, _headers, _ref} = resp) do
44+
{:error, to_response(resp)}
45+
end
46+
47+
defp handle({:error, _} = result) do
48+
result
49+
end
50+
51+
defp to_response({_, status, headers, ref}) do
52+
response = %{status: status, headers: headers, body: nil}
53+
54+
with {:ok, body} <- :hackney.body(ref),
55+
{:ok, json} <- json_library().decode(body) do
56+
%{response | body: json}
57+
else
58+
_ -> response
59+
end
60+
end
61+
62+
defp api_url() do
63+
case Application.get_env(:posthog, :api_url) do
64+
url when is_bitstring(url) ->
65+
url
66+
67+
term ->
68+
raise """
69+
Expected a string API URL, got: #{inspect(term)}. Set a
70+
URL and key in your config:
71+
72+
config :posthog,
73+
api_url: "https://posthog.example.com",
74+
api_key: "my-key"
75+
"""
76+
end
77+
end
78+
79+
defp api_key() do
80+
case Application.get_env(:posthog, :api_key) do
81+
key when is_bitstring(key) ->
82+
key
83+
84+
term ->
85+
raise """
86+
Expected a string API key, got: #{inspect(term)}. Set a
87+
URL and key in your config:
88+
89+
config :posthog,
90+
api_url: "https://posthog.example.com",
91+
api_key: "my-key"
92+
"""
93+
end
94+
end
95+
96+
defp json_library() do
97+
Application.get_env(:posthog, :json_library, Jason)
98+
end
99+
end

mix.exs

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
defmodule Posthog.MixProject do
2+
use Mix.Project
3+
4+
def project do
5+
[
6+
app: :posthog,
7+
version: "0.1.0",
8+
elixir: "~> 1.8",
9+
start_permanent: Mix.env() == :prod,
10+
deps: deps(),
11+
description: description(),
12+
package: package()
13+
]
14+
end
15+
16+
def application do
17+
[]
18+
end
19+
20+
defp description do
21+
"""
22+
Elixir HTTP client for Posthog.
23+
"""
24+
end
25+
26+
defp package do
27+
[
28+
name: :posthog,
29+
files: ["lib", "mix.exs", "README.md", "LICENSE"],
30+
maintainers: ["Nick Kezhaya"],
31+
licenses: ["MIT"],
32+
links: %{"GitHub" => "https://github.com/whitepaperclip/posthog"}
33+
]
34+
end
35+
36+
defp deps do
37+
[
38+
{:hackney, "~> 1.10"},
39+
{:jason, "~> 1.2", optional: true},
40+
{:ex_doc, ">= 0.0.0", only: [:doc]}
41+
]
42+
end
43+
end

mix.lock

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
%{
2+
"certifi": {:hex, :certifi, "2.5.2", "b7cfeae9d2ed395695dd8201c57a2d019c0c43ecaf8b8bcb9320b40d6662f340", [:rebar3], [{:parse_trans, "~>3.3", [hex: :parse_trans, repo: "hexpm", optional: false]}], "hexpm", "3b3b5f36493004ac3455966991eaf6e768ce9884693d9968055aeeeb1e575040"},
3+
"earmark": {:hex, :earmark, "1.4.5", "62ffd3bd7722fb7a7b1ecd2419ea0b458c356e7168c1f5d65caf09b4fbdd13c8", [:mix], [], "hexpm", "b7d0e6263d83dc27141a523467799a685965bf8b13b6743413f19a7079843f4f"},
4+
"ex_doc": {:hex, :ex_doc, "0.22.1", "9bb6d51508778193a4ea90fa16eac47f8b67934f33f8271d5e1edec2dc0eee4c", [:mix], [{:earmark, "~> 1.4.0", [hex: :earmark, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.14", [hex: :makeup_elixir, repo: "hexpm", optional: false]}], "hexpm", "d957de1b75cb9f78d3ee17820733dc4460114d8b1e11f7ee4fd6546e69b1db60"},
5+
"hackney": {:hex, :hackney, "1.16.0", "5096ac8e823e3a441477b2d187e30dd3fff1a82991a806b2003845ce72ce2d84", [:rebar3], [{:certifi, "2.5.2", [hex: :certifi, repo: "hexpm", optional: false]}, {:idna, "6.0.1", [hex: :idna, repo: "hexpm", optional: false]}, {:metrics, "1.0.1", [hex: :metrics, repo: "hexpm", optional: false]}, {:mimerl, "~>1.1", [hex: :mimerl, repo: "hexpm", optional: false]}, {:parse_trans, "3.3.0", [hex: :parse_trans, repo: "hexpm", optional: false]}, {:ssl_verify_fun, "1.1.6", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}], "hexpm", "3bf0bebbd5d3092a3543b783bf065165fa5d3ad4b899b836810e513064134e18"},
6+
"idna": {:hex, :idna, "6.0.1", "1d038fb2e7668ce41fbf681d2c45902e52b3cb9e9c77b55334353b222c2ee50c", [:rebar3], [{:unicode_util_compat, "0.5.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "a02c8a1c4fd601215bb0b0324c8a6986749f807ce35f25449ec9e69758708122"},
7+
"jason": {:hex, :jason, "1.2.1", "12b22825e22f468c02eb3e4b9985f3d0cb8dc40b9bd704730efa11abd2708c44", [:mix], [{:decimal, "~> 1.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "b659b8571deedf60f79c5a608e15414085fa141344e2716fbd6988a084b5f993"},
8+
"makeup": {:hex, :makeup, "1.0.2", "0b9f7bfb7a88bed961341b359bc2cc1b233517af891ba4890ec5a580ffe738b4", [:mix], [{:nimble_parsec, "~> 0.5", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "43833299231c6a6983afc75a34e43eeba638521d5527ff89809fa6372424fd7e"},
9+
"makeup_elixir": {:hex, :makeup_elixir, "0.14.1", "4f0e96847c63c17841d42c08107405a005a2680eb9c7ccadfd757bd31dabccfb", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "f2438b1a80eaec9ede832b5c41cd4f373b38fd7aa33e3b22d9db79e640cbde11"},
10+
"metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [:rebar3], [], "hexpm", "69b09adddc4f74a40716ae54d140f93beb0fb8978d8636eaded0c31b6f099f16"},
11+
"mime": {:hex, :mime, "1.3.1", "30ce04ab3175b6ad0bdce0035cba77bba68b813d523d1aac73d9781b4d193cf8", [:mix], [], "hexpm", "6cbe761d6a0ca5a31a0931bf4c63204bceb64538e664a8ecf784a9a6f3b875f1"},
12+
"mimerl": {:hex, :mimerl, "1.2.0", "67e2d3f571088d5cfd3e550c383094b47159f3eee8ffa08e64106cdf5e981be3", [:rebar3], [], "hexpm", "f278585650aa581986264638ebf698f8bb19df297f66ad91b18910dfc6e19323"},
13+
"nimble_parsec": {:hex, :nimble_parsec, "0.6.0", "32111b3bf39137144abd7ba1cce0914533b2d16ef35e8abc5ec8be6122944263", [:mix], [], "hexpm", "27eac315a94909d4dc68bc07a4a83e06c8379237c5ea528a9acff4ca1c873c52"},
14+
"parse_trans": {:hex, :parse_trans, "3.3.0", "09765507a3c7590a784615cfd421d101aec25098d50b89d7aa1d66646bc571c1", [:rebar3], [], "hexpm", "17ef63abde837ad30680ea7f857dd9e7ced9476cdd7b0394432af4bfc241b960"},
15+
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.6", "cf344f5692c82d2cd7554f5ec8fd961548d4fd09e7d22f5b62482e5aeaebd4b0", [:make, :mix, :rebar3], [], "hexpm", "bdb0d2471f453c88ff3908e7686f86f9be327d065cc1ec16fa4540197ea04680"},
16+
"tesla": {:hex, :tesla, "1.3.3", "26ae98627af5c406584aa6755ab5fc96315d70d69a24dd7f8369cfcb75094a45", [:mix], [{:castore, "~> 0.1", [hex: :castore, repo: "hexpm", optional: true]}, {:exjsx, ">= 3.0.0", [hex: :exjsx, repo: "hexpm", optional: true]}, {:fuse, "~> 2.4", [hex: :fuse, repo: "hexpm", optional: true]}, {:gun, "~> 1.3", [hex: :gun, repo: "hexpm", optional: true]}, {:hackney, "~> 1.6", [hex: :hackney, repo: "hexpm", optional: true]}, {:ibrowse, "~> 4.4.0", [hex: :ibrowse, repo: "hexpm", optional: true]}, {:jason, ">= 1.0.0", [hex: :jason, repo: "hexpm", optional: true]}, {:mime, "~> 1.0", [hex: :mime, repo: "hexpm", optional: false]}, {:mint, "~> 1.0", [hex: :mint, repo: "hexpm", optional: true]}, {:poison, ">= 1.0.0", [hex: :poison, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4", [hex: :telemetry, repo: "hexpm", optional: true]}], "hexpm", "2648f1c276102f9250299e0b7b57f3071c67827349d9173f34c281756a1b124c"},
17+
"unicode_util_compat": {:hex, :unicode_util_compat, "0.5.0", "8516502659002cec19e244ebd90d312183064be95025a319a6c7e89f4bccd65b", [:rebar3], [], "hexpm", "d48d002e15f5cc105a696cf2f1bbb3fc72b4b770a184d8420c8db20da2674b38"},
18+
}

test/posthog_test.exs

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
defmodule PosthogTest do
2+
use ExUnit.Case
3+
doctest Posthog
4+
5+
test "greets the world" do
6+
assert Posthog.hello() == :world
7+
end
8+
end

test/test_helper.exs

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ExUnit.start()

0 commit comments

Comments
 (0)