api_analytics 1.0.2 → 1.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +21 -12
- data/lib/api_analytics/version.rb +1 -1
- data/lib/api_analytics.rb +19 -4
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e63c1589bb56b02570e9ae05d9f38ca68bb604ef071084f49638b9ff7cc7e90a
|
4
|
+
data.tar.gz: e468852d2153f6827c8359aceb349884c21e00766b8efbc4895057b89e26318a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 25be82084145978d624eb701f84dfe0639ba5c405011b55398f6da5a0e788d4ed87322bd9253002ed9528ac3e757e709a393962327f3864bb7f696a161245c89
|
7
|
+
data.tar.gz: d65d9b770e3640008a9243dba26d07722bd47acbf786e571c984f2c568f40e2bb2a94d973c667fbe0c02545d091d523663687b26c88e1437bf1fbe3576b6a634
|
data/README.md
CHANGED
@@ -18,19 +18,11 @@ gem install api_analytics
|
|
18
18
|
|
19
19
|
#### Rails
|
20
20
|
|
21
|
-
|
22
|
-
|
23
|
-
```yml
|
24
|
-
development:
|
25
|
-
ANALYTICS_API_KEY: <api_key>
|
26
|
-
production:
|
27
|
-
ANALYTICS_API_KEY: <api_key>
|
28
|
-
```
|
29
|
-
|
30
|
-
Require `api_analytics` and add the analytics middleware to your rails application in `config/application.rb`.
|
21
|
+
Add the analytics middleware to your rails application in `config/application.rb`.
|
31
22
|
|
32
23
|
```ruby
|
33
|
-
require
|
24
|
+
require 'rails'
|
25
|
+
require 'api_analytics'
|
34
26
|
|
35
27
|
Bundler.require(*Rails.groups)
|
36
28
|
|
@@ -39,11 +31,28 @@ module RailsMiddleware
|
|
39
31
|
config.load_defaults 6.1
|
40
32
|
config.api_only = true
|
41
33
|
|
42
|
-
config.middleware.use ::Analytics::
|
34
|
+
config.middleware.use ::Analytics::Rails, <api_key> # Add middleware
|
43
35
|
end
|
44
36
|
end
|
45
37
|
```
|
46
38
|
|
39
|
+
#### Sinatra
|
40
|
+
|
41
|
+
```ruby
|
42
|
+
require 'sinatra'
|
43
|
+
require 'api_analytics'
|
44
|
+
|
45
|
+
use Analytics::Sinatra, <api_key>
|
46
|
+
|
47
|
+
before do
|
48
|
+
content_type 'application/json'
|
49
|
+
end
|
50
|
+
|
51
|
+
get '/' do
|
52
|
+
{message: 'Hello World!'}.to_json
|
53
|
+
end
|
54
|
+
```
|
55
|
+
|
47
56
|
### 3. View your analytics
|
48
57
|
|
49
58
|
Your API will log requests on all valid routes. Head over to https://my-api-analytics.vercel.app/dashboard and paste in your API key to view your dashboard.
|
data/lib/api_analytics.rb
CHANGED
@@ -6,10 +6,9 @@ require 'json'
|
|
6
6
|
|
7
7
|
module Analytics
|
8
8
|
class Middleware
|
9
|
-
def initialize(app)
|
9
|
+
def initialize(app, api_key)
|
10
10
|
@app = app
|
11
|
-
@api_key =
|
12
|
-
raise StandardError.new 'ANALYTICS_API_KEY secret unset.' if @api_key.nil?
|
11
|
+
@api_key = api_key
|
13
12
|
end
|
14
13
|
|
15
14
|
def call(env)
|
@@ -23,7 +22,7 @@ module Analytics
|
|
23
22
|
user_agent: env['HTTP_USER_AGENT'],
|
24
23
|
method: env['REQUEST_METHOD'],
|
25
24
|
status: status,
|
26
|
-
framework:
|
25
|
+
framework: @framework,
|
27
26
|
response_time: (Time.now - start).to_f.round,
|
28
27
|
}
|
29
28
|
|
@@ -41,6 +40,22 @@ module Analytics
|
|
41
40
|
res = Net::HTTP.post(uri, data.to_json)
|
42
41
|
end
|
43
42
|
end
|
43
|
+
|
44
|
+
private_constant :Middleware
|
45
|
+
|
46
|
+
class Rails < Middleware
|
47
|
+
def initialize(app, api_key)
|
48
|
+
super(app, api_key)
|
49
|
+
@framework = "Rails"
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
class Sinatra < Middleware
|
54
|
+
def initialize(app, api_key)
|
55
|
+
super(app, api_key)
|
56
|
+
@framework = "Sinatra"
|
57
|
+
end
|
58
|
+
end
|
44
59
|
end
|
45
60
|
|
46
61
|
|