Class: Keybox::Convert::CSV

Inherits:
Object
  • Object
show all
Defined in:
lib/keybox/convert/csv.rb

Overview

Convert to/from a CSV file. When loading a CSV file, it is assumed that there is a header on the csv file that shows which of the fields belongs to the various fields of the entry. At a minimum the header line should have the default fields from an HostAccountEntry which are:

- title
- username
- hostname
- password
- additional_info

These headers can be in any order and CSV will do the right thing. But these headers must exist. A Keybox::ValidationError is thrown if they do not.

Class Method Summary collapse

Class Method Details

.from_file(csv_filename) ⇒ Object

returns an Array of AccountEntry classes or its descendants



42
43
44
45
46
47
# File 'lib/keybox/convert/csv.rb', line 42

def from_file(csv_filename)
    reader = ::CSV.open(csv_filename,"r")
    entries = Keybox::Convert::CSV.from_reader(reader)
    reader.close
    return entries
end

.from_reader(csv_reader) ⇒ Object

pull all the items from the CSV file. There MUST be a header line that says what the different fields are. A HostAccountEntry object is created for each line and the array of those objects is returned



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/keybox/convert/csv.rb', line 54

def from_reader(csv_reader)
    field_indexes = parse_header(csv_reader.shift)
    entries = []
    csv_reader.each do |row|
        entry = Keybox::HostAccountEntry.new
        field_indexes.each_pair do |field,index|
            value = row[index] || ""
            entry.send("#{field}=",value.strip)
        end
        entries << entry
    end
    return entries
end

.parse_header(header) ⇒ Object

parse the header line from the CSV file and make sure that all the required columns are listed.



28
29
30
31
32
33
34
35
36
37
# File 'lib/keybox/convert/csv.rb', line 28

def parse_header(header) 
    field_indexes = {}
    Keybox::HostAccountEntry.default_fields.each do |field|
        field_indexes[field] = header.index(field)
        if field_indexes[field].nil? then
            raise Keybox::ValidationError, "There must be a header on the CSV to import and it must contain the '#{field}' field."
        end
    end
    field_indexes
end

.to_file(records, csv_filename) ⇒ Object

records should be an array of AccountEntry objects



71
72
73
74
75
# File 'lib/keybox/convert/csv.rb', line 71

def to_file(records,csv_filename)
    writer = ::CSV.open(csv_filename,"w")
    Keybox::Convert::CSV.to_writer(records,writer)
    writer.close
end

.to_writer(records, csv_writer) ⇒ Object

write all the fields for each record. We go through all the records (an array of AccountEntry objects), recording all the fields, then using that as the header.



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/keybox/convert/csv.rb', line 82

def to_writer(records,csv_writer)
    field_names = records.collect { |r| r.fields }.flatten.uniq
    csv_writer << field_names
    records.each do |record|
        values = []
        field_names.each do |field|
            values << record.send(field) || ""
        end
        csv_writer << values
    end
end