rails_api_guard 0.1.1 → 0.2.1

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: ff2c4a75442207f09d733b09cb7cbf2cfc81a8837be142485b5e9ca34ad0314e
4
- data.tar.gz: 3fc2814cfd88a03b7230e780fa39fea656a7ea93fc757fca1900c32d5f7d9fe5
3
+ metadata.gz: 93890693de684b76c7ac78ed99e9d1ca804272c7cbf14b1c8087d5070ab2fcea
4
+ data.tar.gz: f4056a7057f5d56e26143ebf7d810d2d742090a2e920c4cf29a9b289bea96924
5
5
  SHA512:
6
- metadata.gz: 9d3fdc1f9a02bc18d4f7c71050121b572d47c1d9bbed33067c559a821773323b5882ec5a8fac97ed20c6599dc8a1573896fc26a7884a5d1b06d0de73c3ada67a
7
- data.tar.gz: eeb6d2a72fead98a52ad278ec089fafdb69897d9fb03f0f4097f56c729297dced6dc76a00a01cf6abd8b73b658852f46c2f632a89fd7d67b1a636c9e6a71e726
6
+ metadata.gz: a4b8acff3abc11669ad3d6ca6a1d35886e06f420f51f27822d6292e942b920b10faccfe4a365c15e337b410330283868876249b5909e5da9bcb8efd815efa95b
7
+ data.tar.gz: e6cab1599d4edcf9b013a89139e7bd398764ca5491d3d0d076742472de1414cbe0ee77514b4439077d4209d9ce6c078ad3c935005ffe3753b0156b6362adea41
data/README.md CHANGED
@@ -1,19 +1,18 @@
1
1
  # RailsApiGuard
2
- Short description and motivation.
3
2
 
4
- ## Usage
5
- How to use my plugin.
3
+ A pluggable, configurable rate limiting middleware for Rails APIs with Redis backend, Slack alerts, and endpoint exclusions.
4
+
5
+ ## 🚀 Installation
6
6
 
7
- ## Installation
8
7
  Add this line to your application's Gemfile:
9
8
 
10
9
  ```ruby
11
- gem "rails_api_guard"
10
+ gem 'rails_api_guard'
12
11
  ```
13
12
 
14
13
  And then execute:
15
14
  ```bash
16
- $ bundle
15
+ $ bundle install
17
16
  ```
18
17
 
19
18
  Or install it yourself as:
@@ -21,8 +20,22 @@ Or install it yourself as:
21
20
  $ gem install rails_api_guard
22
21
  ```
23
22
 
23
+ ## Generate the Initializer
24
+ ```bash
25
+ rails generate rails_api_guard:install
26
+ ```
27
+
28
+ ## Middleware
29
+ Add this to application.rb if not used generator
30
+ ```ruby
31
+ config.middleware.use RailsApiGuard::Middleware::RateLimiter
32
+ ```
33
+
34
+ ## Configuration
35
+ Edit `config/initializers/rails_api_guard.rb` to set request limits, expiry time, Slack alerts, and excluded endpoint patterns.
36
+
24
37
  ## Contributing
25
- Contribution directions go here.
38
+ Contribution directions are yet to add.
26
39
 
27
40
  ## License
28
41
  The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,29 @@
1
+ require "rails/generators"
2
+
3
+ module RailsApiGuard
4
+ module Generators
5
+ class InstallGenerator < Rails::Generators::Base
6
+ source_root File.expand_path("templates", __dir__)
7
+
8
+ desc "Creates a RailsApiGuard initializer file."
9
+
10
+ def copy_initializer
11
+ template "rails_api_guard_initializer.rb", "config/initializers/rails_api_guard.rb"
12
+ end
13
+
14
+ def add_middleware
15
+ application_file = 'config/application.rb'
16
+ middleware_pattern = /config\.middleware\.use RailsApiGuard::Middleware::RateLimiter/
17
+ middleware_line = "\t\t# Added by RailsApiGuard gem to apply API rate limiting middleware\n"
18
+ middleware_line += "\t\tconfig.middleware.use RailsApiGuard::Middleware::RateLimiter\n"
19
+
20
+ if File.readlines(application_file).grep(/config\.middleware\.use RailsApiGuard::Middleware::RateLimiter/).any?
21
+ say_status :skipped, "Middleware already added to #{application_file}", :yellow
22
+ else
23
+ insert_into_file application_file, "\n\n#{middleware_line}", after: "class Application < Rails::Application"
24
+ say_status :added, "Middleware added to #{application_file}", :green
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,41 @@
1
+ # config/initializers/rails_api_guard.rb
2
+
3
+ # RailsApiGuard configuration initializer
4
+ #
5
+ # This file allows you to customize rate limiting behavior for your Rails API.
6
+ # You can adjust request limits, expiry time, Slack notifications, and exclusion rules
7
+ # for specific endpoints from rate limiting.
8
+ #
9
+ # You can safely add, remove, or modify these settings as per your application needs.
10
+
11
+ RailsApiGuard.configure do |config|
12
+ # 🚦 Maximum number of allowed requests within the expiry_time window per IP (or per key)
13
+ config.limit = 5
14
+
15
+ # 🕒 Expiry time (in seconds) for the rate limiting window.
16
+ # After this time window passes, the request count for an IP or key resets.
17
+ config.expiry_time = 60
18
+
19
+ # 🔔 Slack webhook URL for sending alerts when a client exceeds the rate limit.
20
+ # Best practice: set this securely via environment variables.
21
+ config.slack_webhook_url = ENV['SLACK_WEBHOOK_URL']
22
+
23
+ # 🚫 List of endpoint patterns to be excluded from rate limiting.
24
+ #
25
+ # Supports:
26
+ # - Exact string paths (e.g. '/healthcheck')
27
+ # - Wildcard patterns (e.g. '/public/*')
28
+ # - Regular expressions (must start with '^', e.g. '^/user/\d+/document/\w+$')
29
+ #
30
+ # 👉 This list is **fully extendable** — feel free to add any additional paths
31
+ # or dynamic route patterns you want to bypass the rate limiter.
32
+ #
33
+ # Example:
34
+ # config.excluded_patterns << '/open-api/*'
35
+ #
36
+ config.excluded_patterns = [
37
+ '/healthcheck',
38
+ '/public/*',
39
+ '^/user/\\d+/document/\\w+$'
40
+ ]
41
+ end
@@ -1,3 +1,3 @@
1
1
  module RailsApiGuard
2
- VERSION = "0.1.1"
2
+ VERSION = "0.2.1"
3
3
  end
@@ -3,14 +3,17 @@ require "rails_api_guard/engine"
3
3
  require "rails_api_guard/middleware/rate_limiter"
4
4
  require "rails_api_guard/config"
5
5
  require "redis"
6
+ require "rails_api_guard/generators/install/install_generator"
7
+
6
8
 
7
9
  module RailsApiGuard
8
10
  class << self
9
11
  attr_accessor :config
10
12
  end
11
13
 
14
+ self.config ||= Config.new
15
+
12
16
  def self.configure
13
- self.config ||= Config.new
14
- yield(config)
17
+ yield(config) if block_given?
15
18
  end
16
19
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_api_guard
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - sugat dhole
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2025-06-29 00:00:00.000000000 Z
11
+ date: 2025-06-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -80,8 +80,9 @@ files:
80
80
  - config/routes.rb
81
81
  - lib/rails_api_guard.rb
82
82
  - lib/rails_api_guard/config.rb
83
- - lib/rails_api_guard/config_manager.rb
84
83
  - lib/rails_api_guard/engine.rb
84
+ - lib/rails_api_guard/generators/install/install_generator.rb
85
+ - lib/rails_api_guard/generators/install/templates/rails_api_guard_initializer.rb
85
86
  - lib/rails_api_guard/middleware/rate_limiter.rb
86
87
  - lib/rails_api_guard/services/exclusion_checker.rb
87
88
  - lib/rails_api_guard/services/rate_limit_store.rb
@@ -95,6 +96,7 @@ licenses:
95
96
  metadata:
96
97
  homepage_uri: https://github.com/sudo0809/rails_api_guard
97
98
  source_code_uri: https://github.com/sudo0809/rails_api_guard
99
+ changelog_uri: https://github.com/sudo0809/rails_api_guard/blob/main/CHANGELOG.md
98
100
  post_install_message:
99
101
  rdoc_options: []
100
102
  require_paths:
@@ -103,7 +105,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
103
105
  requirements:
104
106
  - - ">="
105
107
  - !ruby/object:Gem::Version
106
- version: '0'
108
+ version: '3.0'
107
109
  required_rubygems_version: !ruby/object:Gem::Requirement
108
110
  requirements:
109
111
  - - ">="
@@ -1,39 +0,0 @@
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