Class: Retriever::Link

Inherits:
Object
  • Object
show all
Defined in:
lib/retriever/link.rb

Constant Summary collapse

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

Instance Method Summary collapse

Constructor Details

#initialize(host, link) ⇒ Link

Returns a new instance of Link.



10
11
12
13
# File 'lib/retriever/link.rb', line 10

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

Instance Method Details

#path ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/retriever/link.rb', line 15

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

  # link begins with '//'
  return "http:#{link}" if DOUBLE_SLASH_RE =~ link

  # link uses relative path with no slashes at all
  return "http://#{host}/#{link}" if NO_SLASH_PAGE_RE =~ link
end