Module: Rack::Reqorder::RailsRecognizer

Included in:
Rack::Reqorder
Defined in:
lib/rack/reqorder/route_recognizers.rb

Instance Method Summary collapse

Instance Method Details

#prefixesObject



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/rack/reqorder/route_recognizers.rb', line 4

def prefixes
  return @prefixes unless @prefixes.blank?

  @prefixes = {} #{'/mount_prefix' => {search_method: xxx_recognize_path, rack_app: xxx}}
  Rails.application.routes.routes.routes.select{|r| r.defaults.blank?}.each do |route|
    __superclass = route.app.try(:superclass) || route.app.try(:app).try(:superclass)

    next unless __superclass

    case __superclass.to_s
    when 'Sinatra::Base'
      @prefixes[route.path.spec.try(:left).to_s + route.path.spec.try(:right).to_s] = {
        search_method: :sinatra_recognize_path,
        rack_app: route.app.try(:superclass).nil? ? route.app.app : route.app
      }
    when 'Rails::Engine'
      @prefixes[route.path.spec.try(:left).to_s + route.path.spec.try(:right).to_s] = {
        search_method: :rails_recognize_path,
        rack_app: route.app.try(:superclass).nil? ? route.app.app : route.app
      }
    when 'Grape::API'
      @prefixes[route.path.spec.try(:left).to_s + route.path.spec.try(:right).to_s] = {
        search_method: :grape_recognize_path,
        rack_app: route.app.try(:superclass).nil? ? route.app.app : route.app
      }
    end
  end

  return @prefixes
end

#rails_paths(rails_app) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/rack/reqorder/route_recognizers.rb', line 35

def rails_paths(rails_app)
  paths = {}
  rails_app.routes.routes.routes.reverse.each do |route|
    route_key = route.defaults.select do
      |key, value| [:action, :controller].include?(key)
    end

    paths[route_key] = route.path.spec.to_s.gsub('(.:format)', '')
  end

  return paths
end

#rails_recognize_path(path_uri:, rack_app:, options: {}) ⇒ Object

rack_app is basically a rails app here but we keep it for the sake of the interface



81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/rack/reqorder/route_recognizers.rb', line 81

def rails_recognize_path(path_uri:, rack_app:, options: {})
  memoized_var = "@#{rack_app.class.to_s.split('::').join('_').downcase}".to_sym

  if self.instance_variable_get(memoized_var).nil?
    self.instance_variable_set(memoized_var, rails_paths(rack_app))
  end

  Rack::Reqorder.instance_variable_get(memoized_var)[
    rack_app.routes.recognize_path(path_uri, options)
    .select{|key, value| [:action, :controller].include?(key)}
  ]
end

#recognize_path(path_uri, options = {}) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/rack/reqorder/route_recognizers.rb', line 58

def recognize_path(path_uri, options = {})
  prefixes.each do |prefix, engine|
    if path_uri.start_with?(prefix)
      return prefix + self.send(engine[:search_method].to_sym, {
        path_uri: path_uri.gsub(prefix, ''),
        rack_app: engine[:rack_app],
        options: options
      })
    end
  end

  begin
    return rails_recognize_path(
      path_uri: path_uri,
      rack_app: Rails.application,
      options: options
    )
  rescue ActionController::RoutingError
    return "/#{path_uri.split('/')[1]}"
  end
end

#recognize_unknown_pathObject



48
49
50
51
52
53
54
55
56
# File 'lib/rack/reqorder/route_recognizers.rb', line 48

def recognize_unknown_path
  prefixes.each do |prefix, engine|
    if path_uri.start_with?(prefix)
      return prefix
    end
  end

  return ''
end