rails_api_guard 0.1.1 → 0.2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ff2c4a75442207f09d733b09cb7cbf2cfc81a8837be142485b5e9ca34ad0314e
4
- data.tar.gz: 3fc2814cfd88a03b7230e780fa39fea656a7ea93fc757fca1900c32d5f7d9fe5
3
+ metadata.gz: a7b4d8d394e924a76d3aa00b2fdb2fdf782747545d8ffc2542506bc854ea6a65
4
+ data.tar.gz: 9dfa228e433502c43df3e0ed50348af2581c73cfa7376d88e1a491310ca8eac5
5
5
  SHA512:
6
- metadata.gz: 9d3fdc1f9a02bc18d4f7c71050121b572d47c1d9bbed33067c559a821773323b5882ec5a8fac97ed20c6599dc8a1573896fc26a7884a5d1b06d0de73c3ada67a
7
- data.tar.gz: eeb6d2a72fead98a52ad278ec089fafdb69897d9fb03f0f4097f56c729297dced6dc76a00a01cf6abd8b73b658852f46c2f632a89fd7d67b1a636c9e6a71e726
6
+ metadata.gz: dc940368929aa36574ff9fac9326eaf2c2d6a104c42081491bf0552857becbfb5ae9dbe51f2fc63f518f5ad771e59f63ac3ed1c0b8f37fdf20c91de17ca06906
7
+ data.tar.gz: 94144edae5b36b29d9319d152a1d80b95d524f9e66e9b22d8880805baf74f692e6645cf051e352442a4b698698d7056011d9a0263cafaeed502d75e5c3977791
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,16 @@ 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
+ ## Configuration
29
+ Edit `config/initializers/rails_api_guard.rb` to set request limits, expiry time, Slack alerts, and excluded endpoint patterns.
30
+
24
31
  ## Contributing
25
- Contribution directions go here.
32
+ Contribution directions are yet to add.
26
33
 
27
34
  ## License
28
35
  The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,15 @@
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
+ end
14
+ end
15
+ 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.0"
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,7 +1,7 @@
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.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - sugat dhole
@@ -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