Class: Rack::SimpleEndpoint

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/contrib/simple_endpoint.rb

Overview

Create simple endpoints with routing rules, similar to Sinatra actions.

Simplest example:

use Rack::SimpleEndpoint, '/ping_monitor' do
  'pong'
end

The value returned from the block will be written to the response body, so the above example will return “pong” when the request path is /ping_monitor.

HTTP verb requirements can optionally be specified:

use Rack::SimpleEndpoint, '/foo' => :get do
  'only GET requests will match'
end

use Rack::SimpleEndpoint, '/bar' => [:get, :post] do
  'only GET and POST requests will match'
end

Rack::Request and Rack::Response objects are yielded to block:

use Rack::SimpleEndpoint, '/json' do |req, res|
  res['Content-Type'] = 'application/json'
  %({"foo": "#{req[:foo]}"})
end

When path is a Regexp, match data object is yielded as third argument to block

use Rack::SimpleEndpoint, %r{^/(john|paul|george|ringo)} do |req, res, match|
  "Hello, #{match[1]}"
end

A :pass symbol returned from block will not return a response; control will continue down the Rack stack:

use Rack::SimpleEndpoint, '/api_key' do |req, res|
  req.env['myapp.user'].authorized? ? '12345' : :pass
end

# Unauthorized access to /api_key will be handled by PublicApp
run PublicApp

Instance Method Summary collapse

Constructor Details

#initialize(app, arg, &block) ⇒ SimpleEndpoint

Returns a new instance of SimpleEndpoint.



48
49
50
51
52
53
# File 'lib/rack/contrib/simple_endpoint.rb', line 48

def initialize(app, arg, &block)
  @app    = app
  @path   = extract_path(arg)
  @verbs  = extract_verbs(arg)
  @block  = block
end

Instance Method Details

#call(env) ⇒ Object



55
56
57
58
59
60
61
62
63
64
# File 'lib/rack/contrib/simple_endpoint.rb', line 55

def call(env)
  match = match_path(env['PATH_INFO'])
  if match && valid_method?(env['REQUEST_METHOD'])
    req, res = Request.new(env), Response.new
    body = @block.call(req, res, (match unless match == true))
    body == :pass ? @app.call(env) : (res.write(body); res.finish)
  else
    @app.call(env)
  end
end