summaryrefslogtreecommitdiffstats
path: root/app/controllers/concerns/rate_limit_headers.rb
diff options
context:
space:
mode:
authorMatt Jankowski <mjankowski@thoughtbot.com>2017-06-07 11:23:26 -0400
committerEugen Rochko <eugen@zeonfederated.com>2017-06-07 17:23:26 +0200
commitf0634ba876639fcd7e506466683bf71ae81362d4 (patch)
tree8adf600ec5eb00979a72b5f9d545fd6dce58fe4f /app/controllers/concerns/rate_limit_headers.rb
parent1d68fe1a60088183e6907a93dc5148b7dd11cdec (diff)
Coverage improvement and concern extraction for rate limit headers in API controller (#3625)
* Coverage for rate limit headers * Move rate limit headers methods to concern * Move throttle check to condition on before_action * Move match_data variable into method * Move utc timestamp to separate method * Move header setting into smaller methods * specs cleanup
Diffstat (limited to 'app/controllers/concerns/rate_limit_headers.rb')
-rw-r--r--app/controllers/concerns/rate_limit_headers.rb57
1 files changed, 57 insertions, 0 deletions
diff --git a/app/controllers/concerns/rate_limit_headers.rb b/app/controllers/concerns/rate_limit_headers.rb
new file mode 100644
index 00000000000..36cb9107534
--- /dev/null
+++ b/app/controllers/concerns/rate_limit_headers.rb
@@ -0,0 +1,57 @@
+# frozen_string_literal: true
+
+module RateLimitHeaders
+ extend ActiveSupport::Concern
+
+ included do
+ before_action :set_rate_limit_headers, if: :rate_limited_request?
+ end
+
+ private
+
+ def set_rate_limit_headers
+ apply_header_limit
+ apply_header_remaining
+ apply_header_reset
+ end
+
+ def rate_limited_request?
+ !request.env['rack.attack.throttle_data'].nil?
+ end
+
+ def apply_header_limit
+ response.headers['X-RateLimit-Limit'] = rate_limit_limit
+ end
+
+ def rate_limit_limit
+ api_throttle_data[:limit].to_s
+ end
+
+ def apply_header_remaining
+ response.headers['X-RateLimit-Remaining'] = rate_limit_remaining
+ end
+
+ def rate_limit_remaining
+ (api_throttle_data[:limit] - api_throttle_data[:count]).to_s
+ end
+
+ def apply_header_reset
+ response.headers['X-RateLimit-Reset'] = rate_limit_reset
+ end
+
+ def rate_limit_reset
+ (request_time + reset_period_offset).iso8601(6)
+ end
+
+ def api_throttle_data
+ request.env['rack.attack.throttle_data']['api']
+ end
+
+ def request_time
+ @_request_time ||= Time.now.utc
+ end
+
+ def reset_period_offset
+ api_throttle_data[:period] - request_time.to_i % api_throttle_data[:period]
+ end
+end