Class: Midori::APIEngine
- Inherits:
-
Object
- Object
- Midori::APIEngine
- Defined in:
- lib/midori/api_engine.rb
Overview
Merge and manage all APIs.
Instance Attribute Summary collapse
-
#routes ⇒ Hash
A hash of all routes merged.
Class Method Summary collapse
-
.websocket_header(key) ⇒ Hash
Return websocket header with given key.
Instance Method Summary collapse
-
#initialize(root_api, type = :sinatra) ⇒ APIEngine
constructor
Init an API Engine.
-
#receive(request, connection = nil) ⇒ Midori::Response
Process after receive data from client.
Constructor Details
#initialize(root_api, type = :sinatra) ⇒ APIEngine
Init an API Engine
10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/midori/api_engine.rb', line 10 def initialize(root_api, type = :sinatra) @routes = {} Midori::Const::ROUTE_METHODS.map {|method| @routes[method] = []} @root_api = root_api @type = type @routes = merge('', root_api, []) @routes.delete :MOUNT @routes.each do |method| method[1].each do |route| route.path = Mustermann.new(route.path, type: type) end end end |
Instance Attribute Details
#routes ⇒ Hash
A hash of all routes merged
4 5 6 |
# File 'lib/midori/api_engine.rb', line 4 def routes @routes end |
Class Method Details
.websocket_header(key) ⇒ Hash
Return websocket header with given key
83 84 85 86 87 |
# File 'lib/midori/api_engine.rb', line 83 def self.websocket_header(key) header = Midori::Const::WEBSOCKET_HEADER.clone header['Sec-WebSocket-Accept'] = Digest::SHA1.base64digest(key + '258EAFA5-E914-47DA-95CA-C5AB0DC85B11') header end |
Instance Method Details
#receive(request, connection = nil) ⇒ Midori::Response
Process after receive data from client
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/midori/api_engine.rb', line 46 def receive(request, connection = nil) @routes[request.method].each do |route| params = route.path.params(request.path) next unless params # Skip if not matched request.params = params clean_room = Midori::CleanRoom.new(request) if request.websocket? # Send 101 Switching Protocol connection.send_data Midori::Response.new( status: 101, header: Midori::APIEngine.websocket_header(request.header['Sec-WebSocket-Key']), body: '') connection.websocket.request = request Midori::Sandbox.run(clean_room, route.function, connection.websocket) return Midori::Response.new elsif request.eventsource? connection.send_data Midori::Response.new( status: 200, header: Midori::Const::EVENTSOURCE_HEADER) Midori::Sandbox.run(clean_room, route.function, connection.eventsource) return Midori::Response.new else request = middleware_exec(route.middlewares, clean_room, request) return request if request.is_a? Midori::Response # Early stop result = Midori::Sandbox.run(clean_room, route.function) clean_room.body = result response = result.is_a?(Midori::Response) ? result : clean_room.raw_response response = middleware_exec(route.middlewares, clean_room, request, response) return response end end raise Midori::Exception::NotFound end |