Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to parse multiple cards #21

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 19 additions & 11 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,32 +1,40 @@
PATH
remote: .
specs:
vcardigan (0.0.8)
vcardigan (0.0.9)

GEM
remote: https://rubygems.org/
specs:
coderay (1.1.0)
diff-lcs (1.1.3)
diff-lcs (1.5.1)
method_source (0.8.2)
pry (0.9.12.6)
coderay (~> 1.0)
method_source (~> 0.8)
slop (~> 3.4)
rspec (2.12.0)
rspec-core (~> 2.12.0)
rspec-expectations (~> 2.12.0)
rspec-mocks (~> 2.12.0)
rspec-core (2.12.2)
rspec-expectations (2.12.1)
diff-lcs (~> 1.1.3)
rspec-mocks (2.12.1)
rspec (3.13.0)
rspec-core (~> 3.13.0)
rspec-expectations (~> 3.13.0)
rspec-mocks (~> 3.13.0)
rspec-core (3.13.0)
rspec-support (~> 3.13.0)
rspec-expectations (3.13.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.13.0)
rspec-mocks (3.13.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.13.0)
rspec-support (3.13.1)
slop (3.4.7)

PLATFORMS
ruby

DEPENDENCIES
pry
rspec (~> 2.0)
rspec (~> 3.0)
vcardigan!

BUNDLED WITH
2.5.7
8 changes: 8 additions & 0 deletions lib/vcardigan.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ def parse(*args)
VCardigan::VCard.new.parse(*args)
end

def parse!(*args)
VCardigan::VCard.new.parse(*args, strict: true)
end

def parse_all!(io, skip_invalid: false, &block)
VCardigan::VCard.parse_all(io, skip_invalid: skip_invalid, &block)
end

end

end
28 changes: 27 additions & 1 deletion lib/vcardigan/errors.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,29 @@
module VCardigan
EncodingError = Class.new(StandardError)
class Error < StandardError
class << self
attr_accessor :default_message
end

def initialize(message = nil)
super(message || self.class.default_message)
end
end

class EncodingError < Error; end

class MissingEndError < EncodingError
self.default_message = "vCards must end with an END:VCARD line"
end

class MissingVersionError < EncodingError
self.default_message = "vCards must include a VERSION field"
end

class MissingFullNameError < EncodingError
self.default_message = "vCards must include an FN field"
end

class UnexpectedBeginError < EncodingError
self.default_message = "vCard has more than one BEGIN:VCARD line"
end
end
68 changes: 60 additions & 8 deletions lib/vcardigan/vcard.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'stringio'

module VCardigan

class VCard
Expand All @@ -6,6 +8,37 @@ class VCard
# it's not terminated
UNTERMINATED_QUOTED_PRINTABLE = /ENCODING=QUOTED-PRINTABLE:.*=$/

class << self
def parse_all(io, skip_invalid: false, &block)
io = StringIO.new(io.to_s) unless io_like?(io)

enumerator = Enumerator.new do |yielder|
loop do
break if io.eof?

begin
vcard = new.parse(io, strict: true)
yielder << vcard if vcard
rescue VCardigan::EncodingError => e
raise e unless skip_invalid
end
end
end

if block_given?
enumerator.each(&block)
else
enumerator
end
end

private

def io_like?(obj)
obj.respond_to?(:each_line) && obj.respond_to?(:eof?)
end
end

attr_accessor :version
attr_accessor :chars

Expand All @@ -24,20 +57,25 @@ def initialize(options = {})
@group = nil
end

def parse(data)
lines = unfold(data)
def parse(data, strict: false)
self.version = nil if strict
lines = unfold(data, strict: strict)
return nil if lines.empty? && strict

# Add the parsed properties to this vCard
lines.each do |line|
if line =~ /^VERSION:(.+)/
@version = $1
self.version = $1
next
end

property = VCardigan::Property.parse(self, line)
add_prop(property)
end

raise VCardigan::MissingVersionError if strict && self.version.nil?
raise VCardigan::MissingFullNameError if strict && [email protected]_key?('fn')

self
end

Expand Down Expand Up @@ -170,27 +208,42 @@ def fullname(*args)
# lines to be inserted for readability - it does this by dropping zero-length
# lines.
# Borrowed from https://github.com/qoobaa/vcard
def unfold(card)
def unfold(card, strict: true)
unfolded = []

passed_begining = 0
passed_ending = 0

prior_line = nil
card.lines do |line|
card.each_line do |line|
line.chomp!
# If it's a continuation line, add it to the last.
# If it's an empty line, drop it from the input.
if line =~ /^[ \t]/
next if strict && passed_begining.zero?
unfolded[-1] << line[1, line.size-1]
elsif line =~ /(^BEGIN:VCARD$)|(^END:VCARD$)/
elsif line =~ /^BEGIN:VCARD$/
passed_begining += 1
raise VCardigan::UnexpectedBeginError if strict && passed_begining > 1
elsif line =~ /^END:VCARD$/
passed_ending += 1
break if strict
elsif prior_line && (prior_line =~ UNTERMINATED_QUOTED_PRINTABLE)
next if strict && passed_begining.zero?
# Strip the trailing = off prior line, then append current line
unfolded[-1] = prior_line[0, prior_line.length-1] + line
elsif line =~ /^$/
else
next if strict && passed_begining.zero?
unfolded << line
end
prior_line = unfolded[-1]
end

if strict && passed_begining.positive? && passed_ending.zero?
raise VCardigan::MissingEndError
end

unfolded
end

Expand Down Expand Up @@ -225,8 +278,7 @@ def add_prop(property)

def validate
unless @fields['fn']
raise VCardigan::EncodingError,
"vCards must include an FN field"
raise VCardigan::MissingFullNameError
end
end

Expand Down
163 changes: 163 additions & 0 deletions spec/examples/vcard_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -357,4 +357,167 @@
end
end
end

describe '#parse!' do
context 'invalid vCard' do
let(:data) { File.read(File.dirname(__FILE__) + '/../helpers/joe.vcf') }
let(:vcard) { VCardigan.parse!(data) }
let(:fields) { vcard.instance_variable_get(:@fields) }

context 'when the fields are out of order' do
let(:data) { File.read(File.dirname(__FILE__) + '/../helpers/scrambeled_joe.vcf') }

it 'ignores all fields before the begin line and after the end line' do
expect(fields).not_to have_key('n')
expect(fields).not_to have_key('email')
expect(fields).to have_key('fn')
expect(vcard.fullname.first.value).to eq('Joe Strummer')
expect(vcard.email).to be_nil
end
end

context 'when the version is missing' do
let(:data) { File.read(File.dirname(__FILE__) + '/../helpers/no_version.vcf') }

it 'raises an error' do
expect { vcard }.to raise_error(VCardigan::MissingVersionError)
end
end

context 'when the end line is missing' do
let(:data) { File.read(File.dirname(__FILE__) + '/../helpers/no_end.vcf') }

it 'raises an error' do
expect { vcard }.to raise_error(VCardigan::MissingEndError)
end
end

context 'when the full name is missing' do
let(:data) { File.read(File.dirname(__FILE__) + '/../helpers/no_fullname.vcf') }

it 'raises an error' do
expect { vcard }.to raise_error(VCardigan::MissingFullNameError)
end
end

context 'when there are multiple begin lines' do
let(:data) { File.read(File.dirname(__FILE__) + '/../helpers/multiple_begin.vcf') }

it 'raises an error' do
expect { vcard }.to raise_error(VCardigan::UnexpectedBeginError)
end
end
end

context 'valid 4.0 vCard' do
let(:data) { File.read(File.dirname(__FILE__) + '/../helpers/joe.vcf') }
let(:vcard) { VCardigan.parse!(data) }
let(:fields) { vcard.instance_variable_get(:@fields) }

it 'should set the version' do
vcard.version.should == '4.0'
end

it 'should only have one version property' do
vcard.to_s.lines.count {|l| l =~ /^VERSION:/ }.should == 1
end

it 'should add the properties to the fields array' do
fields.should have_key('n')
fields.should have_key('fn')
end
end

context 'google 3.0 vCard' do
let(:data) { File.read(File.dirname(__FILE__) + '/../helpers/google.vcf') }
let(:vcard) { VCardigan.parse!(data) }
let(:fields) { vcard.instance_variable_get(:@fields) }

it 'should set the version' do
vcard.version.should == '3.0'
end

it 'should add the properties to the fields array' do
fields.should have_key('n')
fields.should have_key('fn')
fields.should have_key('photo')
fields.should have_key('x-socialprofile')
end
end
end

describe '.parse_all!' do
context 'with a valid 4.0 vCard string' do
it 'should return an enumerator of vCards' do
data = File.read(File.dirname(__FILE__) + '/../helpers/doe_family.vcf')
vcards = VCardigan.parse_all!(data)

expect(vcards).to be_an_instance_of(Enumerator)

vcards = vcards.to_a
expect(vcards.size).to eq(2)
expect(vcards).to all(be_an_instance_of(VCardigan::VCard))

vcards = []
VCardigan.parse_all!(data) do |vcard|
vcards << vcard
end

expect(vcards.size).to eq(2)
expect(vcards).to all(be_an_instance_of(VCardigan::VCard))
end
end

context 'with a valid 4.0 vCard io' do
it 'should return an enumerator of vCards' do
data = File.open(File.dirname(__FILE__) + '/../helpers/doe_family.vcf', 'r')
vcards = VCardigan.parse_all!(data)

expect(vcards).to be_an_instance_of(Enumerator)

vcards = vcards.to_a
expect(vcards.size).to eq(2)
expect(vcards).to all(be_an_instance_of(VCardigan::VCard))

vcards = []
data.rewind
VCardigan.parse_all!(data) do |vcard|
vcards << vcard
end

expect(vcards.size).to eq(2)
expect(vcards).to all(be_an_instance_of(VCardigan::VCard))
end
end

context 'with an invalid vCard' do
it 'parses what it can and then rasies an error' do
data = File.open(File.dirname(__FILE__) + '/../helpers/scrambeled_doe_family.vcf', 'r')

vcards = []

expect do
VCardigan.parse_all!(data) do |vcard|
vcards << vcard
end
end.to raise_error(VCardigan::EncodingError)

expect(vcards.size).to eq(1)
expect(vcards.first).to be_an_instance_of(VCardigan::VCard)
end

it 'parses what it can when passed skip_invalid: true' do
data = File.open(File.dirname(__FILE__) + '/../helpers/scrambeled_doe_family.vcf', 'r')

vcards = []

VCardigan.parse_all!(data, skip_invalid: true) do |vcard|
vcards << vcard
end

expect(vcards.size).to eq(1)
expect(vcards.first).to be_an_instance_of(VCardigan::VCard)
end
end
end
end
Loading