Class: Sevennet::Element
- Inherits:
-
Object
- Object
- Sevennet::Element
- Defined in:
- lib/sevennet/api.rb
Overview
Internal wrapper class to provide convenient method to access Nokogiri element value.
Class Method Summary collapse
-
.get(element, path = '.') ⇒ Object
Return the text value of an element.
-
.get_array(element, path = '.') ⇒ Object
Return an array of values based on the given path.
-
.get_hash(element, path = '.') ⇒ Object
Return child element text values of the given path.
-
.get_unescaped(element, path = '.') ⇒ Object
Return an unescaped text value of an element.
Instance Method Summary collapse
-
#/(path) ⇒ Object
Returns a Nokogiri::XML::NodeSet of elements matching the given path.
- #attributes ⇒ Object
-
#elem ⇒ Object
Returns Nokogiri::XML::Element object.
-
#get(path = '.') ⇒ Object
Get the text value of the given path, leave empty to retrieve current element value.
-
#get_array(path = '.') ⇒ Object
Get the array values of the given path.
-
#get_element(path) ⇒ Object
Similar with search_and_convert but always return first element if more than one elements found.
-
#get_elements(path) ⇒ Object
Return an array of Sevennet::Element matching the given path.
-
#get_hash(path = '.') ⇒ Object
Get the children element text values in hash format with the element names as the hash keys.
-
#get_unescaped(path = '.') ⇒ Object
Get the unescaped HTML text of the given path.
-
#initialize(element) ⇒ Element
constructor
Pass Nokogiri::XML::Element object.
- #to_s ⇒ Object
Constructor Details
#initialize(element) ⇒ Element
Pass Nokogiri::XML::Element object
271 272 273 |
# File 'lib/sevennet/api.rb', line 271 def initialize(element) @element = element end |
Class Method Details
.get(element, path = '.') ⇒ Object
Return the text value of an element.
229 230 231 232 233 234 |
# File 'lib/sevennet/api.rb', line 229 def get(element, path='.') return unless element result = element.at_xpath(path) result = result.inner_html if result result end |
.get_array(element, path = '.') ⇒ Object
Return an array of values based on the given path.
243 244 245 246 247 248 249 250 251 252 |
# File 'lib/sevennet/api.rb', line 243 def get_array(element, path='.') return unless element result = element/path if (result.is_a? Nokogiri::XML::NodeSet) || (result.is_a? Array) result.collect { |item| self.get(item) } else [self.get(result)] end end |
.get_hash(element, path = '.') ⇒ Object
Return child element text values of the given path.
255 256 257 258 259 260 261 262 263 264 265 266 267 |
# File 'lib/sevennet/api.rb', line 255 def get_hash(element, path='.') return unless element result = element.at_xpath(path) if result hash = {} result = result.children result.each do |item| hash[item.name] = item.inner_html end hash end end |
.get_unescaped(element, path = '.') ⇒ Object
Return an unescaped text value of an element.
237 238 239 240 |
# File 'lib/sevennet/api.rb', line 237 def get_unescaped(element, path='.') result = self.get(element, path) CGI.unescape(result) if result end |
Instance Method Details
#/(path) ⇒ Object
Returns a Nokogiri::XML::NodeSet of elements matching the given path. Example: element/“author”.
281 282 283 284 285 |
# File 'lib/sevennet/api.rb', line 281 def /(path) elements = @element/path return nil if elements.size == 0 elements end |
#attributes ⇒ Object
320 321 322 323 |
# File 'lib/sevennet/api.rb', line 320 def attributes return unless self.elem self.elem.attributes end |
#elem ⇒ Object
Returns Nokogiri::XML::Element object
276 277 278 |
# File 'lib/sevennet/api.rb', line 276 def elem @element end |
#get(path = '.') ⇒ Object
Get the text value of the given path, leave empty to retrieve current element value.
301 302 303 |
# File 'lib/sevennet/api.rb', line 301 def get(path='.') Element.get(@element, path) end |
#get_array(path = '.') ⇒ Object
Get the array values of the given path.
311 312 313 |
# File 'lib/sevennet/api.rb', line 311 def get_array(path='.') Element.get_array(@element, path) end |
#get_element(path) ⇒ Object
Similar with search_and_convert but always return first element if more than one elements found
295 296 297 298 |
# File 'lib/sevennet/api.rb', line 295 def get_element(path) elements = get_elements(path) elements[0] if elements end |
#get_elements(path) ⇒ Object
Return an array of Sevennet::Element matching the given path
288 289 290 291 292 |
# File 'lib/sevennet/api.rb', line 288 def get_elements(path) elements = self./(path) return unless elements elements = elements.map{|element| Element.new(element)} end |
#get_hash(path = '.') ⇒ Object
Get the children element text values in hash format with the element names as the hash keys.
316 317 318 |
# File 'lib/sevennet/api.rb', line 316 def get_hash(path='.') Element.get_hash(@element, path) end |
#get_unescaped(path = '.') ⇒ Object
Get the unescaped HTML text of the given path.
306 307 308 |
# File 'lib/sevennet/api.rb', line 306 def get_unescaped(path='.') Element.get_unescaped(@element, path) end |
#to_s ⇒ Object
325 326 327 |
# File 'lib/sevennet/api.rb', line 325 def to_s elem.to_s if elem end |