Class: HentryConsumer::Element
- Inherits:
-
Object
- Object
- HentryConsumer::Element
- Defined in:
- lib/hentry_consumer/element.rb
Instance Attribute Summary collapse
-
#element ⇒ Object
Returns the value of attribute element.
-
#items ⇒ Object
Returns the value of attribute items.
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
- #assign_value(symbolized_class, value) ⇒ Object
-
#cleanse_classes(classes) ⇒ Object
:(.
-
#initialize(element) ⇒ Element
constructor
If a single element is give, return that If many elements are given, return as array.
- #parse_element(element) ⇒ Object
- #parse_elements(elements) ⇒ Object
- #parse_h_card(element) ⇒ Object
- #parse_h_entry(element) ⇒ Object
- #parse_microformat(element, c) ⇒ Object
- #symbolize_class(c) ⇒ Object
- #to_hash ⇒ Object
- #to_html ⇒ Object
- #to_json(*a) ⇒ Object
- #to_xml ⇒ Object
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
#element ⇒ Object
Returns the value of attribute element.
3 4 5 |
# File 'lib/hentry_consumer/element.rb', line 3 def element @element end |
#items ⇒ Object
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
76 77 78 |
# File 'lib/hentry_consumer/element.rb', line 76 def [](key) self.send(key) end |
#[]=(key, value) ⇒ Object
80 81 82 |
# File 'lib/hentry_consumer/element.rb', line 80 def []=(key, value) self.send(key.to_s + "=", value) end |
#assign_value(symbolized_class, value) ⇒ Object
84 85 86 87 88 89 90 91 92 93 |
# File 'lib/hentry_consumer/element.rb', line 84 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) else nil # nothing to see here, move along end end |
#parse_h_card(element) ⇒ Object
63 64 65 |
# File 'lib/hentry_consumer/element.rb', line 63 def parse_h_card(element) HCard.new(element.children) end |
#parse_h_entry(element) ⇒ Object
67 68 69 |
# File 'lib/hentry_consumer/element.rb', line 67 def parse_h_entry(element) HEntry.new(element.children) 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 |
#symbolize_class(c) ⇒ Object
72 73 74 |
# File 'lib/hentry_consumer/element.rb', line 72 def symbolize_class(c) c.to_s.downcase.gsub(/\w{1,2}-/, "").to_sym end |
#to_hash ⇒ Object
95 96 97 98 99 100 101 |
# File 'lib/hentry_consumer/element.rb', line 95 def to_hash { :type => ["element"], :properties => { :items => self.items, } } end |
#to_html ⇒ Object
107 108 109 |
# File 'lib/hentry_consumer/element.rb', line 107 def to_html @element.to_html end |
#to_json(*a) ⇒ Object
103 104 105 |
# File 'lib/hentry_consumer/element.rb', line 103 def to_json(*a) to_hash.to_json(a) end |
#to_xml ⇒ Object
111 112 113 |
# File 'lib/hentry_consumer/element.rb', line 111 def to_xml @element.to_xml end |