Module: Yarss::Attribute

Defined in:
lib/yarss/attribute.rb

Overview

A bunch of helpers to extract a String value out of a Hash, Array, etc.

Class Method Summary collapse

Class Method Details

.absolutize_urls(content, base) ⇒ String

Make relative URLs absolute.

Parameters:

  • content (String)

    Item content.

  • base (String)

    Base URL.

Returns:

  • (String)


101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/yarss/attribute.rb', line 101

def self.absolutize_urls(content, base)
  return content if base.empty? || content.empty?

  regex = %r{
    (?<=src="|href="|src='|href=')
    /
    ([^/"'].+?)? # Don't match "//xx" but do match "/".
    (?="|')
  }x

  content = content.gsub(regex) do |url|
    "#{base.chomp('/')}#{url}"
  end
  content.gsub!("\r\n", "\n")

  content.freeze
end

.author_value(value) ⇒ String

Extract a String value from a given author attribute.

Examples:

Yarss::Attribute.author_value('Foo') # => 'Foo'
Yarss::Attribute.author_value('name' => 'Foo') # => 'Foo'

Parameters:

  • value (String, Hash)

    An attribute.

Returns:

  • (String)

Raises:



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/yarss/attribute.rb', line 44

def self.author_value(value)
  value ||= ''

  case value
  when Hash
    value(value.fetch('name'))
  when String
    value.strip
  else
    raise ParseError, "Unknown #{value.class} attribute: #{value.inspect}"
  end
rescue KeyError => e
  raise ParseError, e
end

Extract a String value from a given link attribute.

Examples:

Yarss::Attribute.link_value('Foo') # => 'Foo'
Yarss::Attribute.link_value('href' => 'Foo') # => 'Foo'
Yarss::Attribute.link_value([{ 'rel' => 'self', 'href' => 'Foo' }])
  # => 'Foo'
Yarss::Attribute.link_value([{ 'rel' => 'alternate', 'href' => 'Foo' }])
  # => 'Foo'

Parameters:

  • value (String, Hash, Array)

    A link attribute.

Returns:

  • (String)

Raises:

  • (ParseError)

    If type of value is not known or no link is found.



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/yarss/attribute.rb', line 74

def self.link_value(value)
  value ||= ''

  case value
  when Hash
    link_value(value.fetch('href'))
  when Array
    item = value.find { |l| l.is_a?(String) } ||
           value.find { |l| l['rel'] && l['rel'] == 'alternate' } ||
           value.find { |l| l['rel'].nil? } ||
           ''
    link_value(item)
  when String
    value.strip
  else
    raise ParseError, "Unknown #{value.class} attribute: #{value.inspect}"
  end
rescue KeyError => e
  raise ParseError, e
end

.value(value) ⇒ String

Extract a String value from a given attribute.

Examples:

Yarss::Attribute.value('Foo') # => 'Foo'
Yarss::Attribute.value('__content__' => 'Foo') # => 'Foo'

Parameters:

  • value (String, Hash)

    An attribute.

Returns:

  • (String)

Raises:



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/yarss/attribute.rb', line 18

def self.value(value)
  value ||= ''

  case value
  when Hash
    value(value.fetch('__content__'))
  when String
    value.strip
  else
    raise ParseError, "Unknown #{value.class} attribute: #{value.inspect}"
  end
rescue KeyError => e
  raise ParseError, e
end