pirsch_api 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +9 -0
- data/Gemfile.lock +1 -1
- data/README.md +35 -3
- data/lib/pirsch_api/client.rb +45 -12
- data/lib/pirsch_api/error.rb +4 -0
- data/lib/pirsch_api/object.rb +19 -0
- data/lib/pirsch_api/objects/active.rb +4 -0
- data/lib/pirsch_api/objects/domain.rb +4 -0
- data/lib/pirsch_api/objects/event.rb +4 -0
- data/lib/pirsch_api/objects/growth.rb +4 -0
- data/lib/pirsch_api/objects/page.rb +4 -0
- data/lib/pirsch_api/objects/referrer.rb +4 -0
- data/lib/pirsch_api/objects/token.rb +4 -0
- data/lib/pirsch_api/objects/utm_campaign.rb +4 -0
- data/lib/pirsch_api/objects/utm_medium.rb +4 -0
- data/lib/pirsch_api/objects/utm_source.rb +4 -0
- data/lib/pirsch_api/objects/visitor.rb +4 -0
- data/lib/pirsch_api/resources/base.rb +57 -0
- data/lib/pirsch_api/resources/domain.rb +11 -0
- data/lib/pirsch_api/resources/statistics/active.rb +11 -0
- data/lib/pirsch_api/resources/statistics/event.rb +10 -0
- data/lib/pirsch_api/resources/statistics/growth.rb +11 -0
- data/lib/pirsch_api/resources/statistics/page.rb +11 -0
- data/lib/pirsch_api/resources/statistics/referrer.rb +11 -0
- data/lib/pirsch_api/resources/statistics/utm_campaign.rb +11 -0
- data/lib/pirsch_api/resources/statistics/utm_medium.rb +11 -0
- data/lib/pirsch_api/resources/statistics/utm_source.rb +11 -0
- data/lib/pirsch_api/resources/statistics/visitor.rb +11 -0
- data/lib/pirsch_api/{token.rb → resources/token.rb} +8 -6
- data/lib/pirsch_api/version.rb +1 -1
- data/lib/pirsch_api.rb +33 -2
- metadata +28 -6
- data/pkg/plausible_api-0.1.0.gem +0 -0
- data/pkg/plausible_api-0.1.7.gem +0 -0
- data/pkg/plausible_api-0.1.8.gem +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f23f5623a3fd98b5f09a7190b83e86a9f8a7fe4f4403ba3da9387f2fe98cc3a1
|
4
|
+
data.tar.gz: 6137f3417cd67bffbe7be5d48fc5cfcd963d5b650f0998a1ce75da3c8547f285
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0755236fcf745ccf413778bddb08e14c71330dddfbc1f1cb98991e29d708a1ec5d4a714516d452fc42d5cc032a662eb1ad1ff90fc4d74abd98ef40c0a3f1583b
|
7
|
+
data.tar.gz: 889498694ba2e82c6fae0fa31e48e21ce09ef538ee02c25112a8ff8388dfd142ce2b8ac2d534924909f2d99a27c790cab19ab55a15f4366c42682f6bb8f0b7eb
|
data/.gitignore
ADDED
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -9,14 +9,46 @@ gem 'pirsch_api'
|
|
9
9
|
```
|
10
10
|
Then you need to initialize a Client with your `client_id` and the `client_secret`.
|
11
11
|
```rb
|
12
|
-
c = PirschApi::Client.new('<client_id>', '<client_secret>')
|
12
|
+
c = PirschApi::Client.new(client_id: '<client_id>', client_secret: '<client_secret>')
|
13
13
|
|
14
14
|
# Test if the credentials are valid
|
15
15
|
c.valid?
|
16
16
|
=> true
|
17
17
|
|
18
|
-
# Get
|
19
|
-
c.
|
18
|
+
# Get /domains
|
19
|
+
c.domains
|
20
|
+
=> [#<PirschApi::Domain id="0D3a...", user_id="0D3a...", hostname="pirsch.io", subdomain="pirsch", ... >]
|
21
|
+
c.domains.first.hostname
|
22
|
+
=> "pirsch.io"
|
23
|
+
|
24
|
+
# You will need one domain for all the following requests
|
25
|
+
d = c.domains.first
|
26
|
+
|
27
|
+
# All the following methods require a `domain` and can receive a `filters` hash
|
28
|
+
|
29
|
+
# Get /statistics/visitor
|
30
|
+
c.visitors(domain: d)
|
31
|
+
=> [#<PirschApi::Visitor day="2021-08-30T00:00:00Z", visitors=87, views=200, sessions=125, bounces=53, bounce_rate=0.6091954022988506>, #<PirschApi::Visitor day="2021-08-31T00:00:00Z", visitors=77, views=159, sessions=113, bounces=49, bounce_rate=0.6363636363636364>, #<PirschApi::Visitor day="2021-09-01T00:00:00Z", visitors=76, views=161, sessions=110, bounces=48, bounce_rate=0.631578947368421>, #<PirschApi::Visitor day="2021-09-02T00:00:00Z", visitors=79, views=152, sessions=130, bounces=52, bounce_rate=0.6582278481012658>, #<PirschApi::Visitor day="2021-09-03T00:00:00Z", visitors=53, views=109, sessions=68, bounces=26, bounce_rate=0.49056603773584906>, #<PirschApi::Visitor day="2021-09-04T00:00:00Z", visitors=54, views=126, sessions=71, bounces=31, bounce_rate=0.5740740740740741>, #<PirschApi::Visitor day="2021-09-05T00:00:00Z", visitors=116, views=202, sessions=131, bounces=84, bounce_rate=0.7241379310344828>, #<PirschApi::Visitor day="2021-09-06T00:00:00Z", visitors=1, views=1, sessions=1, bounces=1, bounce_rate=1>]
|
32
|
+
c.visitors(domain: d, filters: { from: '2021-09-01' })
|
33
|
+
[#<PirschApi::Visitor day="2021-09-01T00:00:00Z", visitors=76, views=161, sessions=110, bounces=48, bounce_rate=0.631578947368421>, #<PirschApi::Visitor day="2021-09-02T00:00:00Z", visitors=79, views=152, sessions=130, bounces=52, bounce_rate=0.6582278481012658>, #<PirschApi::Visitor day="2021-09-03T00:00:00Z", visitors=53, views=109, sessions=68, bounces=26, bounce_rate=0.49056603773584906>, #<PirschApi::Visitor day="2021-09-04T00:00:00Z", visitors=54, views=126, sessions=71, bounces=31, bounce_rate=0.5740740740740741>, #<PirschApi::Visitor day="2021-09-05T00:00:00Z", visitors=116, views=202, sessions=131, bounces=84, bounce_rate=0.7241379310344828>, #<PirschApi::Visitor day="2021-09-06T00:00:00Z", visitors=1, views=1, sessions=1, bounces=1, bounce_rate=1>]
|
34
|
+
c.visitors(domain: d, filters: { from: '2021-09-01' }).first.day
|
35
|
+
=> "2021-09-01T00:00:00Z"
|
36
|
+
|
37
|
+
# Get /statistics/active
|
38
|
+
c.active(domain: d)
|
39
|
+
=> #<PirschApi::Active stats=nil, visitors=0>
|
40
|
+
c.active(domain: d).visitors
|
41
|
+
=> 0
|
42
|
+
|
43
|
+
# Other available methods (Remember that they can all receive a `filters` hash)
|
44
|
+
c.pages(domain: d)
|
45
|
+
c.referrers(domain: d)
|
46
|
+
c.active(domain: d)
|
47
|
+
c.growth(domain: d)
|
48
|
+
c.events(domain: d)
|
49
|
+
c.utm_source(domain: d)
|
50
|
+
c.utm_medium(domain: d)
|
51
|
+
c.utm_campaign(domain: d)
|
20
52
|
```
|
21
53
|
|
22
54
|
|
data/lib/pirsch_api/client.rb
CHANGED
@@ -1,28 +1,61 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require 'pirsch_api/token'
|
4
|
-
|
5
|
-
require 'json'
|
6
|
-
require "net/http"
|
7
|
-
require "uri"
|
8
|
-
require "cgi"
|
9
|
-
|
10
3
|
module PirschApi
|
11
4
|
class Client
|
12
5
|
|
13
|
-
BASE_URL = 'https://api.pirsch.io'
|
6
|
+
BASE_URL = 'https://api.pirsch.io/api/v1'
|
14
7
|
|
15
|
-
def initialize(client_id
|
8
|
+
def initialize(client_id:, client_secret:)
|
16
9
|
@client_id = client_id.to_s
|
17
10
|
@client_secret = client_secret.to_s
|
18
11
|
end
|
19
12
|
|
13
|
+
def valid?
|
14
|
+
true if token rescue false
|
15
|
+
end
|
16
|
+
|
20
17
|
def token
|
21
|
-
@token ||=
|
18
|
+
@token ||= TokenResource.new(@client_id, @client_secret).run
|
22
19
|
end
|
23
20
|
|
24
|
-
def
|
25
|
-
|
21
|
+
def domains
|
22
|
+
DomainResource.new(client: self).run
|
23
|
+
end
|
24
|
+
|
25
|
+
def visitors(filters: {}, domain:)
|
26
|
+
VisitorResource.new(client: self, filters: filters, domain: domain).run
|
27
|
+
end
|
28
|
+
|
29
|
+
def pages(filters: {}, domain:)
|
30
|
+
PageResource.new(client: self, filters: filters, domain: domain).run
|
31
|
+
end
|
32
|
+
|
33
|
+
def referrers(filters: {}, domain:)
|
34
|
+
ReferrerResource.new(client: self, filters: filters, domain: domain).run
|
35
|
+
end
|
36
|
+
|
37
|
+
def active(filters: {}, domain:)
|
38
|
+
ActiveResource.new(client: self, filters: filters, domain: domain).run
|
39
|
+
end
|
40
|
+
|
41
|
+
def growth(filters: {}, domain:)
|
42
|
+
GrowthResource.new(client: self, filters: filters, domain: domain).run
|
43
|
+
end
|
44
|
+
|
45
|
+
def events(filters: {}, domain:)
|
46
|
+
EventResource.new(client: self, filters: filters, domain: domain).run
|
47
|
+
end
|
48
|
+
|
49
|
+
def utm_source(filters: {}, domain:)
|
50
|
+
UtmSourceResource.new(client: self, filters: filters, domain: domain).run
|
51
|
+
end
|
52
|
+
|
53
|
+
def utm_medium(filters: {}, domain:)
|
54
|
+
UtmMediumResource.new(client: self, filters: filters, domain: domain).run
|
55
|
+
end
|
56
|
+
|
57
|
+
def utm_campaign(filters: {}, domain:)
|
58
|
+
UtmCampaignResource.new(client: self, filters: filters, domain: domain).run
|
26
59
|
end
|
27
60
|
end
|
28
61
|
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require "ostruct"
|
2
|
+
|
3
|
+
module PirschApi
|
4
|
+
class Object < OpenStruct
|
5
|
+
def initialize(attributes)
|
6
|
+
super to_ostruct(attributes)
|
7
|
+
end
|
8
|
+
|
9
|
+
def to_ostruct(obj)
|
10
|
+
if obj.is_a?(Hash)
|
11
|
+
OpenStruct.new(obj.map { |key, val| [key, to_ostruct(val)] }.to_h)
|
12
|
+
elsif obj.is_a?(Array)
|
13
|
+
obj.map { |o| to_ostruct(o) }
|
14
|
+
else # Assumed to be a primitive value
|
15
|
+
obj
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
module PirschApi
|
2
|
+
class BaseResource
|
3
|
+
|
4
|
+
attr_accessor :client, :filters, :domain
|
5
|
+
|
6
|
+
def initialize(client:, domain: nil, filters: {})
|
7
|
+
@client = client
|
8
|
+
@filters = filters
|
9
|
+
@domain = domain
|
10
|
+
@errors = []
|
11
|
+
end
|
12
|
+
|
13
|
+
def request_url
|
14
|
+
raise NotImplementedError
|
15
|
+
end
|
16
|
+
|
17
|
+
def parse_response(body)
|
18
|
+
raise NotImplementedError
|
19
|
+
end
|
20
|
+
|
21
|
+
def errors
|
22
|
+
@errors[:client] = 'client is invalid' unless client.valid?
|
23
|
+
@errors
|
24
|
+
end
|
25
|
+
|
26
|
+
def valid?
|
27
|
+
errors.empty?
|
28
|
+
end
|
29
|
+
|
30
|
+
def run
|
31
|
+
raise PirschApi::Error.new errors unless valid?
|
32
|
+
|
33
|
+
if domain
|
34
|
+
filters[:id] = domain.id
|
35
|
+
end
|
36
|
+
|
37
|
+
url = "#{PirschApi::Client::BASE_URL}/#{request_url}"
|
38
|
+
url << "?#{filters.to_a.map { |x| "#{x[0]}=#{x[1]}" }.join("&") }" if filters.any?
|
39
|
+
|
40
|
+
puts "[Pirsch API] run #{url}"
|
41
|
+
|
42
|
+
uri = URI.parse url
|
43
|
+
|
44
|
+
req = Net::HTTP::Get.new(uri.request_uri)
|
45
|
+
req.add_field('Authorization', "Bearer #{client.token.access_token}")
|
46
|
+
|
47
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
48
|
+
http.use_ssl = true
|
49
|
+
|
50
|
+
response = http.request(req)
|
51
|
+
raise PirschApi::Error.new "Api request failed (#{response.body})" unless response.code == "200"
|
52
|
+
raise PirschApi::Error.new "Api request returned nil (#{response.body})" if response.body.nil?
|
53
|
+
|
54
|
+
parse_response response.body
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
@@ -1,6 +1,6 @@
|
|
1
1
|
module PirschApi
|
2
2
|
|
3
|
-
class
|
3
|
+
class TokenResource
|
4
4
|
|
5
5
|
def initialize(client_id, client_secret)
|
6
6
|
@client_id = client_id
|
@@ -8,7 +8,7 @@ module PirschApi
|
|
8
8
|
end
|
9
9
|
|
10
10
|
def request_url
|
11
|
-
"
|
11
|
+
"token"
|
12
12
|
end
|
13
13
|
|
14
14
|
def request_body
|
@@ -19,12 +19,13 @@ module PirschApi
|
|
19
19
|
end
|
20
20
|
|
21
21
|
def parse_response(body)
|
22
|
-
|
22
|
+
puts "[Pirsch API] Token received"
|
23
|
+
Token.new JSON.parse(body)
|
23
24
|
end
|
24
25
|
|
25
26
|
def run
|
26
|
-
uri = URI.parse request_url
|
27
|
-
|
27
|
+
uri = URI.parse "#{PirschApi::Client::BASE_URL}/#{request_url}"
|
28
|
+
puts "[Pirsch API] Requesting Token..."
|
28
29
|
http = Net::HTTP.new(uri.host, uri.port)
|
29
30
|
http.use_ssl = true
|
30
31
|
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
@@ -34,7 +35,8 @@ module PirschApi
|
|
34
35
|
request.body = request_body
|
35
36
|
|
36
37
|
response = http.request(request)
|
37
|
-
|
38
|
+
|
39
|
+
raise Error.new "Token request failed. (#{response.body})" unless response.code == "200"
|
38
40
|
|
39
41
|
parse_response(response.body)
|
40
42
|
end
|
data/lib/pirsch_api/version.rb
CHANGED
data/lib/pirsch_api.rb
CHANGED
@@ -1,6 +1,37 @@
|
|
1
|
+
require 'json'
|
2
|
+
require "net/http"
|
3
|
+
require "uri"
|
4
|
+
require "cgi"
|
5
|
+
|
1
6
|
require 'pirsch_api/version'
|
2
|
-
require 'pirsch_api/client'
|
3
7
|
|
4
8
|
module PirschApi
|
5
|
-
|
9
|
+
autoload :Client, "pirsch_api/client"
|
10
|
+
autoload :Error, "pirsch_api/error"
|
11
|
+
|
12
|
+
autoload :BaseResource, "pirsch_api/resources/base"
|
13
|
+
autoload :TokenResource, "pirsch_api/resources/token"
|
14
|
+
autoload :DomainResource, "pirsch_api/resources/domain"
|
15
|
+
autoload :VisitorResource, "pirsch_api/resources/statistics/visitor"
|
16
|
+
autoload :PageResource, "pirsch_api/resources/statistics/page"
|
17
|
+
autoload :ReferrerResource, "pirsch_api/resources/statistics/referrer"
|
18
|
+
autoload :ActiveResource, "pirsch_api/resources/statistics/active"
|
19
|
+
autoload :GrowthResource, "pirsch_api/resources/statistics/growth"
|
20
|
+
autoload :EventResource, "pirsch_api/resources/statistics/event"
|
21
|
+
autoload :UtmSourceResource, "pirsch_api/resources/statistics/utm_source"
|
22
|
+
autoload :UtmCampaignResource, "pirsch_api/resources/statistics/utm_campaign"
|
23
|
+
autoload :UtmMediumResource, "pirsch_api/resources/statistics/utm_medium"
|
24
|
+
|
25
|
+
autoload :Object, "pirsch_api/object"
|
26
|
+
autoload :Token, "pirsch_api/objects/token"
|
27
|
+
autoload :Domain, "pirsch_api/objects/domain"
|
28
|
+
autoload :Visitor, "pirsch_api/objects/visitor"
|
29
|
+
autoload :Page, "pirsch_api/objects/page"
|
30
|
+
autoload :Referrer, "pirsch_api/objects/referrer"
|
31
|
+
autoload :Active, "pirsch_api/objects/active"
|
32
|
+
autoload :Growth, "pirsch_api/objects/growth"
|
33
|
+
autoload :Event, "pirsch_api/objects/event"
|
34
|
+
autoload :UtmSource, "pirsch_api/objects/utm_source"
|
35
|
+
autoload :UtmCampaign, "pirsch_api/objects/utm_campaign"
|
36
|
+
autoload :UtmMedium, "pirsch_api/objects/utm_medium"
|
6
37
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pirsch_api
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Gustavo Garcia
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-09-
|
11
|
+
date: 2021-09-06 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: A very humble wrapper for the API by Pirsch.io
|
14
14
|
email:
|
@@ -17,6 +17,7 @@ executables: []
|
|
17
17
|
extensions: []
|
18
18
|
extra_rdoc_files: []
|
19
19
|
files:
|
20
|
+
- ".gitignore"
|
20
21
|
- CODE_OF_CONDUCT.md
|
21
22
|
- Gemfile
|
22
23
|
- Gemfile.lock
|
@@ -27,12 +28,33 @@ files:
|
|
27
28
|
- bin/setup
|
28
29
|
- lib/pirsch_api.rb
|
29
30
|
- lib/pirsch_api/client.rb
|
30
|
-
- lib/pirsch_api/
|
31
|
+
- lib/pirsch_api/error.rb
|
32
|
+
- lib/pirsch_api/object.rb
|
33
|
+
- lib/pirsch_api/objects/active.rb
|
34
|
+
- lib/pirsch_api/objects/domain.rb
|
35
|
+
- lib/pirsch_api/objects/event.rb
|
36
|
+
- lib/pirsch_api/objects/growth.rb
|
37
|
+
- lib/pirsch_api/objects/page.rb
|
38
|
+
- lib/pirsch_api/objects/referrer.rb
|
39
|
+
- lib/pirsch_api/objects/token.rb
|
40
|
+
- lib/pirsch_api/objects/utm_campaign.rb
|
41
|
+
- lib/pirsch_api/objects/utm_medium.rb
|
42
|
+
- lib/pirsch_api/objects/utm_source.rb
|
43
|
+
- lib/pirsch_api/objects/visitor.rb
|
44
|
+
- lib/pirsch_api/resources/base.rb
|
45
|
+
- lib/pirsch_api/resources/domain.rb
|
46
|
+
- lib/pirsch_api/resources/statistics/active.rb
|
47
|
+
- lib/pirsch_api/resources/statistics/event.rb
|
48
|
+
- lib/pirsch_api/resources/statistics/growth.rb
|
49
|
+
- lib/pirsch_api/resources/statistics/page.rb
|
50
|
+
- lib/pirsch_api/resources/statistics/referrer.rb
|
51
|
+
- lib/pirsch_api/resources/statistics/utm_campaign.rb
|
52
|
+
- lib/pirsch_api/resources/statistics/utm_medium.rb
|
53
|
+
- lib/pirsch_api/resources/statistics/utm_source.rb
|
54
|
+
- lib/pirsch_api/resources/statistics/visitor.rb
|
55
|
+
- lib/pirsch_api/resources/token.rb
|
31
56
|
- lib/pirsch_api/version.rb
|
32
57
|
- pirsch_api.gemspec
|
33
|
-
- pkg/plausible_api-0.1.0.gem
|
34
|
-
- pkg/plausible_api-0.1.7.gem
|
35
|
-
- pkg/plausible_api-0.1.8.gem
|
36
58
|
homepage: https://github.com/dailytics/pirsch_api
|
37
59
|
licenses:
|
38
60
|
- MIT
|
data/pkg/plausible_api-0.1.0.gem
DELETED
Binary file
|
data/pkg/plausible_api-0.1.7.gem
DELETED
Binary file
|
data/pkg/plausible_api-0.1.8.gem
DELETED
Binary file
|