-
Notifications
You must be signed in to change notification settings - Fork 23
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
Allow to ignore invalid fields #28
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
module Vcard | ||
class Configuration | ||
|
||
attr_accessor :raise_on_invalid_line | ||
alias_method :raise_on_invalid_line?, :raise_on_invalid_line | ||
|
||
attr_accessor :ignore_invalid_vcards | ||
alias_method :ignore_invalid_vcards?, :ignore_invalid_vcards | ||
|
||
def initialize | ||
@raise_on_invalid = true | ||
@ignore_invalid_vcard = true | ||
end | ||
|
||
private | ||
|
||
|
||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,16 +31,27 @@ class DirectoryInfo | |
# Initialize a DirectoryInfo object from +fields+. If +profile+ is | ||
# specified, check the BEGIN/END fields. | ||
def initialize(fields, profile = nil) #:nodoc: | ||
if fields.detect { |f| ! f.kind_of? DirectoryInfo::Field } | ||
raise ArgumentError, "fields must be an array of DirectoryInfo::Field objects" | ||
@valid = true | ||
@fields = [] | ||
|
||
fields.each do |f| | ||
raise ArgumentError, "fields must be an array of DirectoryInfo::Field objects" unless f.kind_of? DirectoryInfo::Field | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should there be a condition on this line regarding whether or not to raise? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I didn't change behavior in this place. It looks for me like a strict type check and it's not clear for me why it needed here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No worries :) |
||
if f.valid? | ||
@fields << f | ||
else | ||
@valid = false | ||
end | ||
end | ||
|
||
@string = nil # this is used as a flag to indicate that recoding will be necessary | ||
@fields = fields | ||
|
||
check_begin_end(profile) if profile | ||
end | ||
|
||
def valid? | ||
@valid | ||
end | ||
|
||
# Decode +card+ into a DirectoryInfo object. | ||
# | ||
# +card+ may either be a something that is convertible to a string using | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,31 +21,31 @@ def test_encode_decode_text() | |
def test_field4 | ||
line = "t;e=a,b: 4 " | ||
part = Field.decode0(line) | ||
assert_equal("4", part[ 3 ]) | ||
assert_equal("4", part[ 4 ]) | ||
end | ||
|
||
def test_field3 | ||
line = "t;e=a,b:4" | ||
part = Field.decode0(line) | ||
assert_equal("4", part[ 3 ]) | ||
assert_equal( {"E" => [ "a","b" ] }, part[ 2 ]) | ||
assert_equal("4", part[ 4 ]) | ||
assert_equal( {"E" => [ "a","b" ] }, part[ 3 ]) | ||
end | ||
|
||
def test_field2 | ||
line = "tel;type=work,voice,msg:+1 313 747-4454" | ||
part = Field.decode0(line) | ||
assert_equal("+1 313 747-4454", part[ 3 ]) | ||
assert_equal( {"TYPE" => [ "work","voice","msg" ] }, part[ 2 ]) | ||
assert_equal("+1 313 747-4454", part[ 4 ]) | ||
assert_equal( {"TYPE" => [ "work","voice","msg" ] }, part[ 3 ]) | ||
end | ||
|
||
def test_field1 | ||
line = 'ORGANIZER;CN="xxxx, xxxx [SC100:370:EXCH]":MAILTO:[email protected]' | ||
parts = Field.decode0(line) | ||
|
||
assert_equal(nil, parts[0]) | ||
assert_equal("ORGANIZER", parts[1]) | ||
assert_equal({ "CN" => [ "xxxx, xxxx [SC100:370:EXCH]" ] }, parts[2]) | ||
assert_equal("MAILTO:[email protected]", parts[3]) | ||
assert_equal(nil, parts[1]) | ||
assert_equal("ORGANIZER", parts[2]) | ||
assert_equal({ "CN" => [ "xxxx, xxxx [SC100:370:EXCH]" ] }, parts[3]) | ||
assert_equal("MAILTO:[email protected]", parts[4]) | ||
end | ||
|
||
=begin this can not be done :-( | ||
|
@@ -67,20 +67,21 @@ def test_case_equiv | |
|
||
def test_field0 | ||
assert_equal("name:", line = Field.encode0(nil, "name")) | ||
assert_equal([ nil, "NAME", {}, ""], Field.decode0(line)) | ||
assert_equal([ true, nil, "NAME", {}, ""], Field.decode0(line)) | ||
|
||
assert_equal("name:value", line = Field.encode0(nil, "name", {}, "value")) | ||
assert_equal([ nil, "NAME", {}, "value"], Field.decode0(line)) | ||
assert_equal([ true, nil, "NAME", {}, "value"], Field.decode0(line)) | ||
|
||
assert_equal("name;encoding=B:dmFsdWU=", line = Field.encode0(nil, "name", { "encoding"=>:b64 }, "value")) | ||
assert_equal([ nil, "NAME", { "ENCODING"=>["B"]}, ["value"].pack("m").chomp ], Field.decode0(line)) | ||
assert_equal([ true, nil, "NAME", { "ENCODING"=>["B"]}, ["value"].pack("m").chomp ], Field.decode0(line)) | ||
|
||
line = Field.encode0("group", "name", {}, "value") | ||
assert_equal "group.name:value", line | ||
assert_equal [ "GROUP", "NAME", {}, "value"], Field.decode0(line) | ||
assert_equal [ true, "GROUP", "NAME", {}, "value"], Field.decode0(line) | ||
end | ||
|
||
def test_invalid_fields | ||
def test_invalid_fields_wih_raise_error | ||
Vcard::configuration.raise_on_invalid_line = true | ||
[ | ||
"g.:", | ||
":v", | ||
|
@@ -89,6 +90,16 @@ def test_invalid_fields | |
end | ||
end | ||
|
||
def test_invalid_fields_wihout_raise_error | ||
Vcard.configuration.raise_on_invalid_line = false | ||
[ | ||
"g.:", | ||
":v", | ||
].each do |line| | ||
assert_nothing_raised { Field.decode0(line) } | ||
end | ||
end | ||
|
||
def test_date_encode | ||
assert_equal("DTSTART:20040101\n", Field.create("DTSTART", Date.new(2004, 1, 1) ).to_s) | ||
assert_equal("DTSTART:20040101\n", Field.create("DTSTART", [Date.new(2004, 1, 1)]).to_s) | ||
|
@@ -97,6 +108,7 @@ def test_date_encode | |
def test_field_modify | ||
f = Field.create("name") | ||
|
||
|
||
assert_equal("", f.value) | ||
f.value = "" | ||
assert_equal("", f.value) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -169,12 +169,50 @@ def _test_cons # FIXME | |
end | ||
|
||
def test_bad | ||
# FIXME: this should THROW, it's badly encoded! | ||
Vcard::configuration.raise_on_invalid_line = true | ||
assert_raises(::Vcard::InvalidEncodingError) do | ||
Vcard::Vcard.decode("BEGIN:VCARD\nVERSION:3.0\nKEYencoding=b:this could be \nmy certificate\n\nEND:VCARD\n") | ||
end | ||
end | ||
|
||
def test_not_raise_error_if_configured_to_ignore | ||
Vcard::configuration.raise_on_invalid_line = false | ||
Vcard::configuration.ignore_invalid_vcards = false | ||
assert_nothing_raised do | ||
Vcard::Vcard.decode("BEGIN:VCARD\nVERSION:3.0\nKEYencoding=b:this could be \nmy certificate\n\nEND:VCARD\n") | ||
end | ||
end | ||
|
||
def test_ignore_vcards_with_invalid_fields | ||
Vcard::configuration.raise_on_invalid_line = false | ||
Vcard::configuration.ignore_invalid_vcards = true | ||
src = <<'EOF' | ||
BEGIN:VCARD | ||
VERSION:3.0 | ||
KEYencoding=b:this could be | ||
my certificate | ||
EMAIL:[email protected] | ||
END:VCARD | ||
BEGIN:VCARD | ||
VERSION:3.0 | ||
EMAIL:[email protected] | ||
END:VCARD | ||
EOF | ||
|
||
cards = Vcard::Vcard.decode(src) | ||
assert_equal 1, cards.size | ||
end | ||
|
||
def test_ignore_only_invalid_fields | ||
Vcard::configuration.raise_on_invalid_line = false | ||
Vcard::configuration.ignore_invalid_vcards = false | ||
email = '[email protected]' | ||
cards = Vcard::Vcard.decode("BEGIN:VCARD\nVERSION:3.0\nKEYencoding=b:this could be \nmy certificate\nEMAIL:#{email}\n\nEND:VCARD\n") | ||
assert_equal email, cards.first.email | ||
# [BEGIN, VERSION, EMAIL, END].size == 4 | ||
assert_equal 4, cards.first.fields.size | ||
end | ||
|
||
def test_create | ||
card = Vcard::Vcard.create | ||
key = Vcard::DirectoryInfo.decode("key;type=x509;encoding=B:dGhpcyBjb3VsZCBiZSAKbXkgY2VydGlmaWNhdGUK\n")['key'] | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you remove this given there are no private methods?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done