Class: SalesforceCertificationCalculator::FileReader

Inherits:
Object
  • Object
show all
Defined in:
lib/salesforce_certification_calculator/file_reader.rb

Constant Summary collapse

SFC =
SalesforceCertificationCalculator

Instance Method Summary collapse

Constructor Details

#initializeFileReader

Creates FileReader object by establishing a base path

Examples:

Create FileReader Object

>> fr = FileReader.new


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

Examples:

Retrieve Exam Data

>> reader = FileReader.new
>> exams = FileReader.generate_exams_list
>> exam = reader.extract_exam_data(exams[0])
=> <SalesforceCertificationCalculator::Exam 0x000987>

Parameters:

  • exam (Exam)

    object, containing name and file properties, to use to get full data

Returns:

  • (Exam)

    object with not only name and file, but also all sections, with their 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_listArray

Gets list of all exams in the database, including their names and file paths

Examples:

Retrieve Exams

>> reader = FileReader.new
>> exams = reader.generate_exams_list
=> [<SalesforceCertificationCalculator::Exam 0x000987>, <SalesforceCertificationCalculator::Exam 0x000988>]

Returns:

  • (Array)

    collection of Exam objects



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