grape-attack 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 57fc46d111b5ae7aea4b21aae9ed2a93e48ab843
4
- data.tar.gz: b0b1dcbf53255bba00ecb4520231ae81303920a7
3
+ metadata.gz: ea399c91939b6f788d31358112d63f737e089948
4
+ data.tar.gz: fb445757cbb24ac4ffab5c268f0ffb4402ef4fc1
5
5
  SHA512:
6
- metadata.gz: 15379ea693fa354a8558b6723ff14586ec1990c5e028528918fbad082c90b16e30954b4d4941e72b3f28dfa64a4af9804e42d93b4cd40dfbad4c282c447fa8d7
7
- data.tar.gz: 4923d3d2571ac652981e6cccdea912849222bd47c2dbf2afef25bf08a9452e7ca1ea57ef5b04a8bd1c319c285fea725176714a7fd907aada1a80eda45d0c40e2
6
+ metadata.gz: fa0787a890e5a3a0da29abf0a9edca02bf5ec8d570222e2069817bc1845077745001721b1c8702e4a8e747b7e63ad686f5efd67030fc2ff7528116d810c70387
7
+ data.tar.gz: 9606cd3d82d81babfeb544e7905fc60f767029b0437fce2a2879d5a841f5454f309f0ca984d3cd000d5aad02cb267e097d65281ddff5b72e4f30b13415eeefeb
@@ -19,8 +19,8 @@ Gem::Specification.new do |spec|
19
19
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
20
  spec.require_paths = ["lib"]
21
21
 
22
- spec.add_dependency "grape", ">= 0.10"
23
- spec.add_dependency "redis-namespace"
22
+ spec.add_dependency "grape", "~> 0.16"
23
+ spec.add_dependency "redis-namespace", "~> 1.5"
24
24
  spec.add_dependency "activemodel", ">= 4.0"
25
25
  spec.add_dependency "activesupport", ">= 4.0"
26
26
 
@@ -1,4 +1,6 @@
1
1
  require 'active_support/core_ext/numeric/time.rb'
2
+ require 'grape'
3
+
2
4
  require 'grape/attack/version'
3
5
  require 'grape/attack/adapters/redis'
4
6
  require 'grape/attack/adapters/memory'
@@ -43,8 +43,6 @@ module Grape
43
43
  counter.update
44
44
  end
45
45
 
46
- # Fix when https://github.com/ruby-grape/grape/issues/1069
47
- # For now we use route_setting to store :remaining value.
48
46
  def set_rate_limit_headers
49
47
  request.context.route_setting(:throttle)[:remaining] = [0, max_requests_allowed - (counter.value + 1)].max
50
48
  end
@@ -4,16 +4,38 @@ module Grape
4
4
  module Attack
5
5
  class Options
6
6
  include ActiveModel::Model
7
+ include ActiveModel::Validations
7
8
 
8
9
  attr_accessor :max, :per, :identifier, :remaining
9
10
 
10
- validates :max, presence: true
11
- validates :per, presence: true
11
+ class ProcOrNumberValidator < ActiveModel::EachValidator
12
+ def validate_each(record, attribute, value)
13
+ return true if value.is_a?(Numeric)
14
+ return true if value.is_a?(Proc) && value.call.is_a?(Numeric)
15
+
16
+ record.errors.add attribute, "must be either a proc resolving in a numeric or a numeric"
17
+ end
18
+ end
19
+
20
+ validates :max, proc_or_number: true
21
+ validates :per, proc_or_number: true
12
22
 
13
23
  def identifier
14
24
  @identifier || Proc.new {}
15
25
  end
16
26
 
27
+ def max
28
+ return @max if @max.is_a?(Numeric)
29
+ return @max.call if @max.is_a?(Proc)
30
+ super
31
+ end
32
+
33
+ def per
34
+ return @per if @per.is_a?(Numeric)
35
+ return @per.call if @per.is_a?(Proc)
36
+ super
37
+ end
38
+
17
39
  end
18
40
  end
19
41
  end
@@ -14,15 +14,15 @@ module Grape
14
14
  end
15
15
 
16
16
  def method
17
- request.route_method
17
+ request.request_method
18
18
  end
19
19
 
20
20
  def path
21
- request.route_path
21
+ request.path
22
22
  end
23
23
 
24
24
  def params
25
- request.route_params
25
+ request.params
26
26
  end
27
27
 
28
28
  def method_missing(method_name, *args, &block)
@@ -1,4 +1,3 @@
1
- require 'grape'
2
1
  require 'grape/attack/limiter'
3
2
 
4
3
  module Grape
@@ -9,17 +8,16 @@ module Grape
9
8
  ::Grape::Attack::Limiter.new(env).call!
10
9
  end
11
10
 
12
- # Fix when https://github.com/ruby-grape/grape/issues/1069
13
- # For now we use route_setting to store :remaining value.
14
11
  def after
15
12
  request = ::Grape::Attack::Request.new(env)
16
13
 
17
14
  return if ::Grape::Attack.config.disable.call
18
15
  return unless request.throttle?
19
16
 
20
- @app_response['X-RateLimit-Limit'] = request.context.route_setting(:throttle)[:max].to_s
21
- @app_response['X-RateLimit-Remaining'] = request.context.route_setting(:throttle)[:remaining].to_s
22
- @app_response['X-RateLimit-Reset'] = request.context.route_setting(:throttle)[:per].from_now.to_i.to_s
17
+ header('X-RateLimit-Limit', request.throttle_options.max.to_s)
18
+ header('X-RateLimit-Reset', request.throttle_options.per.to_s)
19
+ header('X-RateLimit-Remaining', request.throttle_options.remaining.to_s)
20
+
23
21
  @app_response
24
22
  end
25
23
 
@@ -1,5 +1,5 @@
1
1
  module Grape
2
2
  module Attack
3
- VERSION = '0.2.0'
3
+ VERSION = '0.3.0'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,111 +1,111 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: grape-attack
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pierre-Louis Gottfrois
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-11-27 00:00:00.000000000 Z
11
+ date: 2016-05-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: grape
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - '>='
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '0.10'
19
+ version: '0.16'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - '>='
24
+ - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '0.10'
26
+ version: '0.16'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: redis-namespace
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - '>='
31
+ - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '0'
33
+ version: '1.5'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - '>='
38
+ - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '0'
40
+ version: '1.5'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: activemodel
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - '>='
45
+ - - ">="
46
46
  - !ruby/object:Gem::Version
47
47
  version: '4.0'
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - '>='
52
+ - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '4.0'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: activesupport
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - '>='
59
+ - - ">="
60
60
  - !ruby/object:Gem::Version
61
61
  version: '4.0'
62
62
  type: :runtime
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - '>='
66
+ - - ">="
67
67
  - !ruby/object:Gem::Version
68
68
  version: '4.0'
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: bundler
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
- - - ~>
73
+ - - "~>"
74
74
  - !ruby/object:Gem::Version
75
75
  version: '1.10'
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
- - - ~>
80
+ - - "~>"
81
81
  - !ruby/object:Gem::Version
82
82
  version: '1.10'
83
83
  - !ruby/object:Gem::Dependency
84
84
  name: rake
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
- - - ~>
87
+ - - "~>"
88
88
  - !ruby/object:Gem::Version
89
89
  version: '10.0'
90
90
  type: :development
91
91
  prerelease: false
92
92
  version_requirements: !ruby/object:Gem::Requirement
93
93
  requirements:
94
- - - ~>
94
+ - - "~>"
95
95
  - !ruby/object:Gem::Version
96
96
  version: '10.0'
97
97
  - !ruby/object:Gem::Dependency
98
98
  name: rspec
99
99
  requirement: !ruby/object:Gem::Requirement
100
100
  requirements:
101
- - - '>='
101
+ - - ">="
102
102
  - !ruby/object:Gem::Version
103
103
  version: '0'
104
104
  type: :development
105
105
  prerelease: false
106
106
  version_requirements: !ruby/object:Gem::Requirement
107
107
  requirements:
108
- - - '>='
108
+ - - ">="
109
109
  - !ruby/object:Gem::Version
110
110
  version: '0'
111
111
  description: A middleware for Grape to add endpoint-specific throttling.
@@ -115,9 +115,9 @@ executables: []
115
115
  extensions: []
116
116
  extra_rdoc_files: []
117
117
  files:
118
- - .gitignore
119
- - .rspec
120
- - .travis.yml
118
+ - ".gitignore"
119
+ - ".rspec"
120
+ - ".travis.yml"
121
121
  - CHANGELOG.md
122
122
  - Gemfile
123
123
  - LICENSE.txt
@@ -149,17 +149,17 @@ require_paths:
149
149
  - lib
150
150
  required_ruby_version: !ruby/object:Gem::Requirement
151
151
  requirements:
152
- - - '>='
152
+ - - ">="
153
153
  - !ruby/object:Gem::Version
154
154
  version: '0'
155
155
  required_rubygems_version: !ruby/object:Gem::Requirement
156
156
  requirements:
157
- - - '>='
157
+ - - ">="
158
158
  - !ruby/object:Gem::Version
159
159
  version: '0'
160
160
  requirements: []
161
161
  rubyforge_project:
162
- rubygems_version: 2.4.6
162
+ rubygems_version: 2.5.1
163
163
  signing_key:
164
164
  specification_version: 4
165
165
  summary: A middleware for Grape to add endpoint-specific throttling.