Class: MicroMicro::Relationship

Inherits:
Object
  • Object
show all
Defined in:
lib/micro_micro/relationship.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node) ⇒ MicroMicro::Relationship

Parse a node for relationship data.

Parameters:

  • node (Nokogiri::XML::Element)
[View source]

25
26
27
# File 'lib/micro_micro/relationship.rb', line 25

def initialize(node)
  @node = node
end

Instance Attribute Details

#collectionMicroMicro::RelationshipsCollection

The MicroMicro::RelationshipsCollection to which this MicroMicro::Relationship belongs.

Returns:

  • (MicroMicro::RelationshipsCollection)

9
10
11
# File 'lib/micro_micro/relationship.rb', line 9

def collection
  @collection
end

Class Method Details

.from_context(context) ⇒ Array<MicroMicro::Relationship>

Extract MicroMicro::Relationships from a context.

Parameters:

  • context (Nokogiri::HTML5::Document, Nokogiri::XML::Element)

Returns:

[View source]

15
16
17
18
19
# File 'lib/micro_micro/relationship.rb', line 15

def self.from_context(context)
  context
    .css(%([href][rel]:not([rel=""])))
    .filter_map { |node| new(node) unless Helpers.ignore_nodes?(node.ancestors) }
end

Instance Method Details

#hrefString

The value of the node’s href attribute.

Returns:

  • (String)
[View source]

32
33
34
# File 'lib/micro_micro/relationship.rb', line 32

def href
  @href ||= node["href"]
end

#hreflangString?

The value of the node’s hreflang attribute, if present.

Returns:

  • (String, nil)
[View source]

39
40
41
# File 'lib/micro_micro/relationship.rb', line 39

def hreflang
  @hreflang ||= node["hreflang"]&.strip
end

#inspectString

:nocov:

Returns:

  • (String)
[View source]

46
47
48
49
50
# File 'lib/micro_micro/relationship.rb', line 46

def inspect
  "#<#{self.class}:#{format("%#0x", object_id)} " \
    "href: #{href.inspect}, " \
    "rels: #{rels.inspect}>"
end

#mediaString?

The value of the node’s media attribute, if present.

Returns:

  • (String, nil)
[View source]

56
57
58
# File 'lib/micro_micro/relationship.rb', line 56

def media
  @media ||= node["media"]&.strip
end

#relsArray<String>

An Array of unique values from node’s rel attribute.

Returns:

  • (Array<String>)
[View source]

78
79
80
# File 'lib/micro_micro/relationship.rb', line 78

def rels
  @rels ||= Set[*node["rel"].split].to_a.sort
end

#textString

The node’s text content.

Returns:

  • (String)
[View source]

85
86
87
# File 'lib/micro_micro/relationship.rb', line 85

def text
  @text ||= node.text
end

#titleString?

The value of the node’s title attribute, if present.

Returns:

  • (String, nil)
[View source]

92
93
94
# File 'lib/micro_micro/relationship.rb', line 92

def title
  @title ||= node["title"]&.strip
end

#to_hHash{Symbol => String}

Return the parsed MicroMicro::Relationship as a Hash.

Returns:

  • (Hash{Symbol => String})
[View source]

63
64
65
66
67
68
69
70
71
72
73
# File 'lib/micro_micro/relationship.rb', line 63

def to_h
  {
    href: href,
    rels: rels,
    hreflang: hreflang,
    media: media,
    title: title,
    type: type,
    text: text
  }.compact_blank!
end

#typeString?

The value of the node’s type attribute, if present.

Returns:

  • (String, nil)
[View source]

99
100
101
# File 'lib/micro_micro/relationship.rb', line 99

def type
  @type ||= node["type"]&.strip
end