bkarst / throttle_me

Real time APIs like Twitter and Reddit limit the amount of requests per credential per time window. Throttle Me is a gem intended to make single or multi process ruby applications sensitive to these rate limits.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ThrottleMe

Install

Redis is a required dependency. Make sure you have both redis-server installed and running before use.

Add the following line to Gemfile:

gem 'throttle_me', :git => "git://github.com/bkarst/throttle_me.git"

and run bundle install from your shell.

Usage without Blocks

ThrottleMe.request will return either the boolean value true or the number of seconds you need to wait until it is safe to call the API without violating the rate limit.


  token = 'api_token'
  window_size = 60 #window size in seconds
  requests_per_window_size = 60 #requests per windo
  res = ThrottleMe.request(@token,
      window_size: window_size,
      requests_per_window_size: requests_per_window_size)
  if res == true
    puts "RUN MY CODE"
  else
    puts res #number of seconds until it is safe to call the API
  end

Usage with Blocks

  token = 'api_token'
  window_size = 60 #window size in seconds
  requests_per_window_size = 60 #requests per windo
  res = ThrottleMe.request(@token, window_size: window_size,
    requests_per_window_size: requests_per_window_size) do
    puts "RUN MY CODE" #this code will only be run if you are allowed
  end  

About

Real time APIs like Twitter and Reddit limit the amount of requests per credential per time window. Throttle Me is a gem intended to make single or multi process ruby applications sensitive to these rate limits.


Languages

Language:Ruby 100.0%