Class: Canson::Base
- Inherits:
-
Object
- Object
- Canson::Base
- Extended by:
- Forwardable
- Includes:
- Mustermann
- Defined in:
- lib/canson/base.rb
Overview
used to create a new app App < CansonBase
get '/foo' do
{result: 'ok'}
end
end
Instance Attribute Summary collapse
-
#klass ⇒ Object
readonly
Returns the value of attribute klass.
-
#responder ⇒ Object
Returns the value of attribute responder.
Class Method Summary collapse
Instance Method Summary collapse
- #call(env) ⇒ Object
- #filename ⇒ Object
- #handle_socket(env) ⇒ Object
-
#initialize(responder = Responder.new) ⇒ Base
constructor
A new instance of Base.
Constructor Details
Instance Attribute Details
#klass ⇒ Object (readonly)
Returns the value of attribute klass.
18 19 20 |
# File 'lib/canson/base.rb', line 18 def klass @klass end |
#responder ⇒ Object
Returns the value of attribute responder.
19 20 21 |
# File 'lib/canson/base.rb', line 19 def responder @responder end |
Class Method Details
Instance Method Details
#call(env) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/canson/base.rb', line 63 def call(env) m = env['REQUEST_METHOD'].tr('/', '').downcase.to_sym target = routes[m][env['PATH_INFO']] if m == :get && env['PATH_INFO'] == '/' && File.file?(filename) out = File.open(filename) return [200, { 'X-Sendfile' => filename, 'Content-Length' => out.size }, out] end return handle_socket(env) if env['HTTP_UPGRADE'] == 'websocket' return [404, {}, ['no route']] unless target return call_result(env, target) rescue => e puts e. [500, {}, ["server error #{e.}".to_json]] end |
#filename ⇒ Object
59 60 61 |
# File 'lib/canson/base.rb', line 59 def filename @filename end |
#handle_socket(env) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/canson/base.rb', line 81 def handle_socket(env) nickname = env['PATH_INFO'][1..-1].force_encoding 'UTF-8' if env['HTTP_UPGRADE'.freeze] =~ /websocket/i routes[:on_message][nickname] ||= Canson::Websocket.new(env) routes[:on_message][nickname].on_open = routes[:on_open]['websocket_method'] routes[:on_message][nickname].on_close = routes[:on_close]['websocket_method'] routes[:on_message][nickname].on_shutdown = routes[:on_shutdown]['websocket_method'] routes[:on_message][nickname]. = routes[:on_message]['websocket_method'] env['upgrade.websocket'.freeze] = routes[:on_message][nickname] return [0, {}, []] end end |