Module: Synopsis

Defined in:
lib/synopsis.rb

Class Method Summary collapse

Class Method Details

.synopsis(url) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/synopsis.rb', line 6

def self.synopsis(url)

   doc = nil
   description = nil

   begin
		doc = Nokogiri::HTML(open(url))
   rescue
     doc = nil
   end

   # Try the meta description tag first
   if doc
		doc.css('meta').each do |meta|
			if meta.attributes['name'] && meta.attributes['name'].value == 'description'
				description = meta.attributes['content'].value
			end
		end
   end

   # if that fails use bing
   if description.nil? || description.length == 0
 		begin
  		bing_url = "http://www.bing.com/search?q=" + GI.escapeHTML(url)
 			bing_doc = Nokogiri::HTML(open(bing_url))
 			description = bing_doc.css('p').first.to_s.gsub(/<\/?[^>]*>/, "")
 		rescue
       description = nil
 		end
   end

   # still failing grab the first para from the original doc
   if description.nil? || description.length == 0
			description = doc.css('p').first.to_s.gsub(/<\/?[^>]*>/, "")
   end

   description

end