Skip to content

Commit

Permalink
Custom object-type and response bodies.
Browse files Browse the repository at this point in the history
  • Loading branch information
ecnepsnai committed May 18, 2015
1 parent 19ea4a1 commit 619ff93
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
14 changes: 7 additions & 7 deletions lib/rack/throttle.rb
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
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 619ff93

Please sign in to comment.