Class: Karst::Web::RouteLookup

Inherits:
Object
  • Object
show all
Defined in:
lib/karst/web/route_lookup.rb

Overview

Establishes route context for a manually entered application path. Rails' router is the authority: Karst does not infer a controller or action from the shape of the URL.

Defined Under Namespace

Classes: Result

Instance Method Summary collapse

Constructor Details

#initialize(path:, http_method:, application: Rails.application) ⇒ RouteLookup

Returns a new instance of RouteLookup.



14
15
16
17
18
# File 'lib/karst/web/route_lookup.rb', line 14

def initialize(path:, http_method:, application: Rails.application)
  @path = path.to_s.strip
  @http_method = http_method.to_s.strip.upcase
  @application = application
end

Instance Method Details

#callObject



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/karst/web/route_lookup.rb', line 20

def call
  path = local_path
  recognized = @application.routes.recognize_path(path, method: method)
  return limitation("the recognized route did not identify a controller and action") unless complete?(recognized)

  Result.new(params: recognized_params(recognized, path))
rescue URI::InvalidURIError
  limitation("Path must be a valid local application path.")
rescue ActionController::RoutingError
  limitation("Rails could not recognize that path and method. Karst will not guess the route.")
rescue StandardError => e
  limitation("Rails route recognition was unavailable (#{e.class}). Karst will not guess the route.")
end