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
-
.absolutize_urls(content, base) ⇒ String
Make relative URLs absolute.
-
.author_value(value) ⇒ String
Extract a
Stringvalue from a given author attribute. -
.link_value(value) ⇒ String
Extract a
Stringvalue from a given link attribute. -
.value(value) ⇒ String
Extract a
Stringvalue from a given attribute.
Class Method Details
.absolutize_urls(content, base) ⇒ String
Make relative URLs absolute.
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.
44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/yarss/attribute.rb', line 44 def self.(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 |
.link_value(value) ⇒ String
Extract a String value from a given link attribute.
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.
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 |