rails_api_guard 0.2.0 → 0.2.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:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 113eca3b940fb42eb59e46207a1389447a14b1c8e664397bcf2e48060bc87c49
|
4
|
+
data.tar.gz: 7ed64fb43fcf0f8225dc0e0a237c35f6b5720e1b0686126d27381766ada440f2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c5736712e78258a58be184b20bf67628c943c3005649f0a0bc3917c8f416e29b4c4b57cddf5266895bd5073aff91375d2fbbfad25159b58b5fe8cce3f854c97d
|
7
|
+
data.tar.gz: 40e092d2d0ca2471179055399df4fea4e38fddb751ffccb6bd51c0063e77e19b2f065deec319e2e2d45c54f5695465726a6d56de09e4952df0d37c489c100224
|
data/README.md
CHANGED
@@ -25,9 +25,29 @@ $ gem install rails_api_guard
|
|
25
25
|
rails generate rails_api_guard:install
|
26
26
|
```
|
27
27
|
|
28
|
+
## Middleware
|
29
|
+
Add this to application.rb if not used generator
|
30
|
+
```ruby
|
31
|
+
config.middleware.use RailsApiGuard::Middleware::RateLimiter
|
32
|
+
```
|
33
|
+
|
28
34
|
## Configuration
|
29
35
|
Edit `config/initializers/rails_api_guard.rb` to set request limits, expiry time, Slack alerts, and excluded endpoint patterns.
|
30
36
|
|
37
|
+
## Slack Notifications
|
38
|
+
|
39
|
+
RailsApiGuard can optionally send Slack alerts whenever a request exceeds the defined rate limit threshold.
|
40
|
+
|
41
|
+
### Configuration
|
42
|
+
|
43
|
+
To enable Slack alerts, set your Slack webhook URL in the initializer:
|
44
|
+
|
45
|
+
```ruby
|
46
|
+
# config/initializers/rails_api_guard.rb
|
47
|
+
RailsApiGuard.configure do |config|
|
48
|
+
config.slack_webhook_url = ENV["SLACK_WEBHOOK_URL"]
|
49
|
+
end
|
50
|
+
|
31
51
|
## Contributing
|
32
52
|
Contribution directions are yet to add.
|
33
53
|
|
@@ -10,6 +10,20 @@ module RailsApiGuard
|
|
10
10
|
def copy_initializer
|
11
11
|
template "rails_api_guard_initializer.rb", "config/initializers/rails_api_guard.rb"
|
12
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
|
13
27
|
end
|
14
28
|
end
|
15
29
|
end
|
@@ -5,11 +5,22 @@ module RailsApiGuard
|
|
5
5
|
class SlackNotifier
|
6
6
|
def self.notify(message)
|
7
7
|
webhook_url = RailsApiGuard.config.slack_webhook_url
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
8
|
+
|
9
|
+
if webhook_url.nil?
|
10
|
+
Rails.logger.warn 'no Slack webhook URl configured'
|
11
|
+
Rails.logger.warn "Message : #{message}"
|
12
|
+
return
|
13
|
+
end
|
14
|
+
|
15
|
+
response = HTTParty.post(
|
16
|
+
webhook_url,
|
17
|
+
body: { text: message }.to_json,
|
18
|
+
headers: { 'Content-Type' => 'application/json' }
|
19
|
+
)
|
20
|
+
|
21
|
+
unless rails.success?
|
22
|
+
Rails.logger.error "Failed to send Slack alert: #{response.code} - #{response.body}"
|
23
|
+
end
|
13
24
|
end
|
14
25
|
end
|
15
26
|
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.2.
|
4
|
+
version: 0.2.2
|
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-
|
11
|
+
date: 2025-07-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|