From 19ea4a1248d301e60576db2464901371d7bc0cc4 Mon Sep 17 00:00:00 2001 From: Ian Spence Date: Sun, 17 May 2015 23:36:23 -0700 Subject: [PATCH 1/2] Updated readme --- README.md | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index b14264c..715c161 100755 --- a/README.md +++ b/README.md @@ -1,5 +1,11 @@ -HTTP Request Rate Limiter for Rack Applications -=============================================== +HTTP Request Rate Limiter for the Vancity Transit API +===================================================== + +This is a fork of [bendiken/rack-throttle](https://github.com/bendiken/rack-throttle) for use exclusively in the Vancity Transit API. +The modifications to this module allow for the API to rate limit client +requests and provide a custom JSON formatted response. + +--- This is [Rack][] middleware that provides logic for rate-limiting incoming HTTP requests to Rack applications. You can use `Rack::Throttle` with any From 619ff9342be7335d09bd5f1756c4f4f796ce3004 Mon Sep 17 00:00:00 2001 From: Ian Spence Date: Sun, 17 May 2015 23:36:49 -0700 Subject: [PATCH 2/2] Custom object-type and response bodies. --- lib/rack/throttle.rb | 14 +++++++------- lib/rack/throttle/limiter.rb | 9 +++++++-- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/lib/rack/throttle.rb b/lib/rack/throttle.rb index 1049a67..3066ee0 100644 --- a/lib/rack/throttle.rb +++ b/lib/rack/throttle.rb @@ -2,12 +2,12 @@ module Rack module Throttle - autoload :Limiter, 'rack/throttle/limiter' - autoload :Interval, 'rack/throttle/interval' - autoload :TimeWindow, 'rack/throttle/time_window' - autoload :Daily, 'rack/throttle/daily' - autoload :Hourly, 'rack/throttle/hourly' - autoload :Minute, 'rack/throttle/minute' - autoload :VERSION, 'rack/throttle/version' + autoload :Limiter, ::File.expand_path(::File.dirname(__FILE__)) + '/throttle/limiter' + autoload :Interval, ::File.expand_path(::File.dirname(__FILE__)) + '/throttle/interval' + autoload :TimeWindow, ::File.expand_path(::File.dirname(__FILE__)) + '/throttle/time_window' + autoload :Daily, ::File.expand_path(::File.dirname(__FILE__)) + '/throttle/daily' + autoload :Hourly, ::File.expand_path(::File.dirname(__FILE__)) + '/throttle/hourly' + autoload :Minute, ::File.expand_path(::File.dirname(__FILE__)) + '/throttle/minute' + autoload :VERSION, ::File.expand_path(::File.dirname(__FILE__)) + '/throttle/version' end end diff --git a/lib/rack/throttle/limiter.rb b/lib/rack/throttle/limiter.rb index 2268a0d..ce107a6 100644 --- a/lib/rack/throttle/limiter.rb +++ b/lib/rack/throttle/limiter.rb @@ -21,6 +21,7 @@ class Limiter # @option options [String] :key_prefix (nil) # @option options [Integer] :code (403) # @option options [String] :message ("Rate Limit Exceeded") + # @option options [String] :type ("text/plain; charset=utf-8") def initialize(app, options = {}) @app, @options = app, options end @@ -189,8 +190,12 @@ def rate_limit_exceeded(request) # @param [Hash{String => String}] headers # @return [Array(Integer, Hash, #each)] def http_error(code, message = nil, headers = {}) - [code, {'Content-Type' => 'text/plain; charset=utf-8'}.merge(headers), - [http_status(code), (message.nil? ? "\n" : " (#{message})\n")]] + contentType = 'text/plain; charset=utf-8' + if options[:type] + contentType = options[:type] + end + [code, {'Content-Type' => contentType}.merge(headers), + [message]] end ##