Class: Leech::Handler Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/leech/handler.rb

Overview

This class is abstract.

Handlers are extending functionality of server by defining custom command handling callbacks or server instance methods.

Examples:

Writing own handler

class CustomHandler < Leech::Handler
  def self.used(server)
    server.instance_eval { extend ServerMethods }
  end

  module ServerMethods
    def say_hello
      answer("Hello!")
    end
  end

  handle /^Hello server!$/, :say_hello
  handle /^Bye!$/ do |env,params|
    env.answer("Take care!")
  end
end

Enabling handlers by server

Leech::Server.new { use CustomHandler }

Direct Known Subclasses

Leech::Handlers::Auth

Defined Under Namespace

Classes: Error

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env) ⇒ Handler

Constructor.

Parameters:



81
82
83
# File 'lib/leech/handler.rb', line 81

def initialize(env)
  @env = env
end

Instance Attribute Details

#envLeecher::Server (readonly)

Returns Passed server instance.

Returns:

  • (Leecher::Server)

    Passed server instance



71
72
73
# File 'lib/leech/handler.rb', line 71

def env
  @env
end

#paramsArray<String> (readonly)

Returns Parameters matched in passed command.

Returns:

  • (Array<String>)

    Parameters matched in passed command



75
76
77
# File 'lib/leech/handler.rb', line 75

def params
  @params
end

Class Method Details

.handle(pattern, method = nil, &block) ⇒ Object

It defines matching pattern and callback related with. When specified command will match this pattern then callback will be called.

Examples:

handle(/^PRINT) (.*)$/ {|env,params| print params[0]}
handle(/^DELETE FILE (.*)$/) {|env,params| File.delete(params[0])}
handle(/^HELLO$/, :say_hello)

Parameters:

  • patern (Regexp)

    Block will be executed for passed command will be maching it.

  • method (Symbol, nil) (defaults to: nil)

    Name of server method, which should be called when pattern will match with command

Raises:

  • (Leech::Error)

    When specified callback is not valid method name or Proc.



52
53
54
55
56
57
58
59
60
61
# File 'lib/leech/handler.rb', line 52

def self.handle(pattern, method=nil, &block)
  if block_given?
    method = block
  end
  if method.is_a?(Proc) || method.is_a?(Symbol)
    self.matchers[pattern] = method
  else
    raise Error, "Invalid handler callback"
  end
end

.matchersObject

List of declared matchers



32
33
34
# File 'lib/leech/handler.rb', line 32

def self.matchers
  @matchers ||= {}
end

.used(server) ⇒ Object

You should implement this method in your handler eg. if you would like to modify server class.



65
66
67
# File 'lib/leech/handler.rb', line 65

def self.used(server)
  # nothing...
end

Instance Method Details

#callObject

This method can be called only after #match. It executes callback related with matched pattern.

Raises:

  • (Leech::Error)

    When @matcher is not defined, which means that #match method wasn’t called before.



109
110
111
112
113
114
115
116
117
118
# File 'lib/leech/handler.rb', line 109

def call
  case @matcher
  when Proc
    @matcher.call(env, params)
  when String, Symbol 
    env.send(@matcher.to_sym, params)
  else
    raise Error, "Can not call unmatched command"
  end
end

#match(command) ⇒ Leech::Handler?

Compare specified command with declared patterns.

Parameters:

  • command (String)

    Command to handle.

Returns:

  • (Leech::Handler, nil)

    When command match one of declared patterns then it returns itself, otherwise it returns nil.



93
94
95
96
97
98
99
100
101
# File 'lib/leech/handler.rb', line 93

def match(command)
  self.class.matchers.each_pair do |p,m|
    if @params = p.match(command)
      @matcher = m
      return self
    end
  end
  nil
end