class HipTail::Manager
Attributes
Public Class Methods
A new instance of HipTail::Manager
. @param [Hash] params ({}) @option params [HipTail::AuthorityProvider] :authority_provider (new instance of HipTail::MemoryAuthorityProvider
) @return [HipTail::Manager]
# File lib/hiptail/manager.rb, line 15 def initialize(params = {}) @authority_provider = params[:authority_provider] || MemoryAuthorityProvider.new @authority_manager = AuthorityManager.new(@authority_provider) @hook = {} [ :install, :uninstall, :event, :room_messaging, :room_message, :room_notification, :room_topic_change, :room_visiting, :room_enter, :room_exit, ].each do |hook_type| @hook[hook_type] = {} end end
Public Instance Methods
Handles events (room_message, room_enter, etc.). @param [Hash] params Request object (originally represented in JSON) from HipChat Server on installation. @return [void]
# File lib/hiptail/manager.rb, line 155 def handle_event(params) event = Event.parse(params) event.authority = self.authority[event.oauth_client_id] call_hooks :event, event if event.is_a?(Event::RoomMessaging) call_hooks :room_messaging, event case event when Event::RoomMessage call_hooks :room_message, event when Event::RoomNotification call_hooks :room_notification, event end elsif event.is_a?(Event::RoomTopicChange) call_hooks :room_topic_change, event elsif event.is_a?(Event::RoomVisiting) call_hooks :room_visiting, event case event when Event::RoomEnter call_hooks :room_enter, event when Event::RoomExit call_hooks :room_exit, event end end end
Handles installing request. @param [Hash] params Request object (originally represented in JSON) from HipChat Server on installation. @return [void]
# File lib/hiptail/manager.rb, line 133 def handle_install(params) authority = build_authority(params) @authority_provider.register(authority.oauth_id, authority) call_hooks :install, authority end
Handles uninstalling request. @note Uninstall event will be fired after uninstallation on the server.
So you cannot use oauth information to do something (e.g. sending notification) on uninstallation phase.
@param [String] oauth_id Corresponding OAuth ID @return [void]
# File lib/hiptail/manager.rb, line 146 def handle_uninstall(oauth_id) call_hooks :uninstall, oauth_id @authority_provider.unregister(oauth_id) end
Registers hook on events. @return [String] Hook ID @yield [event] @yield [HipTail::Event] event
# File lib/hiptail/manager.rb, line 70 def on_event(*args, &block) register_hook :event, args, block end
Registers hook on installation. @return [String] Hook ID @yield [authority] @yield [HipTail::Authority] authority
# File lib/hiptail/manager.rb, line 54 def on_install(*args, &block) register_hook :install, args, block end
Registers hook on room_enter event. @return [String] Hook ID @yield [event] @yield [HipTail::Event::RoomEnter] event
# File lib/hiptail/manager.rb, line 118 def on_room_enter(*args, &block) register_hook :room_enter, args, block end
Registers hook on room_exit event. @return [String] Hook ID @yield [event] @yield [HipTail::Event::RoomExit] event
# File lib/hiptail/manager.rb, line 126 def on_room_exit(*args, &block) register_hook :room_exit, args, block end
Registers hook on room_message event. @return [String] Hook ID @yield [event] @yield [HipTail::Event::RoomMessage] event
# File lib/hiptail/manager.rb, line 86 def on_room_message(*args, &block) register_hook :room_message, args, block end
Registers hook on messaging events (room_message and room_notification). @return [String] Hook ID @yield [event] @yield [HipTail::Event::RoomMessaging] event
# File lib/hiptail/manager.rb, line 78 def on_room_messaging(*args, &block) register_hook :room_messaging, args, block end
Registers hook on room_notification event. @return [String] Hook ID @yield [event] @yield [HipTail::Event::RoomNotification] event
# File lib/hiptail/manager.rb, line 94 def on_room_notification(*args, &block) register_hook :room_notification, args, block end
Registers hook on room_topic_change event. @return [String] Hook ID @yield [event] @yield [HipTail::Event::RoomTopicChange] event
# File lib/hiptail/manager.rb, line 102 def on_room_topic_change(*args, &block) register_hook :room_topic_change, args, block end
Registers hook on room visiting event (room_enter and room_exit). @return [String] Hook ID @yield [event] @yield [HipTail::Event::RoomVisiting] event
# File lib/hiptail/manager.rb, line 110 def on_room_visiting(*args, &block) register_hook :room_visiting, args, block end
Registers hook on uninstallation. @return [String] Hook ID @yield [oauth_id] @yield [String] oauth_id
# File lib/hiptail/manager.rb, line 62 def on_uninstall(*args, &block) register_hook :uninstall, args, block end
Registers a hook. @param [Symbol] hook_type @param [Proc] block @param [String] hook_id @return [String] Hook ID
# File lib/hiptail/manager.rb, line 189 def register_hook(hook_type, args, block) priority = args.size > 0 ? args.shift : 100 @hook[hook_type][priority] ||= [] @hook[hook_type][priority] << block end
Private Instance Methods
# File lib/hiptail/manager.rb, line 197 def call_hooks(hook_type, *args) @hook[hook_type].keys.sort.each do |key| @hook[hook_type][key].each do |block| aborted = false begin r = block.call(*args) rescue LocalJumpError => e aborted = e.reason raise e unless [:break, :next, :return].include?(aborted) r = e.exit_value end break if aborted == :break end end end