api_analytics 1.0.0 → 1.0.2

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: bee24da4758d197c43e6ef9eb7ff23b51ecb233e1a86c6f9ce10deea06627af1
4
- data.tar.gz: 917ca40b90ded5d83a6b01b74637ab455d72393fd3f7717e36eec82391086a4a
3
+ metadata.gz: 6c4d318b3d83025f43ba07d133256ce35d250466f876f4c2924a133cac7f2806
4
+ data.tar.gz: 793e0d8437796cb2b1ad3db61b1b39b4294b811db599a21c151ec5f088b00a36
5
5
  SHA512:
6
- metadata.gz: 9653b4934817f940483f1203f0e2515edefacd8e2b2608e1860e41682ae75a5c62f92aa486fa74b40a004ed8fa61a9f9460b5d4c36ff9cdeff0438b54f97a9f7
7
- data.tar.gz: 85cff895667d068ec7366c75e7c5031d56c9ff249469fcc681a5a7affcd8310e84bc9e4b1d4b524c4fd17647224035a820c48fbfe1c0400612c28e64948958fd
6
+ metadata.gz: ea85b0e60ad7a9e796dd1a406f3eff0a81149073b5f0e019c7b4f2eec26b932c92063490e49b74422400f6e45e5e1cccbed2f7b1ede7d42e649342925d0f31ca
7
+ data.tar.gz: 37ab6d51c091c4aa7c3e56d42c2eb0c7dc17529a855ab3d1a18c947731f1f3036042f989fffa7c593d4061fb37cced5d2bbf2395114d695e0ab5cc8268ddce59
data/README.md CHANGED
@@ -1,43 +1,49 @@
1
- # ApiAnalytics
1
+ # API Analytics
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/api_analytics`. To experiment with that code, run `bin/console` for an interactive prompt.
3
+ A lightweight API analytics solution, complete with a dashboard.
4
4
 
5
- TODO: Delete this and the text above, and describe your gem
5
+ ## Getting Started
6
6
 
7
- ## Installation
7
+ ### 1. Generate a new API key
8
8
 
9
- Add this line to your application's Gemfile:
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
- ```ruby
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
- $ gem install api_analytics
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
- ## Usage
15
+ ```bash
16
+ gem install api_analytics
17
+ ```
24
18
 
25
- TODO: Write usage instructions here
19
+ #### Rails
26
20
 
27
- ## Development
21
+ Assign your API key to `ANALYTICS_API_KEY` in `config/secrets.yml`.
28
22
 
29
- After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
23
+ ```yml
24
+ development:
25
+ ANALYTICS_API_KEY: <api_key>
26
+ production:
27
+ ANALYTICS_API_KEY: <api_key>
28
+ ```
30
29
 
31
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
30
+ Require `api_analytics` and add the analytics middleware to your rails application in `config/application.rb`.
32
31
 
33
- ## Contributing
32
+ ```ruby
33
+ require "api_analytics"
34
34
 
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/api_analytics. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/api_analytics/blob/master/CODE_OF_CONDUCT.md).
35
+ Bundler.require(*Rails.groups)
36
36
 
37
- ## License
37
+ module RailsMiddleware
38
+ class Application < Rails::Application
39
+ config.load_defaults 6.1
40
+ config.api_only = true
38
41
 
39
- The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
42
+ config.middleware.use ::Analytics::Middleware # Add middleware
43
+ end
44
+ end
45
+ ```
40
46
 
41
- ## Code of Conduct
47
+ ### 3. View your analytics
42
48
 
43
- Everyone interacting in the ApiAnalytics project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/api_analytics/blob/master/CODE_OF_CONDUCT.md).
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.
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Analytics
4
- VERSION = "1.0.0"
4
+ VERSION = "1.0.2"
5
5
  end
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 "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
- json = {
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(json)
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(json)
39
+ def log_request(data)
40
40
  uri = URI('https://api-analytics-server.vercel.app/api/log-request')
41
- http = Net::HTTP.new(uri.host, uri.port)
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
+
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.0
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tom Draper