Skip to content

Add ability to parse multiple cards - #21

Open
monorkin wants to merge 6 commits into
brewster:masterfrom
monorkin:add-ability-to-parse-multiple-cards
Open

monorkin wants to merge 6 commits into
brewster:masterfrom
monorkin:add-ability-to-parse-multiple-cards

Conversation

@monorkin

@monorkin monorkin commented Apr 20, 2024

Copy link
Copy Markdown

Hi, I've noticed that VCardigan behaves a bit odd if I parse with it a file that contains multiple vCards.
I'd expect it to obey the begin and end lines of each card, but it doesn't and it ends up combining all the cards in the file into one large card with all the fields of all cards.

For instance, the following file:

BEGIN:VCARD
VERSION:4.0
N:Doe;John;;;
FN:John Doe
END:VCARD

BEGIN:VCARD
VERSION:4.0
N:Doe;Jane;;;
FN:Jane Doe
END:VCARD

if given to .parse would product a single vCard that combines John and Jane Doe.

In this PR I've added two things:

  1. a .parse_all! method which returns an enumerator which in term yields all cards one-by-one as it parses them
  2. a .parse! method which is the same as .parse but strict in the sense that it raises an error if it encounters something unexpected while parsing

Calling .parse_all! on the file from before would product 2 vCards:

data = <<-VCARD
BEGIN:VCARD
VERSION:4.0
N:Doe;John;;;
FN:John Doe
END:VCARD

BEGIN:VCARD
VERSION:4.0
N:Doe;Jane;;;
FN:Jane Doe
END:VCARD
VCARD

vcards = VCardigan.parse_all!(data).to_a
vcards.size # => 2

While calling .parse! on an invalid vCard produces an error, where before this would produce a vCard:

data = <<-VCARD
END:VCARD
N:Doe;John;;;
FN:John Doe
BEGIN:VCARD
VERSION:4.0
VCARD

vcard = VCardigan.parse!(data) # => VCardigan::MissingBeginError

The old behavior of the parse method was kept in case someone depends on it.
There are cases when that behavior is useful.

All new parser errors inherit from the old EncodingError for backwards compatibility.

In the process I've also upgraded RSpec to version 3 so that it works with newer Ruby versions.

The block form of #lines has been deprecated in Ruby 2.4
RSpec 2 fails to run on Ruby 3
The default parser doesn't consider begin, end, version, or fn
fields while parsing. This leads to odd behaviour if you give
it an invalid vCard - such as one without an begin or end line;
without a version or full name - where it parses it as if it
were valid. This problem is most obvious if you give the parser
a file that contains multiple cards, it will merge all the
cards into a single large card.
@monorkin
monorkin force-pushed the add-ability-to-parse-multiple-cards branch from e72658e to 65c41da Compare April 20, 2024 16:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant