lib/ratelimit/bucketbased.rb
def use(name, cost=nil)
			# create a bucket using the default config if it doesn't already exist
			bkt = @storage.get(name)
			unless bkt
				create_bucket(name)
				bkt = @storage.get(name)
			end
			unless bkt
				raise Exception, "Could not find bucket"
			end
			# first credit the bucket for the time that has elapsed
			epochs_elapsed = ((Time.now.to_f - bkt.last_refill)/bkt.refill_epoch).to_i
			bkt.current += epochs_elapsed * bkt.refill_amount
			bkt.current = bkt.max if bkt.current > bkt.max
			bkt.last_refill += epochs_elapsed*bkt.refill_epoch
			# now see if the bkt has enough to provide service
			cost ||= bkt.cost # if the cost isn't provided, use the default cost
			raise "Invalid cost: #{cost}" if cost < 0
			enough = bkt.current >= cost # true if sufficient, false if insufficient
			# track the total costs, but only if service will be rendered
			bkt.total_used += cost if enough
			# now deduct the cost, capping at the minimum
			bkt.current -= cost
			bkt.current = bkt.min if bkt.current < bkt.min
			# now save the changes into the storage (if memory, then no changes are needed, we updated the object in memory)
			@storage.update(bkt)
			# return the verdict, did they have enough credits to pay the toll?
			enough
		end
def use(name, cost=nil)
			# create a bucket using the default config if it doesn't already exist
			bkt = @storage.get(name)
			unless bkt
				create_bucket(name)
				bkt = @storage.get(name)
			end
			unless bkt
				raise Exception, "Could not find bucket"
			end
			# first credit the bucket for the time that has elapsed
			epochs_elapsed = ((Time.now.to_f - bkt.last_refill)/bkt.refill_epoch).to_i
			bkt.current += epochs_elapsed * bkt.refill_amount
			bkt.current = bkt.max if bkt.current > bkt.max
			bkt.last_refill += epochs_elapsed*bkt.refill_epoch
			# now see if the bkt has enough to provide service
			cost ||= bkt.cost # if the cost isn't provided, use the default cost
			raise "Invalid cost: #{cost}" if cost < 0
			enough = bkt.current >= cost # true if sufficient, false if insufficient
			# track the total costs, but only if service will be rendered
			bkt.total_used += cost if enough
			# now deduct the cost, capping at the minimum
			bkt.current -= cost
			bkt.current = bkt.min if bkt.current < bkt.min
			# now save the changes into the storage (if memory, then no changes are needed, we updated the object in memory)
			@storage.update(bkt)
			# return the verdict, did they have enough credits to pay the toll?
			enough
		end
def use ( name , cost = nil ) # create a bucket using the default config if it doesn't already exist bkt = @storage . get ( name ) unless bkt create_bucket ( name ) bkt = @storage . get ( name ) end unless bkt raise Exception , "Could not find bucket" end # first credit the bucket for the time that has elapsed epochs_elapsed = ( ( Time . now . to_f - bkt . last_refill ) / bkt . refill_epoch ) . to_i bkt . current += epochs_elapsed * bkt . refill_amount bkt . current = bkt . max if bkt . current > bkt . max bkt . last_refill += epochs_elapsed bkt . refill_epoch # now see if the bkt has enough to provide service cost ||= bkt . cost # if the cost isn't provided, use the default cost raise "Invalid cost: #{cost}" if cost < 0 enough = bkt . current >= cost # true if sufficient, false if insufficient # track the total costs, but only if service will be rendered bkt . total_used += cost if enough # now deduct the cost, capping at the minimum bkt . current -= cost bkt . current = bkt . min if bkt . current < bkt . min # now save the changes into the storage (if memory, then no changes are needed, we updated the object in memory) @storage . update ( bkt ) # return the verdict, did they have enough credits to pay the toll? enough end
Returns <i>true</i> if the item <tt>name</tt> has enough credits, <i>false</i> otherwise
 It will automatically create buckets for items that don't already have buckets and it will do all the bookkeeping to deduct credits, regenerate credits, and track all the credits used.
 <tt>name</tt>:: the name of the item to track
 <tt>cost</tt>:: the cost of the transaction (defaults to the cost set in the Bucket if nil)
Returns <i > true< / i > if the item <tt > name< / tt > has enough credits <i > false< / i > otherwise It will automatically create buckets for items that don t already have buckets and it will do all the bookkeeping to deduct credits regenerate credits and track all the credits used . <tt > name< / tt > :: the name of the item to track <tt > cost< / tt > :: the cost of the transaction ( defaults to the cost set in the Bucket if nil )