Class: Amazon::Element

Inherits:
Object
  • Object
show all
Defined in:
lib/amazon/ecs.rb

Overview

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(element) ⇒ Element

Pass Nokogiri::XML::Element object



315
316
317
# File 'lib/amazon/ecs.rb', line 315

def initialize(element)
  @element = element
end

Class Method Details

.get(element, path = '.') ⇒ Object

Return the text value of an element.



273
274
275
276
277
278
# File 'lib/amazon/ecs.rb', line 273

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.



287
288
289
290
291
292
293
294
295
296
# File 'lib/amazon/ecs.rb', line 287

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.



299
300
301
302
303
304
305
306
307
308
309
310
311
# File 'lib/amazon/ecs.rb', line 299

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.



281
282
283
284
# File 'lib/amazon/ecs.rb', line 281

def get_unescaped(element, path='.')
  result = self.get(element, path)
  CGI::unescapeHTML(result) if result
end

Instance Method Details

#/(path) ⇒ Object

Returns a Nokogiri::XML::NodeSet of elements matching the given path. Example: element/“author”.



325
326
327
328
329
# File 'lib/amazon/ecs.rb', line 325

def /(path)
  elements = @element/path
  return nil if elements.size == 0
  elements
end

#attributesObject



364
365
366
367
# File 'lib/amazon/ecs.rb', line 364

def attributes
  return unless self.elem
  self.elem.attributes
end

#elemObject

Returns Nokogiri::XML::Element object



320
321
322
# File 'lib/amazon/ecs.rb', line 320

def elem
  @element
end

#get(path = '.') ⇒ Object

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



345
346
347
# File 'lib/amazon/ecs.rb', line 345

def get(path='.')
  Element.get(@element, path)
end

#get_array(path = '.') ⇒ Object

Get the array values of the given path.



355
356
357
# File 'lib/amazon/ecs.rb', line 355

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



339
340
341
342
# File 'lib/amazon/ecs.rb', line 339

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



332
333
334
335
336
# File 'lib/amazon/ecs.rb', line 332

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.



360
361
362
# File 'lib/amazon/ecs.rb', line 360

def get_hash(path='.')
  Element.get_hash(@element, path)
end

#get_unescaped(path = '.') ⇒ Object

Get the unescaped HTML text of the given path.



350
351
352
# File 'lib/amazon/ecs.rb', line 350

def get_unescaped(path='.')
  Element.get_unescaped(@element, path)
end

#to_sObject



369
370
371
# File 'lib/amazon/ecs.rb', line 369

def to_s
  elem.to_s if elem
end