Class: Amazon::Element
- Inherits:
-
Object
- Object
- Amazon::Element
- Defined in:
- lib/amazon/ecs.rb
Overview
Internal wrapper class to provide convenient method to access Hpricot element value.
Class Method Summary collapse
-
.get(element, path = '') ⇒ Object
Similar to #get, except an element object must be passed-in.
-
.get_array(element, path = '') ⇒ Object
Similar to #get_array, except an element object must be passed-in.
-
.get_hash(element, path = '') ⇒ Object
Similar to #get_hash, except an element object must be passed-in.
-
.get_unescaped(element, path = '') ⇒ Object
Similar to #get_unescaped, except an element object must be passed-in.
Instance Method Summary collapse
-
#/(path) ⇒ Object
Find Hpricot::Elements matching the given path.
- #attributes ⇒ Object
-
#elem ⇒ Object
Returns Hpricot::Elments 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 Amazon::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 Hpricot::Elements object.
-
#search_and_convert(path) ⇒ Object
Return an array of Amazon::Element matching the given path, or Amazon::Element if there is only one element found.
- #to_s ⇒ Object
Constructor Details
#initialize(element) ⇒ Element
Pass Hpricot::Elements object
269 270 271 |
# File 'lib/amazon/ecs.rb', line 269 def initialize(element) @element = element end |
Class Method Details
.get(element, path = '') ⇒ Object
Similar to #get, except an element object must be passed-in.
334 335 336 337 338 339 |
# File 'lib/amazon/ecs.rb', line 334 def self.get(element, path='') return unless element result = element.at(path) result = result.inner_html if result result end |
.get_array(element, path = '') ⇒ Object
Similar to #get_array, except an element object must be passed-in.
348 349 350 351 352 353 354 355 356 357 358 359 360 361 |
# File 'lib/amazon/ecs.rb', line 348 def self.get_array(element, path='') return unless element result = element/path if (result.is_a? Hpricot::Elements) || (result.is_a? Array) parsed_result = [] result.each {|item| parsed_result << Element.get(item) } parsed_result else [Element.get(result)] end end |
.get_hash(element, path = '') ⇒ Object
Similar to #get_hash, except an element object must be passed-in.
364 365 366 367 368 369 370 371 372 373 374 375 376 |
# File 'lib/amazon/ecs.rb', line 364 def self.get_hash(element, path='') return unless element result = element.at(path) if result hash = {} result = result.children result.each do |item| hash[item.name.to_sym] = item.inner_html end hash end end |
.get_unescaped(element, path = '') ⇒ Object
Similar to #get_unescaped, except an element object must be passed-in.
342 343 344 345 |
# File 'lib/amazon/ecs.rb', line 342 def self.get_unescaped(element, path='') result = get(element, path) CGI::unescapeHTML(result) if result end |
Instance Method Details
#/(path) ⇒ Object
Find Hpricot::Elements matching the given path. Example: element/“author”.
279 280 281 282 283 |
# File 'lib/amazon/ecs.rb', line 279 def /(path) elements = @element/path return nil if elements.size == 0 elements end |
#attributes ⇒ Object
328 329 330 331 |
# File 'lib/amazon/ecs.rb', line 328 def attributes return unless self.elem self.elem.attributes end |
#elem ⇒ Object
Returns Hpricot::Elments object
274 275 276 |
# File 'lib/amazon/ecs.rb', line 274 def elem @element end |
#get(path = '') ⇒ Object
Get the text value of the given path, leave empty to retrieve current element value.
309 310 311 |
# File 'lib/amazon/ecs.rb', line 309 def get(path='') Element.get(@element, path) end |
#get_array(path = '') ⇒ Object
Get the array values of the given path.
319 320 321 |
# File 'lib/amazon/ecs.rb', line 319 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
303 304 305 306 |
# File 'lib/amazon/ecs.rb', line 303 def get_element(path) elements = get_elements(path) elements[0] if elements end |
#get_elements(path) ⇒ Object
Return an array of Amazon::Element matching the given path
296 297 298 299 300 |
# File 'lib/amazon/ecs.rb', line 296 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.
324 325 326 |
# File 'lib/amazon/ecs.rb', line 324 def get_hash(path='') Element.get_hash(@element, path) end |
#get_unescaped(path = '') ⇒ Object
Get the unescaped HTML text of the given path.
314 315 316 |
# File 'lib/amazon/ecs.rb', line 314 def get_unescaped(path='') Element.get_unescaped(@element, path) end |
#search_and_convert(path) ⇒ Object
Return an array of Amazon::Element matching the given path, or Amazon::Element if there is only one element found.
DEPRECATED: Please use get_elements
and get_element
instead.
289 290 291 292 293 |
# File 'lib/amazon/ecs.rb', line 289 def search_and_convert(path) elements = self.get_elements(path) return elements.first if elements and elements.size == 1 elements end |
#to_s ⇒ Object
378 379 380 |
# File 'lib/amazon/ecs.rb', line 378 def to_s elem.to_s if elem end |