api_analytics 1.0.1 → 1.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +32 -26
- data/lib/api_analytics/version.rb +1 -1
- data/lib/api_analytics.rb +6 -10
- 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: 6c4d318b3d83025f43ba07d133256ce35d250466f876f4c2924a133cac7f2806
|
4
|
+
data.tar.gz: 793e0d8437796cb2b1ad3db61b1b39b4294b811db599a21c151ec5f088b00a36
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ea85b0e60ad7a9e796dd1a406f3eff0a81149073b5f0e019c7b4f2eec26b932c92063490e49b74422400f6e45e5e1cccbed2f7b1ede7d42e649342925d0f31ca
|
7
|
+
data.tar.gz: 37ab6d51c091c4aa7c3e56d42c2eb0c7dc17529a855ab3d1a18c947731f1f3036042f989fffa7c593d4061fb37cced5d2bbf2395114d695e0ab5cc8268ddce59
|
data/README.md
CHANGED
@@ -1,43 +1,49 @@
|
|
1
|
-
#
|
1
|
+
# API Analytics
|
2
2
|
|
3
|
-
|
3
|
+
A lightweight API analytics solution, complete with a dashboard.
|
4
4
|
|
5
|
-
|
5
|
+
## Getting Started
|
6
6
|
|
7
|
-
|
7
|
+
### 1. Generate a new API key
|
8
8
|
|
9
|
-
|
9
|
+
Head to https://my-api-analytics.vercel.app/generate to generate your unique API key with a single click. This key is used to monitor your specific API, so keep it secret! It's also required in order to view your APIs analytics dashboard.
|
10
10
|
|
11
|
-
|
12
|
-
gem 'api_analytics'
|
13
|
-
```
|
14
|
-
|
15
|
-
And then execute:
|
16
|
-
|
17
|
-
$ bundle install
|
18
|
-
|
19
|
-
Or install it yourself as:
|
11
|
+
### 2. Add middleware to your API
|
20
12
|
|
21
|
-
|
13
|
+
Add our lightweight middleware to your API. Almost all processing is handled by our servers so there should be virtually no impact on your APIs performance.
|
22
14
|
|
23
|
-
|
15
|
+
```bash
|
16
|
+
gem install api_analytics
|
17
|
+
```
|
24
18
|
|
25
|
-
|
19
|
+
#### Rails
|
26
20
|
|
27
|
-
|
21
|
+
Assign your API key to `ANALYTICS_API_KEY` in `config/secrets.yml`.
|
28
22
|
|
29
|
-
|
23
|
+
```yml
|
24
|
+
development:
|
25
|
+
ANALYTICS_API_KEY: <api_key>
|
26
|
+
production:
|
27
|
+
ANALYTICS_API_KEY: <api_key>
|
28
|
+
```
|
30
29
|
|
31
|
-
|
30
|
+
Require `api_analytics` and add the analytics middleware to your rails application in `config/application.rb`.
|
32
31
|
|
33
|
-
|
32
|
+
```ruby
|
33
|
+
require "api_analytics"
|
34
34
|
|
35
|
-
|
35
|
+
Bundler.require(*Rails.groups)
|
36
36
|
|
37
|
-
|
37
|
+
module RailsMiddleware
|
38
|
+
class Application < Rails::Application
|
39
|
+
config.load_defaults 6.1
|
40
|
+
config.api_only = true
|
38
41
|
|
39
|
-
|
42
|
+
config.middleware.use ::Analytics::Middleware # Add middleware
|
43
|
+
end
|
44
|
+
end
|
45
|
+
```
|
40
46
|
|
41
|
-
|
47
|
+
### 3. View your analytics
|
42
48
|
|
43
|
-
|
49
|
+
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
@@ -1,8 +1,8 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require 'uri'
|
3
4
|
require 'net/http'
|
4
5
|
require 'json'
|
5
|
-
require_relative "api_analytics/version"
|
6
6
|
|
7
7
|
module Analytics
|
8
8
|
class Middleware
|
@@ -16,7 +16,7 @@ module Analytics
|
|
16
16
|
start = Time.now
|
17
17
|
status, headers, response = @app.call(env)
|
18
18
|
|
19
|
-
|
19
|
+
data = {
|
20
20
|
api_key: @api_key,
|
21
21
|
hostname: env['HTTP_HOST'],
|
22
22
|
path: env['REQUEST_PATH'],
|
@@ -28,7 +28,7 @@ module Analytics
|
|
28
28
|
}
|
29
29
|
|
30
30
|
Thread.new {
|
31
|
-
log_request(
|
31
|
+
log_request(data)
|
32
32
|
}
|
33
33
|
|
34
34
|
[status, headers, response]
|
@@ -36,16 +36,12 @@ module Analytics
|
|
36
36
|
|
37
37
|
private
|
38
38
|
|
39
|
-
def log_request(
|
39
|
+
def log_request(data)
|
40
40
|
uri = URI('https://api-analytics-server.vercel.app/api/log-request')
|
41
|
-
|
42
|
-
Rails.logger.info("#{uri.host} #{uri.port} #{uri.path}")
|
43
|
-
req = Net::HTTP::Post.new(uri.path, 'Content-Type' => 'application/json')
|
44
|
-
req.body = json.to_json
|
45
|
-
res = http.request(req)
|
46
|
-
rescue => e
|
41
|
+
res = Net::HTTP.post(uri, data.to_json)
|
47
42
|
end
|
48
43
|
end
|
49
44
|
end
|
50
45
|
|
51
46
|
|
47
|
+
|