api_analytics 1.0.1 → 1.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 74c59a8430be151e1152d5c6d7aaff920463d867b765dad9141a377fb5433b75
4
- data.tar.gz: 3674f6152cd22705c436eef36eedf34c098740886c13c1371f186e2081540d56
3
+ metadata.gz: '098d01b85d0fcc21c62cfd725e9627023215347bf514984ce32439c8ad14b032'
4
+ data.tar.gz: 0e9afbc15ee1ffd011c344fb9fded1afeba64381b1040e5e01b42ef422dc5d5a
5
5
  SHA512:
6
- metadata.gz: 9668fc53760f84fa9616303688f12e013d17680acaf18d6c8ed5438a879e32382c324d2b029c38827d9e88e063b70a3fa100bb059f9517af536e9dd3cd1e5a76
7
- data.tar.gz: bdea3236c660f800c3d546855731ebae30ed61c46703ef775419ddb809c5eb35cc28079f65cfa60a55f09a60a3dffc1be861b9f2d514c6776056eff39a8049e9
6
+ metadata.gz: 452d37e57dbcb9fa71fff792238de09f49bc3f0cb6538417478087b553f8dd18bfadeaaca2757706f15872e01ea693d0bcfa0824c4867b8b557febd9de8cab54
7
+ data.tar.gz: 5dc72c197e4d12fcabcfa4e12f1a7f42e1f7d0496095ccde7e981b2d12d64a0e02cd48e1167355c194ac3ed1526696b7fc221e773efe1cef2c3809ad94ce8648
data/README.md CHANGED
@@ -1,43 +1,58 @@
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
- ```
11
+ ### 2. Add middleware to your API
14
12
 
15
- And then execute:
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.
16
14
 
17
- $ bundle install
15
+ ```bash
16
+ gem install api_analytics
17
+ ```
18
18
 
19
- Or install it yourself as:
19
+ #### Rails
20
20
 
21
- $ gem install api_analytics
21
+ Add the analytics middleware to your rails application in `config/application.rb`.
22
22
 
23
- ## Usage
23
+ ```ruby
24
+ require 'rails'
25
+ require 'api_analytics'
24
26
 
25
- TODO: Write usage instructions here
27
+ Bundler.require(*Rails.groups)
26
28
 
27
- ## Development
29
+ module RailsMiddleware
30
+ class Application < Rails::Application
31
+ config.load_defaults 6.1
32
+ config.api_only = true
28
33
 
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.
34
+ config.middleware.use ::Analytics::Middleware, <api_key> # Add middleware
35
+ end
36
+ end
37
+ ```
30
38
 
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).
39
+ #### Sinatra
32
40
 
33
- ## Contributing
41
+ ```ruby
42
+ require 'sinatra'
43
+ require 'api_analytics'
34
44
 
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).
45
+ use Analytics::Middleware, <api_key>
36
46
 
37
- ## License
47
+ before do
48
+ content_type 'application/json'
49
+ end
38
50
 
39
- The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
51
+ get '/' do
52
+ {message: 'Hello World!'}.to_json
53
+ end
54
+ ```
40
55
 
41
- ## Code of Conduct
56
+ ### 3. View your analytics
42
57
 
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).
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.1"
4
+ VERSION = "1.0.3"
5
5
  end
data/lib/api_analytics.rb CHANGED
@@ -1,22 +1,21 @@
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
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)
16
15
  start = Time.now
17
16
  status, headers, response = @app.call(env)
18
17
 
19
- json = {
18
+ data = {
20
19
  api_key: @api_key,
21
20
  hostname: env['HTTP_HOST'],
22
21
  path: env['REQUEST_PATH'],
@@ -28,7 +27,7 @@ module Analytics
28
27
  }
29
28
 
30
29
  Thread.new {
31
- log_request(json)
30
+ log_request(data)
32
31
  }
33
32
 
34
33
  [status, headers, response]
@@ -36,16 +35,12 @@ module Analytics
36
35
 
37
36
  private
38
37
 
39
- def log_request(json)
38
+ def log_request(data)
40
39
  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
40
+ res = Net::HTTP.post(uri, data.to_json)
47
41
  end
48
42
  end
49
43
  end
50
44
 
51
45
 
46
+
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.1
4
+ version: 1.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tom Draper