class Alexandria::Amazon::Element

Internal wrapper class to provide convenient method to access Hpricot element value.

Public Class Methods

get(element, path = "") click to toggle source

Similar to get, except an element object must be passed-in.

# File lib/alexandria/book_providers/amazon_ecs_util.rb, line 323
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 = "") click to toggle source

Similar to get_array, except an element object must be passed-in.

# File lib/alexandria/book_providers/amazon_ecs_util.rb, line 340
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 do |item|
      parsed_result << Element.get(item)
    end
    parsed_result
  else
    [Element.get(result)]
  end
end
get_hash(element, path = "") click to toggle source

Similar to get_hash, except an element object must be passed-in.

# File lib/alexandria/book_providers/amazon_ecs_util.rb, line 356
def self.get_hash(element, path = "")
  result = element&.at(path)
  return unless result

  hash = {}
  result = result.children
  result.each do |item|
    hash[item.name.to_sym] = item.inner_html
  end
  hash
end
get_unescaped(element, path = "") click to toggle source

Similar to get_unescaped, except an element object must be passed-in.

# File lib/alexandria/book_providers/amazon_ecs_util.rb, line 334
def self.get_unescaped(element, path = "")
  result = get(element, path)
  CGI.unescapeHTML(result) if result
end
new(element) click to toggle source

Pass Hpricot::Elements object

# File lib/alexandria/book_providers/amazon_ecs_util.rb, line 271
def initialize(element)
  @element = element
end

Public Instance Methods

/(path) click to toggle source

Find Hpricot::Elements matching the given path. Example: element/“author”.

# File lib/alexandria/book_providers/amazon_ecs_util.rb, line 281
def /(path)
  elements = @element / path
  return nil if elements.empty?

  elements
end
elem() click to toggle source

Returns Hpricot::Elments object

# File lib/alexandria/book_providers/amazon_ecs_util.rb, line 276
def elem
  @element
end
get(path = "") click to toggle source

Get the text value of the given path, leave empty to retrieve current element value.

# File lib/alexandria/book_providers/amazon_ecs_util.rb, line 302
def get(path = "")
  Element.get(@element, path)
end
get_array(path = "") click to toggle source

Get the array values of the given path.

# File lib/alexandria/book_providers/amazon_ecs_util.rb, line 312
def get_array(path = "")
  Element.get_array(@element, path)
end
get_hash(path = "") click to toggle source

Get the children element text values in hash format with the element names as the hash keys.

# File lib/alexandria/book_providers/amazon_ecs_util.rb, line 318
def get_hash(path = "")
  Element.get_hash(@element, path)
end
get_unescaped(path = "") click to toggle source

Get the unescaped HTML text of the given path.

# File lib/alexandria/book_providers/amazon_ecs_util.rb, line 307
def get_unescaped(path = "")
  Element.get_unescaped(@element, path)
end
search_and_convert(path) click to toggle source

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.

# File lib/alexandria/book_providers/amazon_ecs_util.rb, line 291
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() click to toggle source
# File lib/alexandria/book_providers/amazon_ecs_util.rb, line 368
def to_s
  elem&.to_s
end