class Pechkin::RequestHandler

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

Constants

REQ_PATH_PATTERN

Attributes

channel_id[R]
handler[R]
logger[R]
message_id[R]
req[R]

Public Class Methods

new(handler, req, logger) click to toggle source
# 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

Public Instance Methods

handle() click to toggle source
# 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(channel_id, message_id, data).each do |i|
    logger.info "Sent #{channel_id}/#{message_id}: #{i.to_json}"
  end
end

Private Instance Methods

message?() click to toggle source
# File lib/pechkin/app/request_handler.rb, line 39
def message?
  return false unless @channel_id && @message_id

  handler.message?(@channel_id, @message_id)
end
parse_data(data) click to toggle source
# File lib/pechkin/app/request_handler.rb, line 33
def parse_data(data)
  JSON.parse(data)
rescue JSON::JSONError => e
  raise AppError.bad_request(e.message)
end
post?() click to toggle source
# File lib/pechkin/app/request_handler.rb, line 45
def post?
  req.post?
end