Class: HipTail::Web::Handler
- Inherits:
-
Object
- Object
- HipTail::Web::Handler
- Defined in:
- lib/hiptail/web/handler.rb
Instance Attribute Summary collapse
-
#manager ⇒ HipTail::Manager
readonly
Returns HipTail::Manager.
Instance Method Summary collapse
- #create_response(data, status = 200) ⇒ Object
-
#handle_capability(request, params) ⇒ Array
Handles retrieving capability request.
-
#handle_event(request) ⇒ Array
Handles events (room_message, room_enter, etc.).
-
#handle_install(request) ⇒ Array
Handles installing request.
-
#handle_uninstall(request, oauth_id) ⇒ Array
Handles uninstalling request.
-
#initialize(manager) ⇒ Handler
constructor
A new instance of Handler.
Constructor Details
#initialize(manager) ⇒ Handler
Returns a new instance of Handler.
13 14 15 |
# File 'lib/hiptail/web/handler.rb', line 13 def initialize(manager) @manager = manager end |
Instance Attribute Details
#manager ⇒ HipTail::Manager (readonly)
Returns HipTail::Manager.
10 11 12 |
# File 'lib/hiptail/web/handler.rb', line 10 def manager @manager end |
Instance Method Details
#create_response(data, status = 200) ⇒ Object
90 91 92 93 94 95 96 97 |
# File 'lib/hiptail/web/handler.rb', line 90 def create_response(data, status = 200) body = JSON.generate(data, :ascii_only => true) headers = { 'Content-Type' => 'application/json; charset=UTF-8', 'Content-Length' => body.bytesize.to_s, } return [ status, headers, [ body ] ] end |
#handle_capability(request, params) ⇒ Array
Handles retrieving capability request.
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/hiptail/web/handler.rb', line 64 def handle_capability(request, params) requireds = %w( key name webhook_path installed_path ) missings = requireds.select { |k| ! params.include?(k.to_sym) } if missings.length > 0 raise "missing parameters: " + missings.join(%q{, }) end params = params.dup params[:capability_url] = request.url.to_s base_url = URI.parse(request.url).merge(params[:base_path] || '/') params[:base_url] = base_url.to_s params[:webhook_url] = base_url.merge('./' + params[:webhook_path]) params[:installed_url] = base_url.merge('./' + params[:installed_path]) capability = HipTail::Util::create_capability(params) if block_given? yield capability end create_response(capability) end |
#handle_event(request) ⇒ Array
Handles events (room_message, room_enter, etc.).
40 41 42 43 44 |
# File 'lib/hiptail/web/handler.rb', line 40 def handle_event(request) request.body.rewind @manager.handle_event(JSON.parse(request.body.read)) create_response({}) end |
#handle_install(request) ⇒ Array
Handles installing request.
20 21 22 23 24 |
# File 'lib/hiptail/web/handler.rb', line 20 def handle_install(request) request.body.rewind @manager.handle_install(JSON.parse(request.body.read)) create_response({}) end |
#handle_uninstall(request, oauth_id) ⇒ Array
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.
Handles uninstalling request.
32 33 34 35 |
# File 'lib/hiptail/web/handler.rb', line 32 def handle_uninstall(request, oauth_id) @manager.handle_uninstall(oauth_id) create_response({}) end |