Gem that limits the rate at which ActiveRecord model instances can be created.
Ruby HTML JavaScript CSS
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
lib
spec
.gitignore
.rspec
.rubocop.yml
.ruby-gemset
.ruby-version
CHANGELOG.md
Gemfile
Gemfile.lock
README.md
Rakefile
rate_limiter.gemspec

README.md

Rate Limiter

A gem that limits the rate at which ActiveRecord model instances can be created.

Rails Version

The tests for this gem use Rails 5. However, any version of Rails from 3.2 onward should be compatible.

Installation

Add the gem to your project's Gemfile:

gem 'rate_limiter'

Basic Usage

In the models you want to rate limit simply call the rate_limit method inside the model.

class ProductReview < ActiveRecord::Base
  rate_limit
end

By default this will rate limit creation of instances using the ip_address attribute with an interval of one minute. This is a pretty big assumption (that may change in future versions) since there's a good chance you don't have an ip_address attribute on your model. If that's the case then you can do the following:

class ProductReview < ActiveRecord::Base
  rate_limit on: :username
end

This will instead check for ProductReviews with a matching username instead.

Because you may want to increase or decrease the interval between creating instances of your model you can do this:

class ProductReview < ActiveRecord::Base
  rate_limit interval: 3.hours
end

Contributing

If you feel like you can add something useful to rate_limiter then don't hesitate to contribute! To make sure your fix/feature has a high chance of being included, please do the following:

  1. Fork the repo.

  2. Run the tests. I will only take pull requests with passing tests, and it's great to know that you have a clean slate: bundle && rake

  3. Add a test for your change. Only adding tests for existing code, refactoring, and documentation changes require no new tests. If you are adding functionality or fixing a bug, you need a test!

  4. Make the test pass.

  5. Push to your fork and submit a pull request.

I can't guarantee that I will accept the change, but if I don't I will be sure to let you know why!

Some things that will increase the chance that your pull request is accepted, taken straight from the Ruby on Rails guide:

  • Use Rails idioms and helpers
  • Include tests that fail without your code, and pass with it
  • Update the documentation, guides, or whatever is affected by your contribution

Yes, I am well aware of the irony of asking for tests when there are effectively none right now. This gem is a work in progress.