Class: Hanami::Routing::HttpRouter Private

Inherits:
HttpRouter
  • Object
show all
Defined in:
lib/hanami/routing/http_router.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

HTTP router

This implementation is based on ::HttpRouter (http_router gem).

Hanami::Router wraps an instance of this class, in order to protect its public API from any future change of ::HttpRouter.

Since:

  • 0.1.0

Constant Summary collapse

SCRIPT_NAME =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Script name - rack environment variable

Since:

  • 0.5.0

'SCRIPT_NAME'.freeze
PATH_INFO =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Path info - rack environment variable

Since:

  • 0.7.0

'PATH_INFO'.freeze
DEFAULT_PATH_INFO =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Default PATH_INFO for Rack requests

Since:

  • 0.7.0

'/'.freeze
URL_SEPARATOR =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

URL separator

Since:

  • 0.7.0

'/'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}, &blk) ⇒ HttpRouter

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Initialize the router.

See Also:

Since:

  • 0.1.0



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/hanami/routing/http_router.rb', line 68

def initialize(options = {}, &blk)
  @compiled         = false
  @uri_parser       = URI::Parser.new
  super(options, &nil)

  @namespace        = options[:namespace] if options[:namespace]
  @default_scheme   = options[:scheme]    if options[:scheme]
  @default_host     = options[:host]      if options[:host]
  @default_port     = options[:port]      if options[:port]
  @route_class      = options[:route]    || Routing::Route
  @resolver         = options[:resolver] || Routing::EndpointResolver.new(options)
  @parsers          = Routing::Parsers.new(options[:parsers])
  @prefix           = Utils::PathPrefix.new(options[:prefix] || '')
  @force_ssl        = Hanami::Routing::ForceSsl.new(!!options[:force_ssl], host: @default_host, port: @default_port)
end

Instance Attribute Details

#namespaceObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 0.5.0



56
57
58
# File 'lib/hanami/routing/http_router.rb', line 56

def namespace
  @namespace
end

#prefixObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 0.8.0



60
61
62
# File 'lib/hanami/routing/http_router.rb', line 60

def prefix
  @prefix
end

Instance Method Details

#action_separatorObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Separator between controller and action name.

See Also:

Since:

  • 0.1.0



90
91
92
# File 'lib/hanami/routing/http_router.rb', line 90

def action_separator
  @resolver.action_separator
end

#find(options) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Finds a path from the given options.

See Also:

Since:

  • 0.1.0



100
101
102
# File 'lib/hanami/routing/http_router.rb', line 100

def find(options)
  @resolver.find(options)
end

#mount(app, options) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Allow to mount a Rack app

See Also:

Since:

  • 0.1.1



144
145
146
147
148
# File 'lib/hanami/routing/http_router.rb', line 144

def mount(app, options)
  add("#{ options.fetch(:at) }*", host: options[:host]).to(
    @resolver.resolve(to: app)
  )
end

#no_response(request, env) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 0.1.0



172
173
174
175
176
177
178
# File 'lib/hanami/routing/http_router.rb', line 172

def no_response(request, env)
  if request.acceptable_methods.any? && !request.acceptable_methods.include?(env['REQUEST_METHOD'])
    [405, {'Allow' => request.acceptable_methods.sort.join(", ")}, []]
  else
    @default_app.call(env)
  end
end

#options(path, options = {}, &blk) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Support for OPTIONS HTTP verb

See Also:

Since:

  • 0.1.0



134
135
136
# File 'lib/hanami/routing/http_router.rb', line 134

def options(path, options = {}, &blk)
  add_with_request_method(path, :options, options, &blk)
end

#pass_on_response(response) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 0.1.0



167
168
169
# File 'lib/hanami/routing/http_router.rb', line 167

def pass_on_response(response)
  super response.to_a
end

#raw_call(env, &blk) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 0.1.0



151
152
153
154
155
156
157
# File 'lib/hanami/routing/http_router.rb', line 151

def raw_call(env, &blk)
  if response = @force_ssl.call(env)
    response
  else
    super(@parsers.call(env))
  end
end

#raw_path(route, *args) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Generate a relative URL for a specified named route.

See Also:

Since:

  • 0.1.0



110
111
112
113
114
# File 'lib/hanami/routing/http_router.rb', line 110

def raw_path(route, *args)
  _rescue_url_recognition do
    _custom_path(super(route, *args))
  end
end

#raw_url(route, *args) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Generate an absolute URL for a specified named route.

See Also:

Since:

  • 0.1.0



122
123
124
125
126
# File 'lib/hanami/routing/http_router.rb', line 122

def raw_url(route, *args)
  _rescue_url_recognition do
    _custom_path(super(route, *args))
  end
end

#reset!Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 0.1.0



160
161
162
163
164
# File 'lib/hanami/routing/http_router.rb', line 160

def reset!
  uncompile
  @routes, @named_routes, @root = [], Hash.new{|h,k| h[k] = []}, Node::Root.new(self)
  @default_host, @default_port, @default_scheme = 'localhost', 80, 'http'
end

#rewrite_partial_path_info(env, request) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 0.1.0



181
182
183
184
185
186
187
188
189
190
# File 'lib/hanami/routing/http_router.rb', line 181

def rewrite_partial_path_info(env, request)
  if request.path.empty?
    env[SCRIPT_NAME] += env[PATH_INFO]
    env[PATH_INFO]    = DEFAULT_PATH_INFO
  else
    path_info_before  = env[PATH_INFO].dup
    env[PATH_INFO]    = "/#{@uri_parser.escape(request.path.join(URL_SEPARATOR))}"
    env[SCRIPT_NAME] += path_info_before[0, path_info_before.bytesize - env[PATH_INFO].bytesize]
  end
end