Sha256: c7c3b0205d2075bd48e9b4b7e62049117377c18a561681ca5cd502d54ac23322

Contents?: true

Size: 1.78 KB

Versions: 1

Compression:

Stored size: 1.78 KB

Contents

module MicroMicro
  class Relation
    # @param node [Nokogiri::XML::Element]
    def initialize(node)
      @node = node
    end

    # @return [String]
    def href
      @href ||= Absolutely.to_abs(base: node.document.url, relative: node['href'].strip)
    end

    # @return [String, nil]
    def hreflang
      @hreflang ||= node['hreflang']&.strip
    end

    # @return [String]
    def inspect
      format(%(#<#{self.class.name}:%#0x href: #{href.inspect}, rels: #{rels.inspect}>), object_id)
      # format(%(#<#{self.class.name}:%#0x rels: #{rels}>), object_id)
    end

    # @return [String, nil]
    def media
      @media ||= node['media']&.strip
    end

    # @return [Hash{Symbol => String}]
    def to_h
      {
        href: href,
        rels: rels,
        hreflang: hreflang,
        media: media,
        title: title,
        type: type,
        text: text
      }.select { |_, value| value.present? }
    end

    # @return [Array<String>]
    def rels
      @rels ||= node['rel'].split.uniq.sort
    end

    # @return [String]
    def text
      @text ||= node.text
    end

    # @return [String, nil]
    def title
      @title ||= node['title']&.strip
    end

    # @return [String, nil]
    def type
      @type ||= node['type']&.strip
    end

    # @param context [Nokogiri::HTML::Document, Nokogiri::XML::Element]
    # @return [Nokogiri::XML::NodeSet]
    def self.nodes_from(context)
      context.css('[href][rel]:not([rel=""])').reject { |node| (node.ancestors.map(&:name) & Document.ignored_node_names).any? }
    end

    # @param context [Nokogiri::HTML::Document, Nokogiri::XML::Element]
    # @return [Array<MicroMicro::Relation>]
    def self.relations_from(context)
      nodes_from(context).map { |node| new(node) }
    end

    private

    attr_reader :node
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
micromicro-0.1.0 lib/micro_micro/relation.rb