class Martlet::TranscriptParser

Public Class Methods

new(html) click to toggle source
# File lib/martlet/transcript_parser.rb, line 3
def initialize(html)
  @html = html
end

Public Instance Methods

parse_records() click to toggle source
# File lib/martlet/transcript_parser.rb, line 7
def parse_records
  records = []
  document = Nokogiri::HTML(@html)
  rows = document.search("table[@class='dataentrytable'] tr")
  
  rows.each do |row|
    row_data = row.search('td')
    next unless record_data_row?(row_data) && grade_present?(row_data)
    
    record_data = {
      number:  extract_record_data_for(row_data, :number),
      name:    extract_record_data_for(row_data, :name),
      section: extract_record_data_for(row_data, :section),
      credits: extract_record_data_for(row_data, :credits),
      grade:   extract_record_data_for(row_data, :grade),
      average: extract_record_data_for(row_data, :average)
    }
    
    records << Record.new(record_data)
    
  end
  
  records
end

Private Instance Methods

extract_record_data_for(data, name) click to toggle source
# File lib/martlet/transcript_parser.rb, line 45
def extract_record_data_for(data, name)
  index = index_for(name)
  
  if data && data[index]
    span = data[index].search('span')
    
    if span
      span.text
    else
      ''
    end
  end
end
grade_present?(row_data) click to toggle source
# File lib/martlet/transcript_parser.rb, line 63
def grade_present?(row_data)
  grade = extract_record_data_for(row_data, :grade)
  grade && !grade.empty?
end
index_for(name) click to toggle source
# File lib/martlet/transcript_parser.rb, line 34
def index_for(name)
  case name
  when :number  then 1
  when :section then 2
  when :name    then 3
  when :credits then 4
  when :grade   then 6
  when :average then 10
  end
end
record_data_row?(row_data) click to toggle source
# File lib/martlet/transcript_parser.rb, line 59
def record_data_row?(row_data)
  row_data.length == 11
end