Skip to content

Instantly share code, notes, and snippets.

@jameswritescode
Last active December 26, 2015 16:09
Show Gist options
  • Select an option

  • Save jameswritescode/7178257 to your computer and use it in GitHub Desktop.

Select an option

Save jameswritescode/7178257 to your computer and use it in GitHub Desktop.

Plugins

Plugins are centric to the RubyServ ecosystem because it is what handles how your services will function.

Plugins go into the plugins directory, and the best example of one of the plugins is RubyServ itself, found in plugins/core.rb.

The only thing required in a plugin are include RubyServ::Plugin and configure. The module name doesn't matter, but stay out of the RubyServ namespace to avoid namespace clashes.

configure(&block)

configure is a very important method that is used to configure what the bots nickname, hostname, realname, etc, are.

Parameters:

  • nickname (String) (Required)
  • hostname (String) (Optional)
  • username (String) (Optional)
  • realname (String) (Optional)
  • channels (Array) (Optional)

Example:

module SomeServ
  include RubyServ::Plugin
  
  configure do |config|
    config.nickname = 'SomeServ'
    config.hostname = 'someserv.does.something'
    # ...
  end
end

match(/pattern/, options = {}, &block)

Parameters:

  • /pattern/ (Regexp) (Required)
  • options (Hash) (Optional)
  • &block (????) (Required)

Supported options:

  • prefix (default: true)

Example:

module SomeServ
  include RubyServ::Plugin
  
  # ...
  match(/some (\S+)/) do |m, param|
    m.reply "some #{param} called by #{m.user.nickname}"
  end
  # ...
end

When greating groups when in the pattern (in this case, (\S+)) match will take a param for each group. So:

match(/some (\S+) (\S+)/) would yield you: |m, first, second|

event(event, options = {}, &block)

Parameters:

  • event (Symbol) (Required)
  • option (Hash) (Optional)
  • &block (????) (Required)

Supported options:

Example:

module SomeServ
  include RubyServ::Plugin
  
  # ...
  event :privmsg do |m|
    m.reply "#{m.user.nickname} someone said something!"
  end
  # ...
end

web(request_type, route, &block)

web definitions are translated to Sinatra routes, so if you are familiar with Sinatra some of it's magic is available to you.

Parameters:

  • request_type (Symbol) (Required)
  • route (String) (Required)
  • &block (????) (Required)

Example:

module SomeServ
  include RubyServ::Plugin
  
  # ...
  web :post, '/testing' do
    RubyServ::IRC::Client.find_by_nickname(@nickname).first.message('#channel', 'there was a POST to /testing!')
  end
  # ...
end

Be unique with your routes per service, or you'll be looking at clashes.

before_match(method_name)

before_match is designed to act like before_filter in rails, so it should not be very unfamiliar. It simlply runs all methods that before_match specifics before match and event are run. This does not apply to web.

Parameters:

  • method_name (Symbol) (Required)

Example:

module SomeServ
  include RubyServ::Plugin
  
  before_match :test
  
  match(/google/) do |m|
    # ...
  end
  
  def test
    # ...
  end
end

Small warning about before_match is that if instance variables are assigned they will be accessible to the other methods. Do with that what you will.

@jameswritescode

Copy link
Copy Markdown
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment