Class: Midori::API

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

Overview

This class provides methods to be inherited as route definition.

Constant Summary collapse

METHODS =

Constants of supported methods in route definition

%w( delete
  get
  head
  post
  put
  connect
  options
  trace
  copy
  lock
  mkcol
  move
  propfind
  proppatch
  unlock
  report
  mkactivity
  checkout
  merge
  notify
  subscribe
  unsubscribe
  patch
  purge
  websocket
  eventsource
).freeze

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.routesHash

Returns merged routes defined in the instance.

Returns:

  • (Hash)

    merged routes defined in the instance



9
10
11
# File 'lib/midori/api.rb', line 9

def routes
  @routes
end

.scope_middlewaresArray

Returns global middlewares under the scope.

Returns:

  • (Array)

    global middlewares under the scope



9
# File 'lib/midori/api.rb', line 9

attr_accessor :routes, :scope_middlewares

Class Method Details

.capture(error) {|e| ... } ⇒ Object

Definitions for global error handler

Examples:

Basic Usage

capture Midori::InternalError do |e|
  Midori::Response(500, {}, e.backtrace)
end

Parameters:

  • error (Class)

    Error class, must be inherited form StandardError

Yields:

  • what to do to deal with error

Yield Parameters:

  • e (StandardError)

    the detailed error



332
333
334
335
# File 'lib/midori/api.rb', line 332

def capture(error, &block)
  Midori::Sandbox.add_rule(error, block)
  nil
end

.checkout(path) { ... } ⇒ nil

Add CHECKOUT method as a DSL for route definition

Examples:

String as router

checkout '/' do
   puts 'Hello World'
end

Parameters:

  • path (String)

    Accepts as part of path in route definition

Yields:

  • what to run when route matched

Returns:

  • (nil)

    nil



200
# File 'lib/midori/api.rb', line 200

def checkout(path, &block) end

.class_initializenil

Init private variables of class

Returns:

  • (nil)

    nil



13
14
15
16
17
18
19
20
# 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

.connect(path) { ... } ⇒ nil

Add CONNECT method as a DSL for route definition

Examples:

String as router

connect '/' do
   puts 'Hello World'
end

Parameters:

  • path (String)

    Accepts as part of path in route definition

Yields:

  • what to run when route matched

Returns:

  • (nil)

    nil



80
# File 'lib/midori/api.rb', line 80

def connect(path, &block) end

.copy(path) { ... } ⇒ nil

Add COPY method as a DSL for route definition

Examples:

String as router

copy '/' do
   puts 'Hello World'
end

Parameters:

  • path (String)

    Accepts as part of path in route definition

Yields:

  • what to run when route matched

Returns:

  • (nil)

    nil



109
# File 'lib/midori/api.rb', line 109

def copy(path, &block) end

.delete(path) { ... } ⇒ nil

Add DELETE method as a DSL for route definition

Examples:

String as router

delete '/' do
   puts 'Hello World'
end

Parameters:

  • path (String)

    Accepts as part of path in route definition

Yields:

  • what to run when route matched

Returns:

  • (nil)

    nil



30
# File 'lib/midori/api.rb', line 30

def delete(path, &block) end

.eventsource(path) { ... } ⇒ nil

Add EVENTSOURCE method as a DSL for route definition

Examples:

String as router

eventsource '/' do
   puts 'Hello World'
end

Parameters:

  • path (String)

    Accepts as part of path in route definition

Yields:

  • what to run when route matched

Returns:

  • (nil)

    nil



312
# File 'lib/midori/api.rb', line 312

def eventsource(path, &block) end

.filter(middleware, *args) ⇒ nil

Use a middleware in the next route

Parameters:

  • middleware (Class)

    Inherited from Midori::Middleware

Returns:

  • (nil)

    nil



368
369
370
371
372
# File 'lib/midori/api.rb', line 368

def filter(middleware, *args)
  middleware = middleware.new(*args)
  @temp_middlewares << middleware
  nil
end

.get(path) { ... } ⇒ nil

Add GET method as a DSL for route definition

Examples:

String as router

get '/' do
   puts 'Hello World'
end

Parameters:

  • path (String)

    Accepts as part of path in route definition

Yields:

  • what to run when route matched

Returns:

  • (nil)

    nil



40
# File 'lib/midori/api.rb', line 40

def get(path, &block) end

.head(path) { ... } ⇒ nil

Add HEAD method as a DSL for route definition

Examples:

String as router

head '/' do
   puts 'Hello World'
end

Parameters:

  • path (String)

    Accepts as part of path in route definition

Yields:

  • what to run when route matched

Returns:

  • (nil)

    nil



50
# File 'lib/midori/api.rb', line 50

def head(path, &block) end

.helper(name) { ... } ⇒ Object

Helper block for defining methods in APIs

Parameters:

  • name (Symbol)

    name of the method

Yields:

  • define what to run in CleanRoom



377
378
379
380
381
# 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

Examples:

String as router

link '/' do
   puts 'Hello World'
end

Parameters:

  • path (String)

    Accepts as part of path in route definition

Yields:

  • what to run when route matched

Returns:

  • (nil)

    nil



282
# File 'lib/midori/api.rb', line 282

def link(path, &block) end

.lock(path) { ... } ⇒ nil

Add LOCK method as a DSL for route definition

Examples:

String as router

lock '/' do
   puts 'Hello World'
end

Parameters:

  • path (String)

    Accepts as part of path in route definition

Yields:

  • what to run when route matched

Returns:

  • (nil)

    nil



119
# File 'lib/midori/api.rb', line 119

def lock(path, &block) end

.merge(path) { ... } ⇒ nil

Add MERGE method as a DSL for route definition

Examples:

String as router

merge '/' do
   puts 'Hello World'
end

Parameters:

  • path (String)

    Accepts as part of path in route definition

Yields:

  • what to run when route matched

Returns:

  • (nil)

    nil



210
# File 'lib/midori/api.rb', line 210

def merge(path, &block) end

.mkactivity(path) { ... } ⇒ nil

Add MKACTIVITY method as a DSL for route definition

Examples:

String as router

mkactivity '/' do
   puts 'Hello World'
end

Parameters:

  • path (String)

    Accepts as part of path in route definition

Yields:

  • what to run when route matched

Returns:

  • (nil)

    nil



190
# File 'lib/midori/api.rb', line 190

def mkactivity(path, &block) end

.mkcol(path) { ... } ⇒ nil

Add MKCOK method as a DSL for route definition

Examples:

String as router

mkcol '/' do
   puts 'Hello World'
end

Parameters:

  • path (String)

    Accepts as part of path in route definition

Yields:

  • what to run when route matched

Returns:

  • (nil)

    nil



129
# File 'lib/midori/api.rb', line 129

def mkcol(path, &block) end

.mount(prefix, api) ⇒ nil

Mount a route prefix with another API defined

Parameters:

  • prefix (String)

    prefix of the route String

  • api (Class)

    inherited from Midori::API

Returns:

  • (nil)

    nil

Raises:

  • (ArgumentError)


318
319
320
321
322
# 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

.move(path) { ... } ⇒ nil

Add MOVE method as a DSL for route definition

Examples:

String as router

move '/' do
   puts 'Hello World'
end

Parameters:

  • path (String)

    Accepts as part of path in route definition

Yields:

  • what to run when route matched

Returns:

  • (nil)

    nil



139
# File 'lib/midori/api.rb', line 139

def move(path, &block) end

.msearch(path) { ... } ⇒ nil

Add M-SEARCH method as a DSL for route definition

Examples:

String as router

msearch '/' do
   puts 'Hello World'
end

Parameters:

  • path (String)

    Accepts as part of path in route definition

Yields:

  • what to run when route matched

Returns:

  • (nil)

    nil



220
221
222
# File 'lib/midori/api.rb', line 220

def msearch(path, &block)
  add_route(:'M-SEARCH', path, block)
end

.notify(path) { ... } ⇒ nil

Add NOTIFY method as a DSL for route definition

Examples:

String as router

notify '/' do
   puts 'Hello World'
end

Parameters:

  • path (String)

    Accepts as part of path in route definition

Yields:

  • what to run when route matched

Returns:

  • (nil)

    nil



232
# File 'lib/midori/api.rb', line 232

def notify(path, &block) end

.options(path, &block) ⇒ nil

Add OPTIONS method as a DSL for route definition

Examples:

String as router

options '/' do
   puts 'Hello World'
end

Parameters:

  • path (String)

    Accepts as part of path in route definition

Returns:

  • (nil)

    nil



89
# File 'lib/midori/api.rb', line 89

def options(path, &block) end

.patch(path) { ... } ⇒ nil

Add PATCH method as a DSL for route definition

Examples:

String as router

patch '/' do
   puts 'Hello World'
end

Parameters:

  • path (String)

    Accepts as part of path in route definition

Yields:

  • what to run when route matched

Returns:

  • (nil)

    nil



262
# File 'lib/midori/api.rb', line 262

def patch(path, &block) end

.post(path) { ... } ⇒ nil

Add POST method as a DSL for route definition

Examples:

String as router

post '/' do
   puts 'Hello World'
end

Parameters:

  • path (String)

    Accepts as part of path in route definition

Yields:

  • what to run when route matched

Returns:

  • (nil)

    nil



60
# File 'lib/midori/api.rb', line 60

def post(path, &block) end

.propfind(path) { ... } ⇒ nil

Add PROPFIND method as a DSL for route definition

Examples:

String as router

propfind '/' do
   puts 'Hello World'
end

Parameters:

  • path (String)

    Accepts as part of path in route definition

Yields:

  • what to run when route matched

Returns:

  • (nil)

    nil



150
# File 'lib/midori/api.rb', line 150

def propfind(path, &block) end

.proppatch(path) { ... } ⇒ nil

Add PROPPATCH method as a DSL for route definition

Examples:

String as router

proppatch '/' do
   puts 'Hello World'
end

Parameters:

  • path (String)

    Accepts as part of path in route definition

Yields:

  • what to run when route matched

Returns:

  • (nil)

    nil



160
# File 'lib/midori/api.rb', line 160

def proppatch(path, &block) end

.purge(path) { ... } ⇒ nil

Add PURGE method as a DSL for route definition

Examples:

String as router

purge '/' do
   puts 'Hello World'
end

Parameters:

  • path (String)

    Accepts as part of path in route definition

Yields:

  • what to run when route matched

Returns:

  • (nil)

    nil



272
# File 'lib/midori/api.rb', line 272

def purge(path, &block) end

.put(path) { ... } ⇒ nil

Add PUT method as a DSL for route definition

Examples:

String as router

put '/' do
   puts 'Hello World'
end

Parameters:

  • path (String)

    Accepts as part of path in route definition

Yields:

  • what to run when route matched

Returns:

  • (nil)

    nil



70
# File 'lib/midori/api.rb', line 70

def put(path, &block) end

.report(path) { ... } ⇒ nil

Add REPORT method as a DSL for route definition

Examples:

String as router

report '/' do
   puts 'Hello World'
end

Parameters:

  • path (String)

    Accepts as part of path in route definition

Yields:

  • what to run when route matched

Returns:

  • (nil)

    nil



180
# File 'lib/midori/api.rb', line 180

def report(path, &block) end

.subscribe(path) { ... } ⇒ nil

Add SUBSCRIBE method as a DSL for route definition

Examples:

String as router

subscribe '/' do
   puts 'Hello World'
end

Parameters:

  • path (String)

    Accepts as part of path in route definition

Yields:

  • what to run when route matched

Returns:

  • (nil)

    nil



242
# File 'lib/midori/api.rb', line 242

def subscribe(path, &block) end

.trace(path) { ... } ⇒ nil

Add TRACE method as a DSL for route definition

Examples:

String as router

trace '/' do
   puts 'Hello World'
end

Parameters:

  • path (String)

    Accepts as part of path in route definition

Yields:

  • what to run when route matched

Returns:

  • (nil)

    nil



99
# File 'lib/midori/api.rb', line 99

def trace(path, &block) end

Add UNLINK method as a DSL for route definition

Examples:

String as router

unlink '/' do
   puts 'Hello World'
end

Parameters:

  • path (String)

    Accepts as part of path in route definition

Yields:

  • what to run when route matched

Returns:

  • (nil)

    nil



292
# File 'lib/midori/api.rb', line 292

def unlink(path, &block) end

.unlock(path) { ... } ⇒ nil

Add UNLOCK method as a DSL for route definition

Examples:

String as router

unlock '/' do
   puts 'Hello World'
end

Parameters:

  • path (String)

    Accepts as part of path in route definition

Yields:

  • what to run when route matched

Returns:

  • (nil)

    nil



170
# File 'lib/midori/api.rb', line 170

def unlock(path, &block) end

.unsubscribe(path) { ... } ⇒ nil

Add UNSUBSCRIBE method as a DSL for route definition

Examples:

String as router

unsubscribe '/' do
   puts 'Hello World'
end

Parameters:

  • path (String)

    Accepts as part of path in route definition

Yields:

  • what to run when route matched

Returns:

  • (nil)

    nil



252
# File 'lib/midori/api.rb', line 252

def unsubscribe(path, &block) end

.use(middleware, *args) ⇒ nil

Use a middleware in the all routes

Parameters:

  • middleware (Class)

    Inherited from Midori::Middleware

Returns:

  • (nil)

    nil



359
360
361
362
363
# File 'lib/midori/api.rb', line 359

def use(middleware, *args)
  middleware = middleware.new(*args)
  @scope_middlewares << middleware
  nil
end

.websocket(path) { ... } ⇒ nil

Add WEBSOCKET method as a DSL for route definition

Examples:

String as router

websocket '/' do
   puts 'Hello World'
end

Parameters:

  • path (String)

    Accepts as part of path in route definition

Yields:

  • what to run when route matched

Returns:

  • (nil)

    nil



302
# File 'lib/midori/api.rb', line 302

def websocket(path, &block) end