Class: Pechkin::RequestHandler

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

Overview

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

Constant Summary collapse

REQ_PATH_PATTERN =
%r{^/(.+)/([^/]+)/?$}
DEFAULT_CONTENT_TYPE =
{ 'Content-Type' => 'application/json' }.freeze
DEFAULT_HEADERS =
{}.merge(DEFAULT_CONTENT_TYPE).freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(handler, env) ⇒ RequestHandler

Returns a new instance of RequestHandler.



63
64
65
66
67
68
69
70
71
# File 'lib/pechkin/app.rb', line 63

def initialize(handler, env)
  @handler = handler
  @env = env
  @req = Rack::Request.new(env)

  @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.



60
61
62
# File 'lib/pechkin/app.rb', line 60

def channel_id
  @channel_id
end

#envObject (readonly)

Returns the value of attribute env.



60
61
62
# File 'lib/pechkin/app.rb', line 60

def env
  @env
end

#handlerObject (readonly)

Returns the value of attribute handler.



60
61
62
# File 'lib/pechkin/app.rb', line 60

def handler
  @handler
end

#message_idObject (readonly)

Returns the value of attribute message_id.



60
61
62
# File 'lib/pechkin/app.rb', line 60

def message_id
  @message_id
end

#reqObject (readonly)

Returns the value of attribute req.



60
61
62
# File 'lib/pechkin/app.rb', line 60

def req
  @req
end

Instance Method Details

#handleObject



73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/pechkin/app.rb', line 73

def handle
  return not_allowed unless post?
  return not_found unless message?

  begin
    data = JSON.parse(req.body.read)
  rescue JSON::JSONError => e
    return bad_request(e.message)
  end

  response(200, handler.handle(channel_id, message_id, data).to_json)
end