Sha256: 7b5ba224292b4aa9e346fdfabb40aac2e6765bc60a2507e541af12453e79394e

Contents?: true

Size: 970 Bytes

Versions: 4

Compression:

Stored size: 970 Bytes

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)
      @link_uri = Addressable::URI.parse(this_link)
      @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

4 entries across 4 versions & 1 rubygems

Version Path
rubyretriever-1.2.3 lib/retriever/link.rb
rubyretriever-1.2.2 lib/retriever/link.rb
rubyretriever-1.2.1 lib/retriever/link.rb
rubyretriever-1.2.0 lib/retriever/link.rb