Class: HentryConsumer::Element

Inherits:
Object
  • Object
show all
Defined in:
lib/hentry_consumer/element.rb

Direct Known Subclasses

HCard, HEntry

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(element) ⇒ Element

If a single element is give, return that If many elements are given, return as array



7
8
9
10
# File 'lib/hentry_consumer/element.rb', line 7

def initialize(element)
  @element = element
  @items = parse_elements(@element)
end

Instance Attribute Details

#elementObject

Returns the value of attribute element.



3
4
5
# File 'lib/hentry_consumer/element.rb', line 3

def element
  @element
end

#itemsObject

Returns the value of attribute items.



3
4
5
# File 'lib/hentry_consumer/element.rb', line 3

def items
  @items
end

Instance Method Details

#[](key) ⇒ Object



63
64
65
# File 'lib/hentry_consumer/element.rb', line 63

def [](key)
  self.send(key)
end

#[]=(key, value) ⇒ Object



67
68
69
# File 'lib/hentry_consumer/element.rb', line 67

def []=(key, value)
  self.send(key.to_s + "=", value)
end

#assign_value(symbolized_class, value) ⇒ Object



71
72
73
74
75
76
77
78
79
80
# File 'lib/hentry_consumer/element.rb', line 71

def assign_value(symbolized_class, value)
  return unless self.respond_to?(symbolized_class)
  value = value.gsub('\n', " ").strip if value.is_a?(String)
  if FormatRules.can_have_many?(symbolized_class)
    self[symbolized_class] ||= []
    self[symbolized_class] << value
  else
    self[symbolized_class] = value
  end
end

#cleanse_classes(classes) ⇒ Object

:(



45
46
47
48
49
50
51
52
# File 'lib/hentry_consumer/element.rb', line 45

def cleanse_classes(classes)
  if classes =~ /(h-card)/
    if classes =~ /(p-author)/ || classes =~ /(e-g5-client)/
      classes.gsub!(/h-card/, "")
    end
  end
  classes.strip
end

#parse_element(element) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/hentry_consumer/element.rb', line 27

def parse_element(element)
  classes = element["class"]
  # if element has class with microformat prefix it must be a microformat
  if classes =~ /(p|n|e|i|u|dt)-/
    classes = cleanse_classes(classes)
    # parse the element for each microformat class
    classes.split.map do |c|
      parse_microformat(element, c)
    end
  # if element has children it may contain a microformat element
  elsif element.children
    parse_elements(element.children)
  else
    nil # nothing to see here, move along
  end
end

#parse_elements(elements) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/hentry_consumer/element.rb', line 12

def parse_elements(elements)
  # if there are many elements, parse each of them
  if elements.is_a?(Nokogiri::XML::NodeSet)
    # return an array of microformats contained in elements
    elements.map do |element|
      parse_elements(element)
    # just give me a simple array
    end.flatten.compact
  elsif elements.is_a?(Nokogiri::XML::Element)
    parse_element(elements).flatten.compact
  else
    nil # nothing to see here, move along
  end
end

#parse_microformat(element, c) ⇒ Object



54
55
56
57
58
59
60
61
# File 'lib/hentry_consumer/element.rb', line 54

def parse_microformat(element, c)
  method = :"parse_#{c.underscore}"
  if self.respond_to?(method)
    self.send(method, element)
  else
    nil
  end
end

#to_hashObject



82
83
84
85
86
87
88
# File 'lib/hentry_consumer/element.rb', line 82

def to_hash
  { :type => ["element"],
    :properties => {
      :items => self.items,
    }
  }
end

#to_htmlObject



94
95
96
# File 'lib/hentry_consumer/element.rb', line 94

def to_html
  @element.to_html
end

#to_json(*a) ⇒ Object



90
91
92
# File 'lib/hentry_consumer/element.rb', line 90

def to_json(*a)
  to_hash.to_json(a)
end

#to_xmlObject



98
99
100
# File 'lib/hentry_consumer/element.rb', line 98

def to_xml
  @element.to_xml
end