Module: Roda::RodaPlugins::Base::RequestMethods

Defined in:
lib/roda.rb

Overview

Instance methods for RodaRequest, mostly related to handling routing for the request.

Constant Summary collapse

PATH_INFO =
"PATH_INFO".freeze
SCRIPT_NAME =
"SCRIPT_NAME".freeze
REQUEST_METHOD =
"REQUEST_METHOD".freeze
EMPTY_STRING =
"".freeze
SLASH =
"/".freeze
SEGMENT =
"([^\\/]+)".freeze
TERM_INSPECT =
"TERM".freeze
GET_REQUEST_METHOD =
'GET'.freeze
TERM =
Object.new

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#capturesObject (readonly)

The current captures for the request. This gets modified as routing occurs.



415
416
417
# File 'lib/roda.rb', line 415

def captures
  @captures
end

#scopeObject (readonly)

The Roda instance related to this request object. Useful if routing methods need access to the scope of the Roda route block.



419
420
421
# File 'lib/roda.rb', line 419

def scope
  @scope
end

Instance Method Details

#block_result(result) ⇒ Object

Handle match block return values. By default, if a string is given and the response is empty, use the string as the response body.



462
463
464
465
466
467
# File 'lib/roda.rb', line 462

def block_result(result)
  res = response
  if res.empty? && (body = block_result_body(result))
    res.write(body)
  end
end

#full_path_infoObject

As request routing modifies SCRIPT_NAME and PATH_INFO, this exists as a helper method to get the full path of the request.

r.env['SCRIPT_NAME'] = '/foo'
r.env['PATH_INFO'] = '/bar'
r.full_path_info
# => '/foo/bar'


435
436
437
# File 'lib/roda.rb', line 435

def full_path_info
  "#{@env[SCRIPT_NAME]}#{@env[PATH_INFO]}"
end

#halt(res = response.finish) ⇒ Object

Immediately stop execution of the route block and return the given rack response array of status, headers, and body. If no argument is given, uses the current response.

r.halt [200, {'Content-Type'=>'text/html'}, ['Hello World!']]

response.status = 200
response['Content-Type'] = 'text/html'
response.write 'Hello World!'
r.halt


449
450
451
# File 'lib/roda.rb', line 449

def halt(res=response.finish)
  throw :halt, res
end

#initialize(scope, env) ⇒ Object

Store the roda instance and environment.



422
423
424
425
426
# File 'lib/roda.rb', line 422

def initialize(scope, env)
  @scope = scope
  @captures = []
  super(env)
end

#inspectObject

Show information about current request, including request class, request method and full path.

r.inspect
# => '#<Roda::RodaRequest GET /foo/bar>'


474
475
476
# File 'lib/roda.rb', line 474

def inspect
  "#<#{self.class.inspect} #{@env[REQUEST_METHOD]} #{full_path_info}>"
end

#is(*args, &block) ⇒ Object

Does a terminal match on the current path, matching only if the arguments have fully matched the path. If it matches, the match block is executed, and when the match block returns, the rack response is returned.

r.path_info
# => "/foo/bar"

r.is 'foo' do
  # does not match, as path isn't fully matched (/bar remaining)
end

r.is 'foo/bar' do
  # matches as path is empty after matching
end

If no arguments are given, matches if the path is already fully matched.

r.on 'foo/bar' do
  r.is do
    # matches as path is already empty
  end
end

Note that this matches only if the path after matching the arguments is empty, not if it still contains a trailing slash:

r.path_info
# =>  "/foo/bar/"

r.is 'foo/bar' do
  # does not match, as path isn't fully matched (/ remaining)
end

r.is 'foo/bar/' do
  # matches as path is empty after matching
end

r.on 'foo/bar' do
  r.is "" do
    # matches as path is empty after matching
  end
end


521
522
523
524
525
526
527
528
529
530
# File 'lib/roda.rb', line 521

def is(*args, &block)
  if args.empty?
    if @env[PATH_INFO] == EMPTY_STRING
      always(&block)
    end
  else
    args << TERM
    if_match(args, &block)
  end
end

#is_get?Boolean

Optimized method for whether this request is a GET request. Similar to the default Rack::Request get? method, but can be overridden without changing rack’s behavior.

Returns:

  • (Boolean)


456
457
458
# File 'lib/roda.rb', line 456

def is_get?
  @env[REQUEST_METHOD] == GET_REQUEST_METHOD
end

#on(*args, &block) ⇒ Object

Does a match on the path, matching only if the arguments have matched the path. Because this doesn’t fully match the path, this is usually used to setup branches of the routing tree, not for final handling of the request.

r.path_info
# => "/foo/bar"

r.on 'foo' do
  # matches, path is /bar after matching
end

r.on 'bar' do
  # does not match
end

Like other routing methods, If it matches, the match block is executed, and when the match block returns, the rack response is returned. However, in general you will call another routing method inside the match block that fully matches the path and does the final handling for the request:

r.on 'foo' do
  r.is 'bar' do
    # handle /foo/bar request
  end
end


559
560
561
562
563
564
565
# File 'lib/roda.rb', line 559

def on(*args, &block)
  if args.empty?
    always(&block)
  else
    if_match(args, &block)
  end
end

#redirect(path = default_redirect_path, status = 302) ⇒ Object

Immediately redirect to the path using the status code. This ends the processing of the request:

r.redirect '/page1', 301 if r['param'] == 'value1'
r.redirect '/page2' # uses 302 status code
response.status = 404 # not reached

If you do not provide a path, by default it will redirect to the same path if the request is not a GET request. This is designed to make it easy to use where a POST request to a URL changes state, GET returns the current state, and you want to show the current state after changing:

r.is "foo" do
  r.get do
    # show state
  end

  r.post do
    # change state
    r.redirect
  end
end


600
601
602
603
# File 'lib/roda.rb', line 600

def redirect(path=default_redirect_path, status=302)
  response.redirect(path, status)
  throw :halt, response.finish
end

#responseObject

The response related to the current request. See ResponseMethods for instance methods for the response, but in general the most common usage is to override the response status and headers:

response.status = 200
response['Header-Name'] = 'Header value'


573
574
575
# File 'lib/roda.rb', line 573

def response
  scope.response
end

#root(&block) ⇒ Object

Routing matches that only matches GET requests where the current path is /. If it matches, the match block is executed, and when the match block returns, the rack response is returned.

[r.request_method, r.path_info]
# => ['GET', '/']

r.root do
  # matches
end

This is usuable inside other match blocks:

[r.request_method, r.path_info]
# => ['GET', '/foo/']

r.on 'foo' do
  r.root do
    # matches
  end
end

Note that this does not match non-GET requests:

[r.request_method, r.path_info]
# => ['POST', '/']

r.root do
  # does not match
end

Use r.post "" for POST requests where the current path is /.

Nor does it match empty paths:

[r.request_method, r.path_info]
# => ['GET', '/foo']

r.on 'foo' do
  r.root do
    # does not match
  end
end

Use r.get true to handle GET requests where the current path is empty.



652
653
654
655
656
# File 'lib/roda.rb', line 652

def root(&block)
  if @env[PATH_INFO] == SLASH && is_get?
    always(&block)
  end
end

#run(app) ⇒ Object

Call the given rack app with the environment and return the response from the rack app as the response for this request. This ends the processing of the request:

r.run(proc{[403, {}, []]}) unless r['letmein'] == '1'
r.run(proc{[404, {}, []]})
response.status = 404 # not reached


665
666
667
# File 'lib/roda.rb', line 665

def run(app)
  throw :halt, app.call(@env)
end