Class: SalesforceCertificationCalculator::FileReader
- Inherits:
-
Object
- Object
- SalesforceCertificationCalculator::FileReader
- Defined in:
- lib/salesforce_certification_calculator/file_reader.rb
Constant Summary collapse
Instance Method Summary collapse
-
#extract_initial_exam_data(exam) ⇒ Exam
Gets all details about an exam’s sections, including names and weights.
-
#generate_exams_list ⇒ Array
Gets list of all exams in the database, including their names and file paths.
-
#initialize ⇒ FileReader
constructor
Creates FileReader object by establishing a base path.
Constructor Details
#initialize ⇒ FileReader
Creates FileReader object by establishing a base path
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/salesforce_certification_calculator/file_reader.rb', line 8 def initialize sfc_gems = [] # Get full path to where Ruby gems are stored on user's computer, starting at the base gems_path = ENV["HOME"] + "/gems/gems" all_gems = Dir.children(gems_path) # Create list of all versions of module installed on user's computer all_gems.each do |gem| if gem.include? "salesforce_certification_calculator" sfc_gems.push(gem) end end # Determine most recent version of module available sfc_gems.sort latest_sfc = sfc_gems[sfc_gems.length - 1] # Set path variable for use by the class's methods @base_path = gems_path + "/" + latest_sfc end |
Instance Method Details
#extract_initial_exam_data(exam) ⇒ Exam
Gets all details about an exam’s sections, including names and weights
67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/salesforce_certification_calculator/file_reader.rb', line 67 def extract_initial_exam_data(exam) file_path = @base_path + "/" + exam.file doc = File.open(file_path) { |f| Nokogiri::XML(f) } names = doc.xpath("//name") weights = doc.xpath("//weight") # Add all sections' names and weights to existing exam data (0..names.length-1).each do |i| exam.add_section(names[i].content, weights[i].content.to_i) end return exam end |
#generate_exams_list ⇒ Array
Gets list of all exams in the database, including their names and file paths
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/salesforce_certification_calculator/file_reader.rb', line 38 def generate_exams_list exams = [] files = Dir.glob("data/*xml", base: @base_path) # Create list of all exams, including their titles and file paths files.each do |file| exam = SFC::Exam.new file_path = @base_path + "/" + file doc = File.open(file_path) { |f| Nokogiri::XML(f) } title = doc.at_xpath("//title") exam.title = title.content exam.file = file exams.push(exam) end return exams end |