class Gemstash::RackEnvRewriter::Context

Context containing the logic and the actual Rack environment.

Public Class Methods

new(rewriter, rack_env) click to toggle source
# File lib/gemstash/rack_env_rewriter.rb, line 26
def initialize(rewriter, rack_env)
  @rewriter = rewriter
  @rack_env = rack_env
end

Public Instance Methods

captures() click to toggle source
# File lib/gemstash/rack_env_rewriter.rb, line 50
def captures
  @params ||= begin
    check_match
    @path_info_match.names.inject({}) do |result, name|
      result[name] = @path_info_match[name]
      result
    end
  end
end
matches?() click to toggle source
# File lib/gemstash/rack_env_rewriter.rb, line 31
def matches?
  matches_request_uri? && matches_path_info?
end
rewrite() click to toggle source
# File lib/gemstash/rack_env_rewriter.rb, line 35
def rewrite
  check_match
  log_start = "Rewriting '#{@rack_env["REQUEST_URI"]}'"

  new_request_uri = @rack_env["REQUEST_URI"].dup
  new_request_uri[@request_uri_match.begin(0)...@request_uri_match.end(0)] = ""

  new_path_info = @rack_env["PATH_INFO"].dup
  new_path_info[@path_info_match.begin(0)...@path_info_match.end(0)] = ""

  @rack_env["REQUEST_URI"] = new_request_uri
  @rack_env["PATH_INFO"]   = new_path_info
  log.info "#{log_start} to '#{@rack_env["REQUEST_URI"]}'"
end

Private Instance Methods

check_match() click to toggle source
# File lib/gemstash/rack_env_rewriter.rb, line 70
def check_match
  raise "Rack env did not match!" unless @request_uri_match && @path_info_match
end
matches_path_info?() click to toggle source
# File lib/gemstash/rack_env_rewriter.rb, line 66
def matches_path_info?
  @path_info_match ||= @rack_env["PATH_INFO"].match(regexp)
end
matches_request_uri?() click to toggle source
# File lib/gemstash/rack_env_rewriter.rb, line 62
def matches_request_uri?
  @request_uri_match ||= @rack_env["REQUEST_URI"].match(regexp)
end