plausible_api 0.2 → 0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 77c960fb14e022fa7f1ae47a7c38903325d188d98c67ac598977812326b616be
4
- data.tar.gz: 4d0ed7d883293c49bfb518d544efbad968b544d249cb0bfc05ff5366c87a325e
3
+ metadata.gz: 3164447cd3eac3c7a7cbf0c7e7cccd36eaf2bd195966283fc0da4ad023b7b21e
4
+ data.tar.gz: 34acbb3e21f80bd3537d4273b6555dea0da7e4fdb5cf3dcbf8bede5d477e8ce0
5
5
  SHA512:
6
- metadata.gz: 6247be2a8f503c75bbff787771f1189598426171275122b6dddcc6581354db6ea3e2f81dd5c0ccc247c92ab7587f5f00c32ccc3d83b050ab70ff1470866bbbcb
7
- data.tar.gz: 395a7b263fcfb9ce374f7e2d734a3d2ed608fd11755c4a6087fabc89a299da084fe5411063707e7066371660c1784ca2fa900d434d0daaefe8f00c398974dd7e
6
+ metadata.gz: 03273d140652347b6b282d94ff0c6294746fc7bd410dcaa1feab54a627d80ed64fa8eb1c9c56c0a3a0e3e2a95cb9cda755501987f46a3b795cb9dc2862aa1e01
7
+ data.tar.gz: b6e84673b3763918a28488c717e3be089107571ecb1b357b4d7e2b1ded589b68eade9d65f615e70a6655b4571a73c7bf767a0af3363b6ca90631cd0810ea91e8
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- plausible_api (0.2)
4
+ plausible_api (0.3)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -87,6 +87,18 @@ c.event({
87
87
  })
88
88
  ```
89
89
 
90
+
91
+ ### Self-hosted Plausible instances
92
+
93
+ If you are using a self-hosted Plausible instance, you can set the `base_url` before initializing the client. On a Ruby on Rails app, you can add this to an initializer like `config/initializers/plausible.rb`
94
+
95
+ ```rb
96
+ # Do not include a trailing slash
97
+ PlausibleApi.configure do |config|
98
+ config.base_url = "https://your-plausible-instance.com"
99
+ end
100
+ ```
101
+
90
102
  ## Development
91
103
 
92
104
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -15,8 +15,6 @@ require "cgi"
15
15
 
16
16
  module PlausibleApi
17
17
  class Client
18
- BASE_URL = "https://plausible.io"
19
-
20
18
  def initialize(site_id, token)
21
19
  @site_id = site_id.to_s
22
20
  @token = token.to_s
@@ -54,9 +52,10 @@ module PlausibleApi
54
52
  SUCCESS_CODES = %w[200 202].freeze
55
53
 
56
54
  def call(api)
57
- raise StandardError.new api.errors unless api.valid?
55
+ raise Error, api.errors unless api.valid?
56
+ raise ConfigurationError, PlausibleApi.configuration.errors unless PlausibleApi.configuration.valid?
58
57
 
59
- url = "#{BASE_URL}#{api.request_url.gsub("$SITE_ID", @site_id)}"
58
+ url = "#{PlausibleApi.configuration.base_url}#{api.request_url.gsub("$SITE_ID", @site_id)}"
60
59
  uri = URI.parse(url)
61
60
 
62
61
  req = api.request_class.new(uri.request_uri)
@@ -0,0 +1,26 @@
1
+ module PlausibleApi
2
+ class Configuration
3
+ attr_accessor :base_url
4
+
5
+ # Setting up default values
6
+ def initialize
7
+ @base_url = "https://plausible.io"
8
+ end
9
+
10
+ def valid?
11
+ errors.empty?
12
+ end
13
+
14
+ def errors
15
+ errors = []
16
+ if base_url.nil? || base_url.empty?
17
+ errors.push(base_url: "base_url is required")
18
+ elsif !(URI.parse base_url).is_a? URI::HTTP
19
+ errors.push(base_url: "base_url is not a valid URL")
20
+ elsif base_url.end_with?("/")
21
+ errors.push(base_url: "base_url should not end with a trailing slash")
22
+ end
23
+ errors
24
+ end
25
+ end
26
+ end
@@ -5,11 +5,11 @@ module PlausibleApi
5
5
  class Aggregate < Base
6
6
 
7
7
  def initialize(options = {})
8
- super({ period: '30d',
8
+ super({ period: '30d',
9
9
  metrics: 'visitors,visits,pageviews,views_per_visit,bounce_rate,visit_duration,events' }
10
10
  .merge(options))
11
11
  end
12
-
12
+
13
13
  def request_url_base
14
14
  "/api/v1/stats/aggregate?site_id=$SITE_ID"
15
15
  end
@@ -7,7 +7,7 @@ module PlausibleApi
7
7
  def initialize(options = {})
8
8
  super({ period: '30d', property: 'event:page' }.merge(options))
9
9
  end
10
-
10
+
11
11
  def request_url_base
12
12
  "/api/v1/stats/breakdown?site_id=$SITE_ID"
13
13
  end
@@ -17,4 +17,4 @@ module PlausibleApi
17
17
  end
18
18
  end
19
19
  end
20
- end
20
+ end
@@ -7,7 +7,7 @@ module PlausibleApi
7
7
  def initialize(options = {})
8
8
  super({ period: '30d' }.merge(options))
9
9
  end
10
-
10
+
11
11
  def request_url_base
12
12
  "/api/v1/stats/timeseries?site_id=$SITE_ID"
13
13
  end
@@ -17,4 +17,4 @@ module PlausibleApi
17
17
  end
18
18
  end
19
19
  end
20
- end
20
+ end
@@ -1,3 +1,3 @@
1
1
  module PlausibleApi
2
- VERSION = "0.2"
2
+ VERSION = "0.3"
3
3
  end
data/lib/plausible_api.rb CHANGED
@@ -1,7 +1,20 @@
1
1
  require "plausible_api/version"
2
2
  require "plausible_api/client"
3
+ require "plausible_api/configuration"
3
4
 
4
5
  module PlausibleApi
5
6
  class Error < StandardError; end
7
+
8
+ class ConfigurationError < StandardError; end
9
+
6
10
  # Your code goes here...
11
+ class << self
12
+ def configuration
13
+ @configuration ||= Configuration.new
14
+ end
15
+
16
+ def configure
17
+ yield(configuration)
18
+ end
19
+ end
7
20
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: plausible_api
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.2'
4
+ version: '0.3'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gustavo Garcia
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-12-23 00:00:00.000000000 Z
11
+ date: 2024-02-02 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: A very humble wrapper for the new API by Plausible
14
14
  email:
@@ -30,6 +30,7 @@ files:
30
30
  - examples/event.rb
31
31
  - lib/plausible_api.rb
32
32
  - lib/plausible_api/client.rb
33
+ - lib/plausible_api/configuration.rb
33
34
  - lib/plausible_api/event/base.rb
34
35
  - lib/plausible_api/event/post.rb
35
36
  - lib/plausible_api/stats/aggregate.rb