class HipTail::Manager

Attributes

authority_provider[R]

@return [HipTail::AuthorityProvider]

Public Class Methods

new(params = {}) click to toggle source

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

authority() click to toggle source

Retrieves authority from oauth_id @attribute [r] authority @example

authority = manager.authority[oauth_id]

@return [HipTail::Manager::AuthorityManager]

# File lib/hiptail/manager.rb, line 46
def authority
  @authority_manager
end
authority_provider=(provider) click to toggle source

@param [HipTail::AuthorityProvider] provider @return [HipTail::AuthorityProvider]

# File lib/hiptail/manager.rb, line 37
def authority_provider=(provider)
  @authority_provider = @authority_manager.authority_provider = provider
end
handle_event(params) click to toggle source

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
handle_install(params) click to toggle source

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
handle_uninstall(oauth_id) click to toggle source

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
on_event(*args, &block) click to toggle source

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
on_install(*args, &block) click to toggle source

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
on_room_enter(*args, &block) click to toggle source

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
on_room_exit(*args, &block) click to toggle source

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
on_room_message(*args, &block) click to toggle source

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
on_room_messaging(*args, &block) click to toggle source

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
on_room_notification(*args, &block) click to toggle source

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
on_room_topic_change(*args, &block) click to toggle source

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
on_room_visiting(*args, &block) click to toggle source

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
on_uninstall(*args, &block) click to toggle source

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
register_hook(hook_type, args, block) click to toggle source

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

build_authority(params) click to toggle source
# File lib/hiptail/manager.rb, line 214
def build_authority(params)
  server_cap = JSON.parse(URI.parse(params['capabilitiesUrl']).read)
  oauth2_info = server_cap['capabilities']['oauth2Provider']
  api_base    = server_cap['links']['api']

  HipTail::Authority.new(
    :oauth_id          => params['oauthId'],
    :oauth_secret      => params['oauthSecret'],
    :room_id           => params['roomId'],
    :group_id          => params['groupId'],
    :authorization_url => oauth2_info['authorizationUrl'],
    :token_url         => oauth2_info['tokenUrl'],
    :api_base          => server_cap['links']['api'],
  )
end
call_hooks(hook_type, *args) click to toggle source
# 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