Skip to content
This repository has been archived by the owner on Oct 2, 2024. It is now read-only.

mwoods79/bubot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Bubot

Watch a method. If it takes longer than a specified amount of time, execute a block. It's a callback that only happens after a threshold.

Requirements

ruby > 2.0.0

Installation

Add this line to your application's Gemfile:

gem 'bubot'

And then execute:

$ bundle

Or install it yourself as:

$ gem install bubot

Usage

Include Bubot in your class.

This gives you the class method .watch(:method_name, options).

If a watched method takes longer than options[:timeout], the block will execute. Remember, the timeout is 0 by default so if you don't pass it a timeout, the block will always execute (like an after callback).

Example

class WebAPI
  include Bubot

  watch(:response, timeout: 2) do |web_api_object, time_it_took, method_response|
    puts web_api_object   # => web_api_object instance
    puts time_it_took     # => 3.5 (seconds)
    puts method_response  # => "body"
  end

  def response
    sleep 3
    "body"
  end
end

You can also pass any object that responds to call by using the :with option.

class LoggingStrategy

  def self.call(web_api_object, time_it_took, method_response)
    puts web_api_object   # => web_api_object instance
    puts time_it_took     # => 5.2 (seconds)
    puts method_response  # => "body"
  end

end

class WebAPI
  include Bubot

  watch :response, timeout: 3, with: LoggingStrategy

  def response
    sleep 5
    "body"
  end
end

About

Take action when methods take too long

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages