Class: Favico::Parser

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/favico.rb,
lib/favico/parser.rb

Instance Method Summary collapse

Instance Method Details

#fetch_favicon(url) ⇒ Object



14
15
16
17
18
19
20
21
# File 'lib/favico/parser.rb', line 14

def fetch_favicon(url)
  favicon_url = fetch_favicon_url(url)
  response = self.class.get(favicon_url)
  if not response.nil? && response.code == 200
    return response.body
  end
  nil
end

#fetch_favicon_url(url) ⇒ Object



23
24
25
26
27
28
# File 'lib/favico/parser.rb', line 23

def fetch_favicon_url(url)
  if root_favicon?(url)
    return url + "/favicon.ico"
  end
  parse_favicon(url)
end

#parse_favicon(url) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/favico/parser.rb', line 30

def parse_favicon(url)
  response = self.class.get(url)
  favicon = xpath_parse(response.body)
  if (!favicon.start_with?("http"))
    favicon = url + "/" + favicon
  end
  favicon
end

#root_favicon?(url) ⇒ Boolean

Returns:

  • (Boolean)


8
9
10
11
12
# File 'lib/favico/parser.rb', line 8

def root_favicon?(url)
  url = url + "/favicon.ico"
  response = self.class.head(url)
  return response.code == 200
end

#xpath_parse(body) ⇒ Object



39
40
41
42
# File 'lib/favico/parser.rb', line 39

def xpath_parse(body)
  doc = Nokogiri::HTML(body)
  doc.xpath("//link[contains(@rel, 'icon')]/@href")[0].value
end