class AmsLayout::Parser

Public Instance Methods

layout() click to toggle source
# File lib/ams_layout/parser.rb, line 16
def layout
  @layout ||= {}
end
parse(html) click to toggle source
# File lib/ams_layout/parser.rb, line 20
def parse html
  # Table: ctl00_ContentPlaceHolder1_tblMain
  Nokogiri::HTML(html).css("table#ctl00_ContentPlaceHolder1_tblMain").each do |tbl|
    tbl.css('tr').each do |row|
      parse_row row
    end
  end

  layout
end

Private Instance Methods

add_control(control) click to toggle source
# File lib/ams_layout/parser.rb, line 63
def add_control control
  current_section << control.to_hash unless control.is_a?(NullControl)
end
add_section(section_name) click to toggle source
# File lib/ams_layout/parser.rb, line 49
def add_section section_name
  @current_section = section_name
end
assert_control_td(cell) click to toggle source
# File lib/ams_layout/parser.rb, line 99
def assert_control_td cell
  fail "TD does not contain class attribute of 'ControlTD'" unless cell['class'] == 'ControlTD'
end
assert_control_type(type) click to toggle source
# File lib/ams_layout/parser.rb, line 103
def assert_control_type type
  fail 'nil control type' if type.nil?
  fail "unexpected control type: #{type}" unless %w[text textarea select checkbox].include?(type)
end
assert_label_td(cell) click to toggle source
# File lib/ams_layout/parser.rb, line 94
def assert_label_td cell
  label_classes = %w[LabelTD Mandatory]
  fail "TD does not contain class attribute of 'LabelTD'" unless label_classes.include?(cell['class'])
end
build_control(label_cell, control_cell) click to toggle source
# File lib/ams_layout/parser.rb, line 67
def build_control label_cell, control_cell
  assert_label_td label_cell
  assert_control_td control_cell

  label = label_cell.text.gsub("\u00A0", ' ').strip
  # Return if this is a blank cell
  return NullControl.new if label.empty?

  # Actual control is nested INPUT field
  ctrl = find_input_element(control_cell)
  if ctrl.nil?
    fail "Unable to determine input element.\n#{control_cell.inspect}"
  end
  id = ctrl['id']
  type = ctrl['type']
  type = ctrl.name if type.nil?
  assert_control_type type
  Control.new label, id, type
end
current_section() click to toggle source
# File lib/ams_layout/parser.rb, line 53
def current_section
  fail "@current_section not set" if @current_section.nil?

  if layout[@current_section].nil?
    layout[@current_section] = Array.new
  end

  layout[@current_section]
end
find_input_element(cell) click to toggle source
# File lib/ams_layout/parser.rb, line 87
def find_input_element cell
  ctrl = cell.css('input').first
  ctrl = cell.css('select').first if ctrl.nil?
  ctrl = cell.css('textarea').first if ctrl.nil?
  ctrl
end
parse_row(row) click to toggle source
# File lib/ams_layout/parser.rb, line 32
def parse_row row
  cells = row.css('td')

  if cells[0]['class'] == 'sectionheader'
    add_section cells[0].text
    return
  end

  if cells.size >= 2
    add_control build_control(cells[0], cells[1])
  end

  if cells.size >= 4
    add_control build_control(cells[2], cells[3])
  end
end