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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6c4d318b3d83025f43ba07d133256ce35d250466f876f4c2924a133cac7f2806
4
- data.tar.gz: 793e0d8437796cb2b1ad3db61b1b39b4294b811db599a21c151ec5f088b00a36
3
+ metadata.gz: e63c1589bb56b02570e9ae05d9f38ca68bb604ef071084f49638b9ff7cc7e90a
4
+ data.tar.gz: e468852d2153f6827c8359aceb349884c21e00766b8efbc4895057b89e26318a
5
5
  SHA512:
6
- metadata.gz: ea85b0e60ad7a9e796dd1a406f3eff0a81149073b5f0e019c7b4f2eec26b932c92063490e49b74422400f6e45e5e1cccbed2f7b1ede7d42e649342925d0f31ca
7
- data.tar.gz: 37ab6d51c091c4aa7c3e56d42c2eb0c7dc17529a855ab3d1a18c947731f1f3036042f989fffa7c593d4061fb37cced5d2bbf2395114d695e0ab5cc8268ddce59
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
- Assign your API key to `ANALYTICS_API_KEY` in `config/secrets.yml`.
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 "api_analytics"
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::Middleware # Add middleware
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.
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Analytics
4
- VERSION = "1.0.2"
4
+ VERSION = "1.0.4"
5
5
  end
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 = Rails.application.secrets.ANALYTICS_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: "Rails",
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
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: api_analytics
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tom Draper