Sha256: 13bdfc76233cf8fb2368d02177eb1fa66420a72f33712760792bf7b2c9b621ee

Contents?: true

Size: 1.09 KB

Versions: 15

Compression:

Stored size: 1.09 KB

Contents

require 'sitehub/constants'
require 'sitehub/memoize'
class SiteHub
  class RequestMapping
    extend Memoize
    include Equality

    attr_reader :source_url, :mapped_url, :mapped_path

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

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

    def computed_uri
      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
      URI(mapped_url)
    end
    memoize :computed_uri

    def host
      URI(source_url).host
    end
    memoize :host

    private

    def url_scanner_regex
      /#{BASE_URL_MATCHER.source}#{mapped_path.source}/
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
sitehub-0.5.0.alpha12 lib/sitehub/request_mapping.rb
sitehub-0.4.10 lib/sitehub/request_mapping.rb
sitehub-0.5.0.alpha11 lib/sitehub/request_mapping.rb
sitehub-0.5.0.alpha10 lib/sitehub/request_mapping.rb
sitehub-0.5.0.alpha8 lib/sitehub/request_mapping.rb
sitehub-0.5.0.alpha7 lib/sitehub/request_mapping.rb
sitehub-0.5.0.alpha6 lib/sitehub/request_mapping.rb
sitehub-0.5.0.alpha5 lib/sitehub/request_mapping.rb
sitehub-0.5.0.alpha4 lib/sitehub/request_mapping.rb
sitehub-0.5.0.alpha3 lib/sitehub/request_mapping.rb
sitehub-0.5.0.alpha2 lib/sitehub/request_mapping.rb
sitehub-0.4.9 lib/sitehub/request_mapping.rb
sitehub-0.4.8 lib/sitehub/request_mapping.rb
sitehub-0.4.7 lib/sitehub/request_mapping.rb
sitehub-0.4.6 lib/sitehub/request_mapping.rb