Class: Pechkin::RequestHandler
- Inherits:
-
Object
- Object
- Pechkin::RequestHandler
- 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
-
#channel_id ⇒ Object
readonly
Returns the value of attribute channel_id.
-
#env ⇒ Object
readonly
Returns the value of attribute env.
-
#handler ⇒ Object
readonly
Returns the value of attribute handler.
-
#message_id ⇒ Object
readonly
Returns the value of attribute message_id.
-
#req ⇒ Object
readonly
Returns the value of attribute req.
Instance Method Summary collapse
- #handle ⇒ Object
-
#initialize(handler, env) ⇒ RequestHandler
constructor
A new instance of RequestHandler.
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_id ⇒ Object (readonly)
Returns the value of attribute channel_id.
60 61 62 |
# File 'lib/pechkin/app.rb', line 60 def channel_id @channel_id end |
#env ⇒ Object (readonly)
Returns the value of attribute env.
60 61 62 |
# File 'lib/pechkin/app.rb', line 60 def env @env end |
#handler ⇒ Object (readonly)
Returns the value of attribute handler.
60 61 62 |
# File 'lib/pechkin/app.rb', line 60 def handler @handler end |
#message_id ⇒ Object (readonly)
Returns the value of attribute message_id.
60 61 62 |
# File 'lib/pechkin/app.rb', line 60 def @message_id end |
#req ⇒ Object (readonly)
Returns the value of attribute req.
60 61 62 |
# File 'lib/pechkin/app.rb', line 60 def req @req end |
Instance Method Details
#handle ⇒ Object
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 begin data = JSON.parse(req.body.read) rescue JSON::JSONError => e return bad_request(e.) end response(200, handler.handle(channel_id, , data).to_json) end |