Sha256: 00ab868be8dad377056ca7eda0a137f34481d4ffc90a38c42caefc8440ea1807

Contents?: true

Size: 1.19 KB

Versions: 2

Compression:

Stored size: 1.19 KB

Contents

require 'addressable/uri'
module Retriever
  #
  class Link
    # HTTP_RE = Regexp.new(/^http/i).freeze
    SLASH_RE = Regexp.new(%r(^/{1}[^/])).freeze
    DOUBLE_SLASH_RE = Regexp.new(%r(^/{2}[^/])).freeze
    WWW_DOT_RE = Regexp.new(/^www\./i).freeze

    def initialize(target_scheme, target_host, this_link)
      begin
        @link_uri = Addressable::URI.parse(Addressable::URI.encode(this_link)).normalize
      rescue Addressable::URI::InvalidURIError => e
        dummy_link = Retriever::Link.new(target_scheme, target_host, target_host)
        @link_uri = Addressable::URI.parse(dummy_link.path)
      end
      @scheme = target_scheme
      @host = target_host
      @this_link = @link_uri.to_s
    end

    def path
      return this_link if link_uri.absolute?

      return "#{@scheme}://#{this_link}" if WWW_DOT_RE =~ this_link

      return "#{@scheme}://#{host}#{this_link}" if SLASH_RE =~ this_link

      # link begins with '//'
      return "#{@scheme}:#{this_link}" if DOUBLE_SLASH_RE =~ this_link

      # link uses relative path with no slashes at all
      return "#{@scheme}://#{host}/#{this_link}" if link_uri.relative?
    end

    private

    attr_reader :this_link, :host, :link_uri
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rubyretriever-1.3.0 lib/retriever/link.rb
rubyretriever-1.2.4 lib/retriever/link.rb