Class: Pechkin::RequestHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/pechkin/app/request_handler.rb

Overview

Http requests handler. We need fresh instance per each request. To keep internal state isolated

Constant Summary collapse

REQ_PATH_PATTERN =
%r{^/(.+)/([^/]+)/?$}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(handler, req, logger) ⇒ RequestHandler

Returns a new instance of RequestHandler.



11
12
13
14
15
16
17
18
19
# File 'lib/pechkin/app/request_handler.rb', line 11

def initialize(handler, req, logger)
  @handler = handler
  @req = req
  @logger = logger

  @channel_id, @message_id = req.path_info.match(REQ_PATH_PATTERN) do |m|
    [m[1], m[2]]
  end
end

Instance Attribute Details

#channel_idObject (readonly)

Returns the value of attribute channel_id.



7
8
9
# File 'lib/pechkin/app/request_handler.rb', line 7

def channel_id
  @channel_id
end

#handlerObject (readonly)

Returns the value of attribute handler.



7
8
9
# File 'lib/pechkin/app/request_handler.rb', line 7

def handler
  @handler
end

#loggerObject (readonly)

Returns the value of attribute logger.



7
8
9
# File 'lib/pechkin/app/request_handler.rb', line 7

def logger
  @logger
end

#message_idObject (readonly)

Returns the value of attribute message_id.



7
8
9
# File 'lib/pechkin/app/request_handler.rb', line 7

def message_id
  @message_id
end

#reqObject (readonly)

Returns the value of attribute req.



7
8
9
# File 'lib/pechkin/app/request_handler.rb', line 7

def req
  @req
end

Instance Method Details

#handleObject



21
22
23
24
25
26
27
28
29
30
# File 'lib/pechkin/app/request_handler.rb', line 21

def handle
  raise AppError.http_method_not_allowed unless post?
  raise AppError.message_not_found unless message?

  data = parse_data(req.body.read)
  # handler.handle() requires keyword arguments
  handler.handle(channel_id, message_id, **data).each do |i|
    logger.info "Sent #{channel_id}/#{message_id}: #{i.to_json}"
  end
end