Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom Response and Objects #17

Merged
merged 2 commits into from
Nov 8, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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