Sha256: 7fd8d7666306a2685b8fc10da74f7c190f7b623a94124dee0bd4b29ff06a3038

Contents?: true

Size: 1.29 KB

Versions: 10

Compression:

Stored size: 1.29 KB

Contents

# frozen_string_literal: true

class Rage::Router::Util
  class << self
    # converts controller name in a path form into a class
    # `api/v1/users` => `Api::V1::UsersController`
    def path_to_class(str)
      str = str.capitalize
      str.gsub!(/([\/_])([a-zA-Z0-9]+)/) do
        if $1 == "/"
          "::#{$2.capitalize}"
        else
          $2.capitalize
        end
      end

      klass = "#{str}Controller"
      if Object.const_defined?(klass)
        Object.const_get(klass)
      else
        raise Rage::Errors::RouterError, "Routing error: could not find the #{klass} class"
      end
    end

    @@names_map = {}

    # converts controller name in a path form into a string representation of a class
    # `api/v1/users` => `"Api::V1::UsersController"`
    def path_to_name(str)
      @@names_map[str] || begin
        @@names_map[str] = path_to_class(str).name
      end
    end
  end

  # @private
  class Cascade
    def initialize(rage_app, rails_app)
      @rage_app = rage_app
      @rails_app = rails_app
    end

    def call(env)
      result = @rage_app.call(env)
      return result if result[0] == :__http_defer__

      if result[1]["X-Cascade"] == "pass" || env["PATH_INFO"].start_with?("/rails/")
        @rails_app.call(env)
      else
        result
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
rage-rb-1.14.0 lib/rage/router/util.rb
rage-rb-1.13.0 lib/rage/router/util.rb
rage-rb-1.12.0 lib/rage/router/util.rb
rage-rb-1.11.0 lib/rage/router/util.rb
rage-rb-1.10.1 lib/rage/router/util.rb
rage-rb-1.10.0 lib/rage/router/util.rb
rage-rb-1.9.0 lib/rage/router/util.rb
rage-rb-1.8.0 lib/rage/router/util.rb
rage-rb-1.7.0 lib/rage/router/util.rb
rage-rb-1.6.0 lib/rage/router/util.rb