Skip to content

Commit 986af2e

Browse files
author
wangxz
committed
Add Usage
1 parent 926c60b commit 986af2e

File tree

4 files changed

+19
-10
lines changed

4 files changed

+19
-10
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ test/tmp
1616
test/version_tmp
1717
tmp
1818
*~
19+
nbproject/*

README.md

+15-7
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,32 @@
11
# Limiter
22

3-
TODO: Write a gem description
3+
Rack middleware for rate-limiting incoming HTTP requests with black_list and white_list support.
44

55
## Installation
66

77
Add this line to your application's Gemfile:
88

9-
gem 'limiter'
9+
gem 'limiter', :git => "git://github.com/csdn-dev/limiter.git"
1010

1111
And then execute:
1212

1313
$ bundle
1414

15-
Or install it yourself as:
16-
17-
$ gem install limiter
18-
1915
## Usage
2016

21-
TODO: Write usage instructions here
17+
```ruby
18+
# config/initializers/limiter.rb
19+
require File.expand_path("../redis", __FILE__)
20+
Rails.configuration.app_middleware.insert_before(Rack::MethodOverride,
21+
Limiter::RateLimiter,
22+
:black_list => Limiter::BlackList.new($redis),
23+
:white_list => Limiter::WhiteList.new($redis),
24+
:allow_path => Rails.env.development? ? /^\/(assets|human_validations|simple_captcha)/ :
25+
/^\/(human_validations|simple_captcha)/,
26+
:message => "<a href='/human_validations/new'>我不是机器人</a>",
27+
:visit_counter => Limiter::VisitCounter.new($redis)
28+
)
29+
```
2230

2331
## Contributing
2432

lib/limiter/base.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,8 @@ def rate_limit_exceeded
113113
# @param [Hash{String => String}] headers
114114
# @return [Array(Integer, Hash, #each)]
115115
def http_error(code, message = nil, headers = {})
116-
[code, {'Content-Type' => 'text/plain; charset=utf-8'}.merge(headers),
117-
http_status(code) + (message.nil? ? "\n" : " (#{message})\n")]
116+
[code, {'Content-Type' => 'text/html; charset=utf-8'}.merge(headers),
117+
[http_status(code) + (message.nil? ? "\n" : " (#{message})\n")]]
118118
end
119119

120120
##

lib/limiter/version.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
# -*- encoding : utf-8 -*-
22
module Limiter
3-
VERSION = "0.0.1"
3+
VERSION = "0.0.2"
44
end

0 commit comments

Comments
 (0)