Class: MetaInspector::Parsers::ImagesParser
- Includes:
- Enumerable
- Defined in:
- lib/meta_inspector/parsers/images.rb
Instance Method Summary collapse
-
#best ⇒ Object
Returns either the Facebook Open Graph image, twitter suggested image or the largest image in the image collection.
-
#favicon ⇒ Object
Return favicon url if exist.
- #images ⇒ Object
-
#initialize(main_parser, options = {}) ⇒ ImagesParser
constructor
A new instance of ImagesParser.
-
#largest ⇒ Object
Returns the largest image from the image collection, filtered for images that are more square than 10:1 or 1:10.
-
#owner_suggested ⇒ Object
Returns the parsed image from Facebook’s open graph property tags Most major websites now define this property and is usually relevant See doc at developers.facebook.com/docs/opengraph/ If none found, tries with Twitter image.
Constructor Details
#initialize(main_parser, options = {}) ⇒ ImagesParser
Returns a new instance of ImagesParser.
11 12 13 14 |
# File 'lib/meta_inspector/parsers/images.rb', line 11 def initialize(main_parser, = {}) @download_images = [:download_images] super(main_parser) end |
Instance Method Details
#best ⇒ Object
Returns either the Facebook Open Graph image, twitter suggested image or the largest image in the image collection
22 23 24 |
# File 'lib/meta_inspector/parsers/images.rb', line 22 def best owner_suggested || largest end |
#favicon ⇒ Object
Return favicon url if exist
61 62 63 64 65 66 67 |
# File 'lib/meta_inspector/parsers/images.rb', line 61 def favicon query = '//link[@rel="icon" or contains(@rel, "shortcut")]' value = parsed.xpath(query)[0].attributes['href'].value @favicon ||= URL.absolutify(value, base_url) rescue nil end |
#images ⇒ Object
16 17 18 |
# File 'lib/meta_inspector/parsers/images.rb', line 16 def images self end |
#largest ⇒ Object
Returns the largest image from the image collection, filtered for images that are more square than 10:1 or 1:10
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/meta_inspector/parsers/images.rb', line 36 def largest() @larget_image ||= begin img_nodes = parsed.search('//img').select{ |img_node| img_node['src'] } sizes = img_nodes.map { |img_node| [URL.absolutify(img_node['src'], base_url), img_node['width'], img_node['height']] } sizes.uniq! { |url, width, height| url } if @download_images sizes.map! do |url, width, height| width, height = FastImage.size(url) if width.nil? || height.nil? [url, width, height] end else sizes.map! do |url, width, height| width, height = [0, 0] if width.nil? || height.nil? [url, width, height] end end sizes.map! { |url, width, height| [url, width.to_i * height.to_i, width.to_f / height.to_f] } sizes.keep_if { |url, area, ratio| ratio > 0.1 && ratio < 10 } sizes.sort_by! { |url, area, ratio| -area } url, area, ratio = sizes.first url end end |
#owner_suggested ⇒ Object
Returns the parsed image from Facebook’s open graph property tags Most major websites now define this property and is usually relevant See doc at developers.facebook.com/docs/opengraph/ If none found, tries with Twitter image
30 31 32 |
# File 'lib/meta_inspector/parsers/images.rb', line 30 def owner_suggested ['og:image'] || ['twitter:image'] end |