Module: Grabber::Util

Included in:
Page, Site
Defined in:
lib/grabber/util.rb

Instance Method Summary collapse

Instance Method Details

#format_url(url) ⇒ Object



4
5
6
7
8
9
10
11
12
# File 'lib/grabber/util.rb', line 4

def format_url(url)
  url = strip_non_url_parts(url)
  if URI.parse(url).relative?
    url = URI.join("#{uri.scheme}://#{uri.host}", url).to_s
  end

  url.chop! while url.end_with?('/')
  with_url_protocol(url)
end

#strip_non_url_parts(link) ⇒ Object



18
19
20
21
22
23
24
25
26
# File 'lib/grabber/util.rb', line 18

def strip_non_url_parts(link)
  if (index = (link =~ /#/))
    link.slice!(index..link.size)
  end
  if (index = (link =~ /\?/))
    link.slice!(index..link.size)
  end
  link
end

#uriObject



28
29
30
# File 'lib/grabber/util.rb', line 28

def uri
  URI.parse(@url)
end

#with_url_protocol(path) ⇒ Object



14
15
16
# File 'lib/grabber/util.rb', line 14

def with_url_protocol(path)
  path =~ /^http/ ? path : 'http://' + path
end