Module: Nexpose::DataTable
- Defined in:
- lib/nexpose/data_table.rb
Overview
Data table functions which extract data from the Nexpose UI.
The functions in this file are utility functions for accessing data in the same manner as the Nexpose UI. These functions are not designed for external use, but to aid exposing data through other methods in the gem.
Class Method Summary collapse
-
._dyn_headers(response) ⇒ Object
Parse headers out of a dyntable response.
-
._dyn_record(row) ⇒ Object
Parse records out of the row of a dyntable.
-
._dyn_rows(response) ⇒ Object
Parse rows out of a dyntable into an array of values.
-
._get_dyn_table(console, address, payload = nil) ⇒ Array[Hash]
Helper method to get a Dyntable into a consumable Ruby object.
-
._get_json_table(console, address, parameters = {}, page_size = 500, records = nil, post = true) ⇒ Array[Hash]
Helper method to get the YUI tables into a consumable Ruby object.
Class Method Details
._dyn_headers(response) ⇒ Object
Parse headers out of a dyntable response.
88 89 90 91 92 93 94 |
# File 'lib/nexpose/data_table.rb', line 88 def _dyn_headers(response) headers = [] response.elements.each('DynTable/MetaData/Column') do |header| headers << header.attributes['name'] end headers end |
._dyn_record(row) ⇒ Object
Parse records out of the row of a dyntable.
106 107 108 109 110 111 112 |
# File 'lib/nexpose/data_table.rb', line 106 def _dyn_record(row) record = [] row.elements.each('td') do |value| record << (value.text ? value.text.to_s : '') end record end |
._dyn_rows(response) ⇒ Object
Parse rows out of a dyntable into an array of values.
97 98 99 100 101 102 103 |
# File 'lib/nexpose/data_table.rb', line 97 def _dyn_rows(response) rows = [] response.elements.each('DynTable/Data/tr') do |row| rows << _dyn_record(row) end rows end |
._get_dyn_table(console, address, payload = nil) ⇒ Array[Hash]
Helper method to get a Dyntable into a consumable Ruby object.
Example usage:
DataTable._get_dyn_table(@console, '/data/asset/os/dyntable.xml?tableID=OSSynopsisTable')
74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/nexpose/data_table.rb', line 74 def _get_dyn_table(console, address, payload = nil) if payload response = AJAX.post(console, address, payload) else response = AJAX.get(console, address) end response = REXML::Document.new(response) headers = _dyn_headers(response) rows = _dyn_rows(response) rows.map { |row| Hash[headers.zip(row)] } end |
._get_json_table(console, address, parameters = {}, page_size = 500, records = nil, post = true) ⇒ Array[Hash]
Helper method to get the YUI tables into a consumable Ruby object.
Example usage:
DataTable._get_json_table(@console,
'/data/asset/site',
{ 'sort' => 'assetName',
'table-id' => 'site-assets',
'siteID' => site_id })
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/nexpose/data_table.rb', line 34 def _get_json_table(console, address, parameters = {}, page_size = 500, records = nil, post = true) parameters['dir'] = 'DESC' parameters['startIndex'] = -1 parameters['results'] = -1 if post request = lambda { |p| AJAX.form_post(console, address, p) } else request = lambda { |p| AJAX.get(console, address.dup, AJAX::CONTENT_TYPE::JSON, p) } end response = request.(parameters) data = JSON.parse(response) # Don't attept to grab more records than there are. total = data['totalRecords'] return [] if total == 0 total = records.nil? ? total : [records, total].min rows = [] parameters['results'] = page_size while rows.length < total parameters['startIndex'] = rows.length data = JSON.parse(request.(parameters)) rows.concat data['records'] end rows end |