Skip to content
Open
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
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ begin
gem.summary = %Q{Rate limiting for Rails action controllers.}
gem.description = %Q{Rate limiting for Rails action controllers.}
gem.email = "ssayles@users.sourceforge.net"
gem.homepage = "http://github.com/ssayles/curbit"
gem.homepage = "http://github.com/codemariner/curbit"
gem.authors = ["Scott Sayles"]
gem.add_development_dependency "thoughtbot-shoulda"
end
Expand Down
55 changes: 27 additions & 28 deletions curbit.gemspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Generated by jeweler
# DO NOT EDIT THIS FILE
# Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
# DO NOT EDIT THIS FILE DIRECTLY
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
# -*- encoding: utf-8 -*-

Gem::Specification.new do |s|
Expand All @@ -9,49 +9,47 @@ Gem::Specification.new do |s|

s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Scott Sayles"]
s.date = %q{2010-01-26}
s.date = %q{2011-02-16}
s.description = %q{Rate limiting for Rails action controllers.}
s.email = %q{ssayles@users.sourceforge.net}
s.extra_rdoc_files = [
"LICENSE",
"README.rdoc"
"README.rdoc"
]
s.files = [
".gitignore",
"LICENSE",
"Manifest",
"README.rdoc",
"Rakefile",
"VERSION",
"curbit.gemspec",
"init.rb",
"lib/curbit.rb",
"test/conditional_controller_test.rb",
"test/custom_key_controller_test.rb",
"test/custom_message_format_controller.rb",
"test/standard_controller_test.rb",
"test/test_helper.rb",
"test/test_rails_helper.rb"
"LICENSE",
"Manifest",
"README.rdoc",
"Rakefile",
"VERSION",
"curbit.gemspec",
"init.rb",
"lib/curbit.rb",
"test/conditional_controller_test.rb",
"test/custom_key_controller_test.rb",
"test/custom_message_format_controller.rb",
"test/standard_controller_test.rb",
"test/test_helper.rb",
"test/test_rails_helper.rb"
]
s.homepage = %q{http://github.com/ssayles/curbit}
s.rdoc_options = ["--charset=UTF-8"]
s.homepage = %q{http://github.com/codemariner/curbit}
s.require_paths = ["lib"]
s.rubygems_version = %q{1.3.5}
s.rubygems_version = %q{1.3.7}
s.summary = %q{Rate limiting for Rails action controllers.}
s.test_files = [
"test/conditional_controller_test.rb",
"test/custom_key_controller_test.rb",
"test/custom_message_format_controller.rb",
"test/standard_controller_test.rb",
"test/test_helper.rb",
"test/test_rails_helper.rb"
"test/custom_key_controller_test.rb",
"test/custom_message_format_controller.rb",
"test/standard_controller_test.rb",
"test/test_helper.rb",
"test/test_rails_helper.rb"
]

if s.respond_to? :specification_version then
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
s.specification_version = 3

if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
s.add_development_dependency(%q<thoughtbot-shoulda>, [">= 0"])
else
s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
Expand All @@ -60,3 +58,4 @@ Gem::Specification.new do |s|
s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
end
end

29 changes: 20 additions & 9 deletions lib/curbit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,20 @@ def rate_limit_opts_valid?(opts = {})
true
end
end


private

private

def write_to_curbit_cache(cache_key, value, options = {})
Rails.cache.write(cache_key, value, options)
end

def read_from_curbit_cache(cache_key)
Rails.cache.read(cache_key)
end

def delete_from_curbit_cache(cache_key)
Rails.cache.delete(cache_key)
end

def curbit_cache_key(key, method)
# TODO: this won't work if there are more than one controller with
Expand Down Expand Up @@ -114,7 +125,7 @@ def rate_limit_filter(method, opts)

cache_key = curbit_cache_key(key, method)

val = Rails.cache.read(cache_key)
val = read_from_curbit_cache(cache_key)

if (val)
val = val.dup
Expand All @@ -128,7 +139,7 @@ def rate_limit_filter(method, opts)
if started_waiting
# did we exceed the wait time?
if Time.now.to_i > (started_waiting.to_i + opts[:wait_time])
Rails.cache.delete(cache_key)
delete_from_curbit_cache(cache_key)
return true
else
get_message(opts)
Expand All @@ -139,25 +150,25 @@ def rate_limit_filter(method, opts)
if val[:count] > opts[:max_calls]
# start waiting and render the message
val[:started_waiting] = Time.now
Rails.cache.write(cache_key, val, :expires_in => opts[:wait_time])
write_to_curbit_cache(cache_key, val, :expires_in => opts[:wait_time])

get_message(opts)

return false
else
# just update the count
Rails.cache.write(cache_key, val, :expires_in => opts[:wait_time])
write_to_curbit_cache(cache_key, val, :expires_in => opts[:wait_time])
return true
end
else
# we exceeded the time limit, so just reset
val = {:started => Time.now, :count => 1}
Rails.cache.write(cache_key, val, :expires_in => opts[:time_limit])
write_to_curbit_cache(cache_key, val, :expires_in => opts[:time_limit])
return true
end
else
val = {:started => Time.now, :count => 1}
Rails.cache.write(cache_key, val, :expires_in => opts[:time_limit])
write_to_curbit_cache(cache_key, val, :expires_in => opts[:time_limit])
end
end

Expand Down