class Microdata::Item
Attributes
id[R]
properties[R]
type[R]
Public Class Methods
new(top_node, page_url)
click to toggle source
# File lib/microdata/item.rb, line 5 def initialize(top_node, page_url) @top_node = top_node @type = extract_itemtype @id = extract_itemid @properties = {} @page_url = page_url add_itemref_properties(@top_node) parse_elements(extract_elements(@top_node)) end
Public Instance Methods
to_hash()
click to toggle source
# File lib/microdata/item.rb, line 15 def to_hash hash = {} hash[:id] = id if id hash[:type] = type hash[:properties] = {} properties.each do |name, values| final_values = values.map do |value| if value.is_a?(Item) value.to_hash else value end end hash[:properties][name] = final_values end hash end
Private Instance Methods
add_itemprop(itemprop)
click to toggle source
Add an 'itemprop' to the properties
# File lib/microdata/item.rb, line 60 def add_itemprop(itemprop) properties = Itemprop.parse(itemprop, @page_url) properties.each { |name, value| (@properties[name] ||= []) << value } end
add_itemref_properties(element)
click to toggle source
Add any properties referred to by 'itemref'
# File lib/microdata/item.rb, line 66 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/microdata/item.rb, line 35 def extract_elements(node) node.search('./*') end
extract_itemid()
click to toggle source
# File lib/microdata/item.rb, line 39 def extract_itemid (value = @top_node.attribute('itemid')) ? value.value : nil end
extract_itemtype()
click to toggle source
# File lib/microdata/item.rb, line 43 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/microdata/item.rb, line 74 def find_with_id(id) @top_node.search("//*[@id='#{id}']") end
parse_element(element)
click to toggle source
# File lib/microdata/item.rb, line 51 def parse_element(element) itemscope = element.attribute('itemscope') itemprop = element.attribute('itemprop') internal_elements = extract_elements(element) add_itemprop(element) if itemscope || itemprop parse_elements(internal_elements) if internal_elements && !itemscope end
parse_elements(elements)
click to toggle source
# File lib/microdata/item.rb, line 47 def parse_elements(elements) elements.each {|element| parse_element(element)} end