class Hypermicrodata::Item

Attributes

id[R]
properties[R]
type[R]

Public Class Methods

new(top_node, page_url) click to toggle source
# File lib/hypermicrodata/item.rb, line 13
def initialize(top_node, page_url)
  @top_node = top_node
  @type = extract_itemtype
  @id   = extract_itemid
  @properties = {}
  @links = {}
  @page_url = page_url
  add_itemref_properties(@top_node)
  parse_elements(extract_elements(@top_node))
end
parse(top_node, page_url) click to toggle source
# File lib/hypermicrodata/item.rb, line 5
def self.parse(top_node, page_url)
  if top_node.name == 'form'
    FormItem.new(top_node, page_url)
  else
    Item.new(top_node, page_url)
  end
end

Public Instance Methods

to_hash() click to toggle source
# File lib/hypermicrodata/item.rb, line 24
def to_hash
  hash = {}
  hash[:id] = id if id
  hash[:type] = type if type
  hash[:properties] = {}
  properties.each do |name, same_name_properties|
    final_values = same_name_properties.map do |property|
      if property.item
        property.item.to_hash
      else
        property.value
      end
    end
    hash[:properties][name] = final_values
  end
  hash[:links] = {}
  links.each do |rel, same_rel_links|
    final_values = same_rel_links.map do |link|
      if link.item
        link.item.to_hash
      else
        link.value
      end
    end
    hash[:links][rel] = final_values
  end
  hash
end

Private Instance Methods

add_itemprop(element) click to toggle source

Add an 'itemprop' to the properties

# File lib/hypermicrodata/item.rb, line 84
def add_itemprop(element)
  property = ItempropParser.parse(element, @page_url)
  if property.link? && property.names.empty? && property.rels.empty?
    href = property.value.to_s.strip
    unless href.empty? || href == '#' # href which doesn't work as link is ignored
      (@links[element.name] ||= []) << property
    end
  else
    property.names.each { |name| (@properties[name] ||= []) << property }
    property.rels.each { |rel| (@links[rel] ||= []) << property }
  end
end
add_itemref_properties(element) click to toggle source

Add any properties referred to by 'itemref'

# File lib/hypermicrodata/item.rb, line 98
def add_itemref_properties(element)
  itemref = element.attribute('itemref')
  if itemref
    itemref.value.split(' ').each {|id| parse_elements(find_with_id(id))}
  end
end
extract_elements(node) click to toggle source
# File lib/hypermicrodata/item.rb, line 59
def extract_elements(node)
  node.search('./*')
end
extract_itemid() click to toggle source
# File lib/hypermicrodata/item.rb, line 63
def extract_itemid
  (value = @top_node.attribute('itemid')) ? value.value : nil
end
extract_itemtype() click to toggle source
# File lib/hypermicrodata/item.rb, line 67
def extract_itemtype
  (value = @top_node.attribute('itemtype')) ? value.value.split(' ') : nil
end
find_with_id(id) click to toggle source

Find an element with a matching id

# File lib/hypermicrodata/item.rb, line 106
def find_with_id(id)
  @top_node.search("//*[@id='#{id}']")
end
parse_element(element) click to toggle source
# File lib/hypermicrodata/item.rb, line 75
def parse_element(element)
  itemscope = element.attribute('itemscope')
  itemprop = element.attribute('itemprop')
  internal_elements = extract_elements(element)
  add_itemprop(element) if itemprop || ItempropParser::LINK_ELEMENTS.include?(element.name)
  parse_elements(internal_elements) if internal_elements && !itemscope
end
parse_elements(elements) click to toggle source
# File lib/hypermicrodata/item.rb, line 71
def parse_elements(elements)
  elements.each {|element| parse_element(element)}
end