Class: MetaInspector::Parsers::LinksParser

Inherits:
Base
  • Object
show all
Defined in:
lib/meta_inspector/parsers/links.rb

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from MetaInspector::Parsers::Base

Instance Method Details

#allObject

Returns all links found, unrelavitized and absolutified



16
17
18
# File 'lib/meta_inspector/parsers/links.rb', line 16

def all
  @all ||= raw.map { |link| URL.absolutify(link, base_url) }.compact.uniq
end

#base_hrefObject

Returns the value of the href attribute on the <base /> tag, if exists



54
55
56
# File 'lib/meta_inspector/parsers/links.rb', line 54

def base_href
  parsed.search('base').first.attributes['href'].value rescue nil
end

#base_urlObject

Returns the base url to absolutify relative links. This can be the one set on a <base> tag, or the url of the document if no <base> tag was found.



49
50
51
# File 'lib/meta_inspector/parsers/links.rb', line 49

def base_url
  base_href || url
end

#externalObject

Returns all external HTTP links found



36
37
38
# File 'lib/meta_inspector/parsers/links.rb', line 36

def external
  @external ||= http.select { |link| URL.new(link).host != host }
end

#httpObject

Returns all HTTP links found



21
22
23
# File 'lib/meta_inspector/parsers/links.rb', line 21

def http
  @http ||= all.select { |link| link =~ /^http(s)?:\/\//i}
end

#internalObject

Returns all internal HTTP links found



31
32
33
# File 'lib/meta_inspector/parsers/links.rb', line 31

def internal
  @internal ||= http.select { |link| URL.new(link).host == host }
end


6
7
8
# File 'lib/meta_inspector/parsers/links.rb', line 6

def links
  self
end

#non_httpObject

Returns all non-HTTP links found



26
27
28
# File 'lib/meta_inspector/parsers/links.rb', line 26

def non_http
  @non_http ||= all.select { |link| link !~ /^http(s)?:\/\//i}
end

#rawObject

Returns all links found, unprocessed



11
12
13
# File 'lib/meta_inspector/parsers/links.rb', line 11

def raw
  @raw ||= cleanup(parsed.search('//a/@href')).compact.uniq
end

#to_hashObject



40
41
42
43
44
# File 'lib/meta_inspector/parsers/links.rb', line 40

def to_hash
  { 'internal' => internal,
    'external' => external,
    'non_http' => non_http }
end