Skip to content

Commit

Permalink
Merge pull request #17 from ecnepsnai/master
Browse files Browse the repository at this point in the history
Added support for custom content types and response bodies.
  • Loading branch information
artob committed Nov 8, 2015
2 parents c8fcd00 + 619ff93 commit 06a9026
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 11 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
14 changes: 7 additions & 7 deletions lib/rack/throttle.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
9 changes: 7 additions & 2 deletions lib/rack/throttle/limiter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

##
Expand Down

0 comments on commit 06a9026

Please sign in to comment.