Class: BirdGrinder::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/bird_grinder/base.rb

Overview

A generic base for building handlers. It makes it easy to implement the most common functionality (e.g., clients, checking origin and the like) without having to reinvent the wheel. Typically used as a handler for Perennial::Dispatchable

Direct Known Subclasses

CommandHandler, StreamHandler

Constant Summary collapse

@@handlers =
Hash.new do |h,k|
  h[k] = Hash.new { |h2,k2| h2[k2] = [] }
end

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#clientObject

Returns the value of attribute client.



54
55
56
# File 'lib/bird_grinder/base.rb', line 54

def client
  @client
end

#optionsObject

Returns the value of attribute options.



54
55
56
# File 'lib/bird_grinder/base.rb', line 54

def options
  @options
end

#userObject

Returns the value of attribute user.



54
55
56
# File 'lib/bird_grinder/base.rb', line 54

def user
  @user
end

Class Method Details

.event_handlers_for(name) ⇒ Array<Proc>

Gets event handlers for a given event.

Parameters:

  • name (Symbol)

    the name of the event

Returns:

  • (Array<Proc>)

    the resultant handlers



23
24
25
26
27
28
29
30
31
32
# File 'lib/bird_grinder/base.rb', line 23

def event_handlers_for(name)
  name = name.to_sym
  handlers = []
  klass = self
  while klass != Object
    handlers += @@handlers[klass][name]
    klass = klass.superclass
  end
  return handlers
end

.on_event(name, method_name = nil, &blk) ⇒ Object

Appends a handler for the given event, either as a block / proc or as a symbol (for a method name) which will be called when the event is triggered.

Parameters:

  • name (Symbol)

    the event name

  • method_name (Symbol) (defaults to: nil)

    if present, will call the given instance method

  • blk (Proc)

    the block to call if method_name isn’t given



41
42
43
44
# File 'lib/bird_grinder/base.rb', line 41

def on_event(name, method_name = nil, &blk)
  blk = proc { self.send(method_name) } if method_name.present?
  @@handlers[self][name.to_sym] << blk
end

.register!Object

Registers the current handler instance to be used. If not registered, events wont be triggered



48
49
50
# File 'lib/bird_grinder/base.rb', line 48

def register!
  BirdGrinder::Client.register_handler(self.new)
end

Instance Method Details

#dm(user, message, opts = {}) ⇒ Object

Direct Messages a specific user if the client exists.

See Also:



87
88
89
# File 'lib/bird_grinder/base.rb', line 87

def dm(user, message, opts = {})
  @client && @client.dm(user, message, opts)
end

#handle(message, options) ⇒ Object

Handles a message / event from a dispatcher. This triggers each respective part of the client / lets us act on events.

Parameters:

  • message (Symbol)

    the name of the event, e.g. :incoming_mention

  • options (Hash, BirdGrinder::Nash)

    the options / params for the given event.



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/bird_grinder/base.rb', line 61

def handle(message, options)
  begin
    setup_details(message, options)
    h = self.class.event_handlers_for(message)
    h.each { |handle| self.instance_eval(&handle) }
  rescue Exception => e
    raise e if e.is_a?(BirdGrinder::HaltHandlerProcessing)
    logger.fatal "Exception processing handlers for #{message}:"
    logger.log_exception(e)
  ensure
    reset_details
  end
end

#reply(message, opts = {}) ⇒ Object

Replies to the last received message in the correct format. if the last message direct, it will send a dm otherwise it will send a tweet with the correct @-prefix and :in_reply_to_status_id set correctly so twitter users can see what it is replying to.

Use the :type option to override the default reply behavior. For example, to reply to an @-mention via direct message, specify :type => :dm

Parameters:

  • message (String)

    the message to reply with

See Also:



102
103
104
105
106
107
108
109
110
111
112
# File 'lib/bird_grinder/base.rb', line 102

def reply(message, opts = {})
  message = message.to_s.strip
  return if @user.blank? || @client.blank? || message.blank?
  if @last_message_direct || opts[:type].to_s == 'dm'
    @client.dm(@user, message)
  else
    opts = {}
    opts[:in_reply_to_status_id] = @last_message_id.to_s if @last_message_id.present?
    @client.reply(@user, message, opts)
  end
end

#tweet(message, opts = {}) ⇒ Object

Tweets a given message.



79
80
81
# File 'lib/bird_grinder/base.rb', line 79

def tweet(message, opts = {})
  @client && @client.tweet(message, opts)
end