Sha256: 5fdf5230249ed51f0d90972e6fbdbcaf458beb56f43c680c77dfa22432467946

Contents?: true

Size: 878 Bytes

Versions: 5

Compression:

Stored size: 878 Bytes

Contents

module Retriever
  class Link
    HTTP_RE = Regexp.new(/^http/i).freeze
    SINGLE_SLASH_RE = Regexp.new(/^\/{1}[^\/]/).freeze
    DOUBLE_SLASH_RE = Regexp.new(/^\/{2}[^\/]/).freeze
    NO_SLASH_PAGE_RE = Regexp.new(/^[a-z0-9\-\_\=\?\.]+\z/ix).freeze
    DUB_DUB_DUB_DOT_RE = Regexp.new(/^www\./i).freeze

    def initialize(host, link)
      @host = host
      @link = link
    end

    def path
      return link if HTTP_RE =~ link

      return "http://#{link}" if DUB_DUB_DUB_DOT_RE =~ link

      return "http://#{host}#{link}" if SINGLE_SLASH_RE =~ link

      return "http:#{link}" if DOUBLE_SLASH_RE =~ link #link begins with '//' (maybe a messed up link?)

      return "http://#{host}/#{link}" if NO_SLASH_PAGE_RE =~ link #link uses relative path with no slashes at all, people actually this - imagine that.
    end

    private
    attr_reader :host, :link
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
rubyretriever-1.0.3 lib/retriever/link.rb
rubyretriever-1.0.2 lib/retriever/link.rb
rubyretriever-1.0.1 lib/retriever/link.rb
rubyretriever-1.0.0 lib/retriever/link.rb
rubyretriever-0.1.4 lib/retriever/link.rb