class Microdata::Itemprop

Class that parses itemprop elements

Constants

NON_TEXTCONTENT_ELEMENTS
URL_ATTRIBUTES

Attributes

properties[R]

A Hash representing the properties. Hash is of the form {'property name' => 'value'}

Public Class Methods

new(element, page_url=nil) click to toggle source

Create a new Itemprop object

element

The itemprop element to be parsed

page_url

The url of the page, including filename, used to form absolute urls

# File lib/microdata/itemprop.rb, line 25
def initialize(element, page_url=nil)
  @element, @page_url = element, page_url
  @properties = extract_properties
end
parse(element, page_url=nil) click to toggle source

Parse the element and return a hash representing the properties. Hash is of the form {'property name' => 'value'}

element

The itemprop element to be parsed

page_url

The url of the page, including filename, used to form absolute urls

# File lib/microdata/itemprop.rb, line 35
def self.parse(element, page_url=nil)
  self.new(element, page_url).properties
end

Private Instance Methods

extract_properties() click to toggle source
# File lib/microdata/itemprop.rb, line 40
def extract_properties
  prop_names = extract_property_names
  prop_names.each_with_object({}) do |name, memo|
    memo[name] = extract_property
  end
end
extract_property() click to toggle source
# File lib/microdata/itemprop.rb, line 82
def extract_property
  if @element.attribute('itemscope')
    Item.new(@element, @page_url)
  else
    extract_property_value
  end
end
extract_property_names() click to toggle source
# File lib/microdata/itemprop.rb, line 66
def extract_property_names
  itemprop_attr = @element.attribute('itemprop')
  itemprop_attr ? itemprop_attr.value.split() : []
end
extract_property_value() click to toggle source
# File lib/microdata/itemprop.rb, line 71
def extract_property_value
  element = @element.name
  if non_textcontent_element?(element)
    attribute = NON_TEXTCONTENT_ELEMENTS[element]
    value = @element.attribute(attribute).value
    url_attribute?(attribute) ? make_absolute_url(value) : value
  else
    @element.inner_text.strip
  end
end
make_absolute_url(url) click to toggle source

This returns an empty string if can't form a valid absolute url as per the Microdata spec.

# File lib/microdata/itemprop.rb, line 49
def make_absolute_url(url)
  return url unless URI.parse(url).relative?
  begin
    URI.parse(@page_url).merge(url).to_s
  rescue URI::Error
    url
  end
end
non_textcontent_element?(element) click to toggle source
# File lib/microdata/itemprop.rb, line 58
def non_textcontent_element?(element)
  NON_TEXTCONTENT_ELEMENTS.has_key?(element)
end
url_attribute?(attribute) click to toggle source
# File lib/microdata/itemprop.rb, line 62
def url_attribute?(attribute)
  URL_ATTRIBUTES.include?(attribute)
end