Class: HipTail::Manager

Inherits:
Object
  • Object
show all
Defined in:
lib/hiptail/manager.rb

Defined Under Namespace

Classes: AuthorityManager

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ HipTail::Manager

A new instance of HipTail::Manager.

Parameters:

  • params (Hash) (defaults to: {})

    ({})

Options Hash (params):



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# 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

Instance Attribute Details

#authorityHipTail::Manager::AuthorityManager (readonly)

Retrieves authority from oauth_id

Examples:

authority = manager.authority[oauth_id]

Returns:



46
47
48
# File 'lib/hiptail/manager.rb', line 46

def authority
  @authority_manager
end

#authority_providerHipTail::AuthorityProvider



33
34
35
# File 'lib/hiptail/manager.rb', line 33

def authority_provider
  @authority_provider
end

Instance Method Details

#handle_event(params) ⇒ void

This method returns an undefined value.

Handles events (room_message, room_enter, etc.).

Parameters:

  • params (Hash)

    Request object (originally represented in JSON) from HipChat Server on installation.



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# 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) ⇒ void

This method returns an undefined value.

Handles installing request.

Parameters:

  • params (Hash)

    Request object (originally represented in JSON) from HipChat Server on installation.



133
134
135
136
137
138
139
# 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) ⇒ void

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.

This method returns an undefined value.

Handles uninstalling request.

Parameters:

  • oauth_id (String)

    Corresponding OAuth ID



146
147
148
149
150
# 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) {|event| ... } ⇒ String

Registers hook on events.

Yields:

Returns:

  • (String)

    Hook ID



70
71
72
# File 'lib/hiptail/manager.rb', line 70

def on_event(*args, &block)
  register_hook :event, args, block
end

#on_install(*args) {|authority| ... } ⇒ String

Registers hook on installation.

Yields:

Returns:

  • (String)

    Hook ID



54
55
56
# File 'lib/hiptail/manager.rb', line 54

def on_install(*args, &block)
  register_hook :install, args, block
end

#on_room_enter(*args) {|event| ... } ⇒ String

Registers hook on room_enter event.

Yields:

Returns:

  • (String)

    Hook ID



118
119
120
# File 'lib/hiptail/manager.rb', line 118

def on_room_enter(*args, &block)
  register_hook :room_enter, args, block
end

#on_room_exit(*args) {|event| ... } ⇒ String

Registers hook on room_exit event.

Yields:

Returns:

  • (String)

    Hook ID



126
127
128
# File 'lib/hiptail/manager.rb', line 126

def on_room_exit(*args, &block)
  register_hook :room_exit, args, block
end

#on_room_message(*args) {|event| ... } ⇒ String

Registers hook on room_message event.

Yields:

Returns:

  • (String)

    Hook ID



86
87
88
# File 'lib/hiptail/manager.rb', line 86

def on_room_message(*args, &block)
  register_hook :room_message, args, block
end

#on_room_messaging(*args) {|event| ... } ⇒ String

Registers hook on messaging events (room_message and room_notification).

Yields:

Returns:

  • (String)

    Hook ID



78
79
80
# File 'lib/hiptail/manager.rb', line 78

def on_room_messaging(*args, &block)
  register_hook :room_messaging, args, block
end

#on_room_notification(*args) {|event| ... } ⇒ String

Registers hook on room_notification event.

Yields:

Returns:

  • (String)

    Hook ID



94
95
96
# 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) {|event| ... } ⇒ String

Registers hook on room_topic_change event.

Yields:

Returns:

  • (String)

    Hook ID



102
103
104
# 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) {|event| ... } ⇒ String

Registers hook on room visiting event (room_enter and room_exit).

Yields:

Returns:

  • (String)

    Hook ID



110
111
112
# File 'lib/hiptail/manager.rb', line 110

def on_room_visiting(*args, &block)
  register_hook :room_visiting, args, block
end

#on_uninstall(*args) {|oauth_id| ... } ⇒ String

Registers hook on uninstallation.

Yields:

  • (oauth_id)
  • (String)

    oauth_id

Returns:

  • (String)

    Hook ID



62
63
64
# File 'lib/hiptail/manager.rb', line 62

def on_uninstall(*args, &block)
  register_hook :uninstall, args, block
end

#register_hook(hook_type, args, block) ⇒ String

Registers a hook.

Parameters:

  • hook_type (Symbol)
  • block (Proc)
  • hook_id (String)

Returns:

  • (String)

    Hook ID



189
190
191
192
193
# 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