Class: SimpleVCF
- Inherits:
-
Object
- Object
- SimpleVCF
- Defined in:
- lib/simplevcf.rb
Overview
A simple class for importing and parsing a .vcf contact file. Currently works only on single vCard per file.
Instance Method Summary collapse
-
#import(vcard) ⇒ Object
To import, pass the contents of the file as string.
-
#output ⇒ Object
The output returns the contact information in form of a hash.
-
#parse ⇒ Object
The parser strips the beginning and ending sections of a vCard, iterates to create an array of lines and creates # a hash containing fields with respective values.
Instance Method Details
#import(vcard) ⇒ Object
To import, pass the contents of the file as string
6 7 8 |
# File 'lib/simplevcf.rb', line 6 def import(vcard) @file_contents = vcard end |
#output ⇒ Object
The output returns the contact information in form of a hash.
33 34 35 |
# File 'lib/simplevcf.rb', line 33 def output return @fields end |
#parse ⇒ Object
The parser strips the beginning and ending sections of a vCard, iterates to create an array of lines and creates # a hash containing fields with respective values.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/simplevcf.rb', line 12 def parse @fields = Hash.new() @file_contents["BEGIN:VCARD"] = "" @file_contents["END:VCARD"] = "" @lines = @file_contents.split("\n") for i in 0..(@lines.length-1) do if @lines[i].include? "\r" @lines[i]["\r"] = "" end x = @lines[i] unless x == "" or x.nil? y = x.split(":") field_name = y[0] field_value = y[1] @fields[field_name] = field_value end end end |