rails_api_guard 0.1.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: ae35863b762ee8926c2893c07c25d9a714041d0663db67af7a99a7b4a3a00cd5
4
+ data.tar.gz: 05fc95bdf98f4d3e0fce584cb8207e984278ffb38c884eea60e6ccd8916a699e
5
+ SHA512:
6
+ metadata.gz: 7251b195ef84cce364ab0cd6b6b492a9bbd46369c62a024c91315c80932f53a994512fc5e79e52e97dc1c5b51bade97a1b673b635b109aa2ac5d8f9ca1d018d9
7
+ data.tar.gz: e54bf900c2047aa6a0817e000f30ba2ff45d9ff830c7c03fd80a77e9ff1f30462f899bb8b29d514a60e078db325670321486b9d8a427896cb8ec8da1dc969c6c
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright sugat dhole
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,28 @@
1
+ # RailsApiGuard
2
+ Short description and motivation.
3
+
4
+ ## Usage
5
+ How to use my plugin.
6
+
7
+ ## Installation
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem "rails_api_guard"
12
+ ```
13
+
14
+ And then execute:
15
+ ```bash
16
+ $ bundle
17
+ ```
18
+
19
+ Or install it yourself as:
20
+ ```bash
21
+ $ gem install rails_api_guard
22
+ ```
23
+
24
+ ## Contributing
25
+ Contribution directions go here.
26
+
27
+ ## License
28
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require "bundler/setup"
2
+
3
+ APP_RAKEFILE = File.expand_path("test/dummy/Rakefile", __dir__)
4
+ load "rails/tasks/engine.rake"
5
+
6
+ load "rails/tasks/statistics.rake"
7
+
8
+ require "bundler/gem_tasks"
@@ -0,0 +1 @@
1
+ //= link_directory ../stylesheets/rails_api_guard .css
@@ -0,0 +1,15 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the bottom of the
9
+ * compiled file so the styles you add here take precedence over styles defined in any other CSS/SCSS
10
+ * files in this directory. Styles in this file should be added after the last require_* statement.
11
+ * It is generally better to create a new file per style scope.
12
+ *
13
+ *= require_tree .
14
+ *= require_self
15
+ */
@@ -0,0 +1,4 @@
1
+ module RailsApiGuard
2
+ class ApplicationController < ActionController::Base
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module RailsApiGuard
2
+ module ApplicationHelper
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module RailsApiGuard
2
+ class ApplicationJob < ActiveJob::Base
3
+ end
4
+ end
@@ -0,0 +1,6 @@
1
+ module RailsApiGuard
2
+ class ApplicationMailer < ActionMailer::Base
3
+ default from: "from@example.com"
4
+ layout "mailer"
5
+ end
6
+ end
@@ -0,0 +1,5 @@
1
+ module RailsApiGuard
2
+ class ApplicationRecord < ActiveRecord::Base
3
+ self.abstract_class = true
4
+ end
5
+ end
@@ -0,0 +1,15 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Rails api guard</title>
5
+ <%= csrf_meta_tags %>
6
+ <%= csp_meta_tag %>
7
+
8
+ <%= stylesheet_link_tag "rails_api_guard/application", media: "all" %>
9
+ </head>
10
+ <body>
11
+
12
+ <%= yield %>
13
+
14
+ </body>
15
+ </html>
data/config/routes.rb ADDED
@@ -0,0 +1,2 @@
1
+ RailsApiGuard::Engine.routes.draw do
2
+ end
@@ -0,0 +1,12 @@
1
+ module RailsApiGuard
2
+ class Config
3
+ attr_accessor :excluded_patterns, :limit, :expiry_time, :slack_webhook_url
4
+
5
+ def initialize
6
+ @excluded_patterns = []
7
+ @limit = 5
8
+ @expiry_time = 60 # in seconds
9
+ @slack_webhook_url = nil
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,39 @@
1
+ require "yaml"
2
+
3
+ module RailsApiGuard
4
+ class ConfigManager
5
+ def self.excluded_patterns
6
+ config["excluded_patterns"] || []
7
+ end
8
+
9
+ def self.limit
10
+ config["limit"] || 5
11
+ end
12
+
13
+ def self.expiry_time
14
+ config["expiry_time"] || 60
15
+ end
16
+
17
+ def self.slack_webhook_url
18
+ config["slack_webhook_url"]
19
+ end
20
+
21
+ def self.config
22
+ @config ||= load_config
23
+ end
24
+
25
+ def self.load_config
26
+ config_file = Rails.root.join("config/rails_api_guard.yml")
27
+ unless File.exist?(config_file)
28
+ Rails.logger.warn "⚠️ Rails API Guard config file missing at #{config_file}"
29
+ return {}
30
+ end
31
+
32
+ YAML.load_file(config_file) || {}
33
+ end
34
+
35
+ def self.reload!
36
+ @config = load_config
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,7 @@
1
+ module RailsApiGuard
2
+ class Engine < ::Rails::Engine
3
+ isolate_namespace RailsApiGuard
4
+
5
+ config.eager_load_paths << root.join("lib")
6
+ end
7
+ end
@@ -0,0 +1,43 @@
1
+ module RailsApiGuard
2
+ module Middleware
3
+ class RateLimiter
4
+ def initialize(app)
5
+ @app = app
6
+ @limit = RailsApiGuard.config.limit
7
+ @expiry_time = RailsApiGuard.config.expiry_time
8
+ @rate_limit_store = Services::RateLimitStore.new(@limit, @expiry_time)
9
+ end
10
+
11
+ def call(env)
12
+ request = Rack::Request.new(env)
13
+
14
+ if Services::ExclusionChecker.excluded_endpoint?(request.path)
15
+ Rails.logger.info "Skipping rate limiting for #{request.path}"
16
+ return @app.call(env)
17
+ end
18
+
19
+ ip = request.ip
20
+
21
+
22
+ if @rate_limit_store.increment_and_check(ip, request.path)
23
+ Rails.logger.info "Rate limiting exceeded!!!"
24
+ Services::SlackNotifier.notify("🚨 Rate limit exceeded for #{ip}!")
25
+ return too_many_requests
26
+ end
27
+
28
+
29
+ @app.call(env)
30
+ end
31
+
32
+ private
33
+
34
+ def too_many_requests
35
+ [
36
+ 429,
37
+ { "Content-Type" => "application/json" },
38
+ [{ error: "Rate limit exceeded" }.to_json]
39
+ ]
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,17 @@
1
+ module RailsApiGuard
2
+ module Services
3
+ class ExclusionChecker
4
+ def self.excluded_endpoint?(path)
5
+ return true if path.start_with?('/.well-known')
6
+
7
+ RailsApiGuard.config.excluded_patterns.any? do |pattern|
8
+ if pattern.start_with?("^")
9
+ Regexp.new(pattern).match?(path)
10
+ else
11
+ File.fnmatch?(pattern, path)
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,24 @@
1
+ module RailsApiGuard
2
+ module Services
3
+ class RateLimitStore
4
+ def initialize(limit, expiry_time)
5
+ @limit = limit
6
+ @expiry_time = expiry_time
7
+ @redis = RedisClient.instance
8
+ end
9
+
10
+ def increment_and_check(ip, path)
11
+ key = "rate_limiter::#{ip}::#{path}"
12
+ current_count = @redis.get(key).to_i
13
+
14
+ if current_count == 0
15
+ @redis.setex(key, @expiry_time, 0)
16
+ end
17
+
18
+ @redis.incr(key)
19
+
20
+ current_count + 1 > @limit
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,9 @@
1
+ module RailsApiGuard
2
+ module Services
3
+ class RedisClient
4
+ def self.instance
5
+ @instance ||= Redis.new
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,16 @@
1
+ require 'httparty'
2
+
3
+ module RailsApiGuard
4
+ module Services
5
+ class SlackNotifier
6
+ def self.notify(message)
7
+ webhook_url = RailsApiGuard.config.slack_webhook_url
8
+ return unless webhook_url.present?
9
+
10
+ payload = { text: message }
11
+
12
+ HTTParty.post(webhook_url, body: payload.to_json, headers: { 'Content-Type' => 'application/json' })
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,3 @@
1
+ module RailsApiGuard
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,16 @@
1
+ require "rails_api_guard/version"
2
+ require "rails_api_guard/engine"
3
+ require "rails_api_guard/middleware/rate_limiter"
4
+ require "rails_api_guard/config"
5
+ require "redis"
6
+
7
+ module RailsApiGuard
8
+ class << self
9
+ attr_accessor :config
10
+ end
11
+
12
+ def self.configure
13
+ self.config ||= Config.new
14
+ yield(config)
15
+ end
16
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :rails_api_guard do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,111 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails_api_guard
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - sugat dhole
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2025-06-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 7.1.3
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 7.1.3
27
+ - !ruby/object:Gem::Dependency
28
+ name: redis
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '5.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '5.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: httparty
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Plug-and-play API rate limiter with per-user/per-plan configs, Redis
56
+ backend, Slack alerts and admin dashboard.
57
+ email:
58
+ - sugatdhole@gmail.com
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - MIT-LICENSE
64
+ - README.md
65
+ - Rakefile
66
+ - app/assets/config/rails_api_guard_manifest.js
67
+ - app/assets/stylesheets/rails_api_guard/application.css
68
+ - app/controllers/rails_api_guard/application_controller.rb
69
+ - app/helpers/rails_api_guard/application_helper.rb
70
+ - app/jobs/rails_api_guard/application_job.rb
71
+ - app/mailers/rails_api_guard/application_mailer.rb
72
+ - app/models/rails_api_guard/application_record.rb
73
+ - app/views/layouts/rails_api_guard/application.html.erb
74
+ - config/routes.rb
75
+ - lib/rails_api_guard.rb
76
+ - lib/rails_api_guard/config.rb
77
+ - lib/rails_api_guard/config_manager.rb
78
+ - lib/rails_api_guard/engine.rb
79
+ - lib/rails_api_guard/middleware/rate_limiter.rb
80
+ - lib/rails_api_guard/services/exclusion_checker.rb
81
+ - lib/rails_api_guard/services/rate_limit_store.rb
82
+ - lib/rails_api_guard/services/redis_client.rb
83
+ - lib/rails_api_guard/services/slack_notifier.rb
84
+ - lib/rails_api_guard/version.rb
85
+ - lib/tasks/rails_api_guard_tasks.rake
86
+ homepage: https://github.com/sudo0809/rails_api_guard
87
+ licenses:
88
+ - MIT
89
+ metadata:
90
+ homepage_uri: https://github.com/sudo0809/rails_api_guard
91
+ source_code_uri: https://github.com/sudo0809/rails_api_guard
92
+ post_install_message:
93
+ rdoc_options: []
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ requirements: []
107
+ rubygems_version: 3.4.10
108
+ signing_key:
109
+ specification_version: 4
110
+ summary: API rate limiter middleware engine for Rails APIs.
111
+ test_files: []