class Midori::API
This class provides methods to be inherited as route definition.
Constants
- METHODS
Constants of supported methods in route definition
Attributes
@!attribute routes
@return [Hash] merged routes defined in the instance
@!attribute scope_middlewares
@return [Array] global middlewares under the scope
@!attribute routes
@return [Hash] merged routes defined in the instance
@!attribute scope_middlewares
@return [Array] global middlewares under the scope
Public Class Methods
Definitions for global error handler @param [Class] error Error class, must be inherited form StandardError @yield what to do to deal with error @yieldparam [StandardError] e the detailed error @example Basic Usage
capture Midori::InternalError do |e| Midori::Response(500, {}, e.backtrace) end
# File lib/midori/api.rb, line 332 def capture(error, &block) Midori::Sandbox.add_rule(error, block) nil end
Add CHECKOUT method as a DSL for route definition @param [ String
] path Accepts as part of path in route definition @yield what to run when route matched @return [ nil ] nil @example String
as router
checkout '/' do puts 'Hello World' end
# File lib/midori/api.rb, line 200 def checkout(path, &block) end
Init private variables of class @return [nil] nil
# File lib/midori/api.rb, line 13 def class_initialize @routes = {} Midori::Const::ROUTE_METHODS.map {|method| @routes[method] = []} @routes[:MOUNT] = [] @scope_middlewares = [] @temp_middlewares = [] nil end
Add CONNECT method as a DSL for route definition @param [String] path Accepts as part of path in route definition @yield what to run when route matched @return [nil] nil @example String
as router
connect '/' do puts 'Hello World' end
# File lib/midori/api.rb, line 80 def connect(path, &block) end
Add EVENTSOURCE method as a DSL for route definition @param [String] path Accepts as part of path in route definition @yield what to run when route matched @return [nil] nil @example String
as router
eventsource '/' do puts 'Hello World' end
# File lib/midori/api.rb, line 312 def eventsource(path, &block) end
Use a middleware in the next route @param [Class] middleware Inherited from Midori::Middleware
@return [nil] nil
# File lib/midori/api.rb, line 368 def filter(middleware, *args) middleware = middleware.new(*args) @temp_middlewares << middleware nil end
Add GET method as a DSL for route definition @param [String] path Accepts as part of path in route definition @yield what to run when route matched @return [nil] nil @example String
as router
get '/' do puts 'Hello World' end
# File lib/midori/api.rb, line 40 def get(path, &block) end
Helper block for defining methods in APIs @param [Symbol] name name of the method @yield define what to run in CleanRoom
# File lib/midori/api.rb, line 377 def helper(name, &block) Midori::CleanRoom.class_exec do define_method(name, &block) end end
Add LINK method as a DSL for route definition @param [String] path Accepts as part of path in route definition @yield what to run when route matched @return [nil] nil @example String
as router
link '/' do puts 'Hello World' end
# File lib/midori/api.rb, line 282 def link(path, &block) end
Add MKACTIVITY method as a DSL for route definition @param [ String
] path Accepts as part of path in route definition @yield what to run when route matched @return [ nil ] nil @example String
as router
mkactivity '/' do puts 'Hello World' end
# File lib/midori/api.rb, line 190 def mkactivity(path, &block) end
Mount a route prefix with another API
defined @param [String] prefix prefix of the route String
@param [Class] api inherited from Midori::API
@return [nil] nil
# File lib/midori/api.rb, line 318 def mount(prefix, api) raise ArgumentError if prefix == '/' # Cannot mount route API @routes[:MOUNT] << [prefix, api] nil end
Add M-SEARCH method as a DSL for route definition @param [ String
] path Accepts as part of path in route definition @yield what to run when route matched @return [ nil ] nil @example String
as router
msearch '/' do puts 'Hello World' end
# File lib/midori/api.rb, line 220 def msearch(path, &block) add_route(:'M-SEARCH', path, block) end
Add OPTIONS method as a DSL for route definition @param [String] path Accepts as part of path in route definition @return [nil] nil @example String
as router
options '/' do puts 'Hello World' end
# File lib/midori/api.rb, line 89 def options(path, &block) end
Add POST method as a DSL for route definition @param [String] path Accepts as part of path in route definition @yield what to run when route matched @return [nil] nil @example String
as router
post '/' do puts 'Hello World' end
# File lib/midori/api.rb, line 60 def post(path, &block) end
Add PROPFIND method as a DSL for route definition @param [ String
] path Accepts as part of path in route definition @yield what to run when route matched @return [ nil ] nil @example String
as router
propfind '/' do puts 'Hello World' end
# File lib/midori/api.rb, line 150 def propfind(path, &block) end
Add PROPPATCH method as a DSL for route definition @param [ String
] path Accepts as part of path in route definition @yield what to run when route matched @return [ nil ] nil @example String
as router
proppatch '/' do puts 'Hello World' end
# File lib/midori/api.rb, line 160 def proppatch(path, &block) end
Add PUT method as a DSL for route definition @param [String] path Accepts as part of path in route definition @yield what to run when route matched @return [nil] nil @example String
as router
put '/' do puts 'Hello World' end
# File lib/midori/api.rb, line 70 def put(path, &block) end
Add SUBSCRIBE method as a DSL for route definition @param [ String
] path Accepts as part of path in route definition @yield what to run when route matched @return [ nil ] nil @example String
as router
subscribe '/' do puts 'Hello World' end
# File lib/midori/api.rb, line 242 def subscribe(path, &block) end
Add UNLINK method as a DSL for route definition @param [String] path Accepts as part of path in route definition @yield what to run when route matched @return [nil] nil @example String
as router
unlink '/' do puts 'Hello World' end
# File lib/midori/api.rb, line 292 def unlink(path, &block) end
Add UNSUBSCRIBE method as a DSL for route definition @param [ String
] path Accepts as part of path in route definition @yield what to run when route matched @return [ nil ] nil @example String
as router
unsubscribe '/' do puts 'Hello World' end
# File lib/midori/api.rb, line 252 def unsubscribe(path, &block) end
Use a middleware in the all routes @param [Class] middleware Inherited from Midori::Middleware
@return [nil] nil
# File lib/midori/api.rb, line 359 def use(middleware, *args) middleware = middleware.new(*args) @scope_middlewares << middleware nil end
Add WEBSOCKET method as a DSL for route definition @param [String] path Accepts as part of path in route definition @yield what to run when route matched @return [nil] nil @example String
as router
websocket '/' do puts 'Hello World' end
# File lib/midori/api.rb, line 302 def websocket(path, &block) end
Private Class Methods
Implementation of route DSL @param [String] method HTTP method @param [String, Regexp] path path definition @param [Proc] block process to run when route matched @return [nil] nil
# File lib/midori/api.rb, line 342 def add_route(method, path, block) # Argument check raise ArgumentError unless path.is_a?String # Insert route to routes route = Midori::Route.new(method, path, block) route.middlewares = @scope_middlewares + @temp_middlewares @routes[method] << route # Clean up temp middleware @temp_middlewares = [] nil end
# File lib/midori/api.rb, line 383 def inherited(subclass) subclass.class_initialize end