Class: LookUpUrl
- Inherits:
-
Object
- Object
- LookUpUrl
- Defined in:
- lib/lookupurl.rb
Instance Attribute Summary collapse
-
#description ⇒ Object
Returns the value of attribute description.
-
#host ⇒ Object
Returns the value of attribute host.
-
#image_url ⇒ Object
Returns the value of attribute image_url.
-
#title ⇒ Object
Returns the value of attribute title.
-
#url ⇒ Object
Returns the value of attribute url.
Instance Method Summary collapse
Instance Attribute Details
#description ⇒ Object
Returns the value of attribute description.
6 7 8 |
# File 'lib/lookupurl.rb', line 6 def description @description end |
#host ⇒ Object
Returns the value of attribute host.
6 7 8 |
# File 'lib/lookupurl.rb', line 6 def host @host end |
#image_url ⇒ Object
Returns the value of attribute image_url.
6 7 8 |
# File 'lib/lookupurl.rb', line 6 def image_url @image_url end |
#title ⇒ Object
Returns the value of attribute title.
6 7 8 |
# File 'lib/lookupurl.rb', line 6 def title @title end |
#url ⇒ Object
Returns the value of attribute url.
6 7 8 |
# File 'lib/lookupurl.rb', line 6 def url @url end |
Instance Method Details
#crawl_url(link) ⇒ Object
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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/lookupurl.rb', line 7 def crawl_url(link) url = link @url = url @host = URI.parse(url).host begin # get the url content response = HTTParty.get(url) # /get the url content # nokogiri in action doc = Nokogiri::HTML(response) #----- description ---- @description = "" doc.xpath("//meta[@name='description']/@content").each do |attr| @description = attr.value end if @description == "" doc.xpath("//p").each do |attr| @description = attr.text break end end if @description == "" doc.xpath("//meta[@name='description']/@content").each do |attr| @description = "" @description = attr.value end end if @description == "" doc.xpath("//meta[@property='og:description']/@content").each do |attr| @description = "" @description = attr.value end end #----- /description -------- #----- images -------- images = [] doc.xpath("//img/@src").each do |attr| images << attr.value if attr.value.include? ".jp" or attr.value.include? ".png" end doc.xpath("//meta[@property='og:image']/@content").each do |attr| if attr.value.include? ".jp" or attr.value.include? ".png" images = [] images << attr.value end end image = images.first #----- /images -------- # #----- title -------- if doc.at_css("title") @title = doc.at_css("title").text else @title = nil end #----- /title -------- # /nokogiri in action # # rescue rescue @title = @host @description = @url return false end # end rescue if image == "" || image == nil @image_url = "" else @image_url = image.force_encoding('iso-8859-1').encode('utf-8') end if @title == "" || @title == nil @title = @host else @title = @title.force_encoding('iso-8859-1').encode('utf-8').to_s[0..100] end if @description == "" || @description == nil @description = @url else @description = @description.force_encoding('iso-8859-1').encode('utf-8') end end |