Sha256: 5c0e1d8af55a8b259166d4249bb7c82cd7cb29bbb972505f143b4c04938da8ba

Contents?: true

Size: 1.26 KB

Versions: 2

Compression:

Stored size: 1.26 KB

Contents

require 'sitehub/constants'
class SiteHub
  class RequestMapping
    attr_reader :source_url, :mapped_url, :mapped_path

    BASE_URL_MATCHER = %r{^\w+://[\w+\.-]+(:\d+)?}
    CAPTURE_GROUP_REFERENCE='$%s'
    USER_SUPPLIED_CAPTURE = 1..-1

    def initialize(source_url:, downstream_url: EMPTY_STRING, mapped_url: EMPTY_STRING, mapped_path:)
      @source_url, @mapped_url = source_url, mapped_url.dup
      @mapped_path = mapped_path.is_a?(Regexp) ? mapped_path : Regexp.new(mapped_path)
      @downstream_url = downstream_url
    end

    def cookie_path
      if mapped_path.is_a?(Regexp)
        mapped_path.source[/^(.*)?\(/,1].gsub(/\/$/, '')
      end
    end


    def computed_uri
      @computed_uri ||= begin
        url_components = url_scanner_regex.match(source_url).captures[USER_SUPPLIED_CAPTURE]
        mapped_url.tap do |url|
          url_components.each_with_index do |match, index|
            url.gsub!(CAPTURE_GROUP_REFERENCE % (index+1), match)
          end
        end
      end
    end

    def == other
      other.is_a?(RequestMapping) && source_url == other.source_url && mapped_url == other.mapped_url && mapped_path == other.mapped_path
    end

    private
    def url_scanner_regex
      %r{#{BASE_URL_MATCHER.source}#{mapped_path.source}}
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
sitehub-0.4.2 lib/sitehub/request_mapping.rb
sitehub-0.4.1 lib/sitehub/request_mapping.rb