Class: Midori::APIEngine

Inherits:
Object
  • Object
show all
Defined in:
lib/midori/api_engine.rb

Overview

Merge and manage all APIs.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root_api, type = :sinatra) ⇒ APIEngine

Init an API Engine

Parameters:

  • root_api (Class)

    API inherited from [Midori::API]

  • type (Symbol) (defaults to: :sinatra)

    type mustermann support



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

#routesHash

A hash of all routes merged

Returns:

  • (Hash)

    the current value of routes



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

Parameters:

  • key (String)

    ‘Sec-WebSocket-Key’ in request header

Returns:

  • (Hash)

    header



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

Parameters:

  • request (Midori::Request)

    Http Raw Request

  • connection (EM::Connection) (defaults to: nil)

    A connection created by EventMachine

Returns:

Raises:

  • (Midori::Error::NotFound)

    If no route matched



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