Sha256: 7c2a5303ec30bd6526b1d5ad13d037e18592c70fb7cb4f8762fafd3e34fc2666

Contents?: true

Size: 2 KB

Versions: 2

Compression:

Stored size: 2 KB

Contents

module Ogo
  module Parsers
    class Opengraph < Ogo::Parsers::Base

      def initialize(parseable, fallback=false)
        @global_fallback = fallback
        super parseable
      end

      def title(fallback=false)
        if fallback
          super
        else
          _val = find_meta('title')
          (!_val.empty? && _val) ||
            (@global_fallback && super) ||
            ''
        end
      end

      def description(fallback=false)
        if fallback
          super
        else
          _val = find_meta('description')
          (!_val.empty? && _val) ||
            (@global_fallback && super) ||
            ''
        end
      end

      def image(fallback=false)
        if fallback
          super
        else
          _val = find_meta('image')
          if _val.empty?
            (@global_fallback && super) || nil
          else
            host_uri = Addressable::URI.parse(url)
            Ogo::ImageInfo.new(url: fix_image_path(_val, host_uri))
          end
        end
      end

      def type(fallback=false)
        if fallback
          super
        else
          _val = find_meta('type')
          (!_val.empty? && _val) ||
            (@global_fallback && super) ||
            ''
        end
      end

      def metadata(fallback=false)
        _meta = super
        if fallback
          _meta[:fallback] = {
            title: title(true),
            description: description(true),
            type: type(true),
            image: nil
          }
          img = image(true)
          if img
            _meta[:fallback][:image] = {
              url:    img.url,
              width:  img.width,
              height: img.height,
              type:   img.type
            }
          end
        end
        _meta
      end

      private

      def find_meta(meta_type)
        tag = page.doc.xpath('//head//meta').find { |it|
          it.attribute('property').to_s == "og:#{meta_type}"
        }
        (tag && tag.attribute('content')).to_s.strip
      end

    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ogo-0.1.2 lib/ogo/parsers/opengraph.rb
ogo-0.1.1 lib/ogo/parsers/opengraph.rb