Class: Amazon::Element
- Inherits:
-
Object
- Object
- Amazon::Element
- Defined in:
- lib/alexandria/book_providers/amazon_ecs_util.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.
-
#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_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
Find Hpricot::Elements matching the given path, and convert to Amazon::Element.
- #to_s ⇒ Object
Constructor Details
#initialize(element) ⇒ Element
Pass Hpricot::Elements object
303 304 305 |
# File 'lib/alexandria/book_providers/amazon_ecs_util.rb', line 303 def initialize(element) @element = element end |
Class Method Details
.get(element, path = '') ⇒ Object
Similar to #get, except an element object must be passed-in.
350 351 352 353 354 355 356 357 |
# File 'lib/alexandria/book_providers/amazon_ecs_util.rb', line 350 def self.get(element, path = '') return unless element result = element.at(path) ## inner_html doesn't decode entities, hence bug #21659 # result = result.inner_html if result result = result.inner_text if result result end |
.get_array(element, path = '') ⇒ Object
Similar to #get_array, except an element object must be passed-in.
366 367 368 369 370 371 372 373 374 375 376 377 378 379 |
# File 'lib/alexandria/book_providers/amazon_ecs_util.rb', line 366 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.
382 383 384 385 386 387 388 389 390 391 392 393 394 |
# File 'lib/alexandria/book_providers/amazon_ecs_util.rb', line 382 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.
360 361 362 363 |
# File 'lib/alexandria/book_providers/amazon_ecs_util.rb', line 360 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”.
313 314 315 316 317 |
# File 'lib/alexandria/book_providers/amazon_ecs_util.rb', line 313 def /(path) elements = @element / path return nil if elements.empty? elements end |
#elem ⇒ Object
Returns Hpricot::Elments object
308 309 310 |
# File 'lib/alexandria/book_providers/amazon_ecs_util.rb', line 308 def elem @element end |
#get(path = '') ⇒ Object
Get the text value of the given path, leave empty to retrieve current element value.
330 331 332 |
# File 'lib/alexandria/book_providers/amazon_ecs_util.rb', line 330 def get(path = '') Element.get(@element, path) end |
#get_array(path = '') ⇒ Object
Get the array values of the given path.
340 341 342 |
# File 'lib/alexandria/book_providers/amazon_ecs_util.rb', line 340 def get_array(path = '') Element.get_array(@element, path) end |
#get_hash(path = '') ⇒ Object
Get the children element text values in hash format with the element names as the hash keys.
345 346 347 |
# File 'lib/alexandria/book_providers/amazon_ecs_util.rb', line 345 def get_hash(path = '') Element.get_hash(@element, path) end |
#get_unescaped(path = '') ⇒ Object
Get the unescaped HTML text of the given path.
335 336 337 |
# File 'lib/alexandria/book_providers/amazon_ecs_util.rb', line 335 def get_unescaped(path = '') Element.get_unescaped(@element, path) end |
#search_and_convert(path) ⇒ Object
Find Hpricot::Elements matching the given path, and convert to Amazon::Element. Returns an array Amazon::Elements if more than Hpricot::Elements size is greater than 1.
321 322 323 324 325 326 327 |
# File 'lib/alexandria/book_providers/amazon_ecs_util.rb', line 321 def search_and_convert(path) elements = self./(path) return unless elements elements = elements.map { |element| Element.new(element) } return elements.first if elements.size == 1 elements end |
#to_s ⇒ Object
396 397 398 |
# File 'lib/alexandria/book_providers/amazon_ecs_util.rb', line 396 def to_s elem&.to_s end |