Class: BirdGrinder::Base
- Inherits:
-
Object
- Object
- BirdGrinder::Base
- 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
Constant Summary collapse
- @@handlers =
Hash.new do |h,k| h[k] = Hash.new { |h2,k2| h2[k2] = [] } end
Instance Attribute Summary collapse
-
#client ⇒ Object
Returns the value of attribute client.
-
#options ⇒ Object
Returns the value of attribute options.
-
#user ⇒ Object
Returns the value of attribute user.
Class Method Summary collapse
-
.event_handlers_for(name) ⇒ Array<Proc>
Gets event handlers for a given event.
-
.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.
-
.register! ⇒ Object
Registers the current handler instance to be used.
Instance Method Summary collapse
-
#dm(user, message, opts = {}) ⇒ Object
Direct Messages a specific user if the client exists.
-
#handle(message, options) ⇒ Object
Handles a message / event from a dispatcher.
-
#reply(message, opts = {}) ⇒ Object
Replies to the last received message in the correct format.
-
#tweet(message, opts = {}) ⇒ Object
Tweets a given message.
Instance Attribute Details
#client ⇒ Object
Returns the value of attribute client.
54 55 56 |
# File 'lib/bird_grinder/base.rb', line 54 def client @client end |
#options ⇒ Object
Returns the value of attribute options.
54 55 56 |
# File 'lib/bird_grinder/base.rb', line 54 def end |
#user ⇒ Object
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.
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.
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.
87 88 89 |
# File 'lib/bird_grinder/base.rb', line 87 def dm(user, , opts = {}) @client && @client.dm(user, , 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.
61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/bird_grinder/base.rb', line 61 def handle(, ) begin setup_details(, ) h = self.class.event_handlers_for() 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
102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/bird_grinder/base.rb', line 102 def reply(, opts = {}) = .to_s.strip return if @user.blank? || @client.blank? || .blank? if || opts[:type].to_s == 'dm' @client.dm(@user, ) else opts = {} opts[:in_reply_to_status_id] = .to_s if .present? @client.reply(@user, , opts) end end |
#tweet(message, opts = {}) ⇒ Object
Tweets a given message.
79 80 81 |
# File 'lib/bird_grinder/base.rb', line 79 def tweet(, opts = {}) @client && @client.tweet(, opts) end |