Skip to content

Commit a105b03

Browse files
committed
Add PlausibleClient;
1 parent 66c9b9a commit a105b03

File tree

3 files changed

+119
-0
lines changed

3 files changed

+119
-0
lines changed

app/clients/plausible_client.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# frozen_string_literal: true
2+
3+
# https://plausible.io/docs/events-api#endpoints
4+
#
5+
# curl -i -X POST https://plausible.io/api/event \
6+
# -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36 OPR/71.0.3770.284' \
7+
# -H 'X-Forwarded-For: 127.0.0.1' \
8+
# -H 'Content-Type: application/json' \
9+
# --data '{"name":"pageview","url":"http://dummy.site","domain":"dummy.site"}'
10+
11+
class PlausibleClient < ApplicationClient
12+
BASE_URI = "https://plausible.io/api"
13+
14+
def post_event(name:, url:, domain: "joyofrails.com", headers: {})
15+
post(
16+
"/event",
17+
body: {
18+
name:,
19+
url:,
20+
domain:
21+
},
22+
headers: headers.reverse_merge("Content-Type" => "application/json")
23+
)
24+
end
25+
end

spec/cassettes/PlausibleClient/_post_event/posts_an_event_to_Plausible.yml

Lines changed: 74 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spec/clients/plausible_client_spec.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
require "rails_helper"
2+
3+
RSpec.describe PlausibleClient, vcr: true do
4+
subject(:client) { described_class.new }
5+
6+
describe "#post_event" do
7+
it "posts an event to Plausible" do
8+
response = client.post_event(
9+
name: "test event",
10+
url: "http://joyofrails.com/about",
11+
headers: {
12+
"User-Agent" => "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36 OPR/71.0.3770.284",
13+
"X-Forwarded-For" => "127.0.0.1"
14+
}
15+
)
16+
17+
expect(response.code.to_i).to eq(202)
18+
end
19+
end
20+
end

0 commit comments

Comments
 (0)