Skip to content

Commit 2324915

Browse files
committed
- Fixed some problems with rescue statements not being specific enough.
- Vcard#birthday may now return a DateTime. - Added Vcard#urls - Fixed a mispelled Uri in Vcard#url
1 parent 1c641f6 commit 2324915

File tree

6 files changed

+86
-12
lines changed

6 files changed

+86
-12
lines changed

CHANGES

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
11

2+
- Fixed some problems with rescue statements not being specific enough.
3+
4+
- Vcard#birthday may now return a DateTime.
5+
6+
- Added Vcard#urls
7+
8+
- Fixed a mispelled Uri in Vcard#url
9+
210
- Global fix of misspellings of "occurrence"
311

412
- KAddressBook compatibility fix - allow / in field names, even though it is

THANKS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Jade Meskill
2+
Ryan King

TODO

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ Read code:
2121
I have the changes locally to make dtstart/... return Date or Time,
2222
depending on what the start is. The change breaks other things. :-(
2323

24-
2524
- can't export if there is a Vtodo?
2625

2726
- EXDATE, etc.

lib/vpim/rfc2425.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ module Bnf #:nodoc:
5252
# time-second = 2 DIGIT
5353
# time-secfrac = "," 1*DIGIT
5454
# time-zone = "Z" / time-numzone
55-
# time-numzome = sign time-hour [":"] time-minute
55+
# time-numzone = sign time-hour [":"] time-minute
5656
TIME = '(\d\d):?(\d\d):?(\d\d)(\.\d+)?(Z|[-+]\d\d:?\d\d)?'
5757

5858
# integer = (["+"] / "-") 1*DIGIT
@@ -112,7 +112,7 @@ def Vpim.decode_date(v) # :nodoc:
112112
[$1.to_i, $2.to_i, $3.to_i]
113113
end
114114

115-
# Convert a RFC 2425 date into an array of Date objects.
115+
# Convert a RFC 2425 date into a Date object.
116116
def self.decode_date_to_date(v)
117117
Date.new(*decode_date(v))
118118
end
@@ -175,6 +175,12 @@ def Vpim.decode_date_time(v) # :nodoc:
175175
]
176176
end
177177

178+
def Vpim.decode_date_time_to_datetime(v) #:nodoc:
179+
year, month, day, hour, min, sec, secfrac, tz = Vpim.decode_date_time(v)
180+
# TODO - DateTime understands timezones, so we could decode tz and use it.
181+
DateTime.civil(year, month, day, hour, min, sec, 0)
182+
end
183+
178184
# Vpim.decode_boolean
179185
#
180186
# float

lib/vpim/vcard.rb

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -446,11 +446,9 @@ def decode_n(field) #:nodoc:
446446
def decode_date_or_datetime(field) #:nodoc:
447447
date = nil
448448
begin
449-
date = Vpim.decode_date(field.value_raw)
450-
date = Date.new(*date)
449+
date = Vpim.decode_date_to_date(field.value_raw)
451450
rescue Vpim::InvalidEncodingError
452-
# FIXME - try and decode as DATE-TIME
453-
raise
451+
date = Vpim.decode_date_time_to_datetime(field.value_raw)
454452
end
455453
Line.new( field.group, field.name, date )
456454
end
@@ -460,7 +458,9 @@ def decode_bday(field) #:nodoc:
460458
return decode_date_or_datetime(field)
461459

462460
rescue Vpim::InvalidEncodingError
463-
if field.value =~ /(\d+)-(\d+)-(\d+)/
461+
# Hack around BDAY dates hat are correct in the month and day, but have
462+
# some kind of garbage in the year.
463+
if field.value =~ /^\s*(\d+)-(\d+)-(\d+)\s*$/
464464
y = $1.to_i
465465
m = $2.to_i
466466
d = $3.to_i
@@ -502,7 +502,7 @@ def decode_structured_text(field) #:nodoc:
502502
end
503503

504504
def decode_uri(field) #:nodoc:
505-
Line.new( field.group, field.name, Uri.new(field.value) )
505+
Line.new( field.group, field.name, Attachment::Uri.new(field.value, nil) )
506506
end
507507

508508
def decode_agent(field) #:nodoc:
@@ -586,7 +586,11 @@ def self.decode(decode, card, field) #:nodoc:
586586

587587
# Return line for a field
588588
def f2l(field) #:nodoc:
589-
Line.decode(@@decode, self, field) rescue nil
589+
begin
590+
Line.decode(@@decode, self, field)
591+
rescue InvalidEncodingError
592+
# Skip invalidly encoded fields.
593+
end
590594
end
591595

592596
# With no block, returns an Array of Line. If +name+ is specified, the
@@ -769,7 +773,10 @@ def value(name, type = nil)
769773
end
770774

771775
if fields.first
772-
line = Line.decode(@@decode, self, fields.first) rescue nil
776+
line = begin
777+
Line.decode(@@decode, self, fields.first)
778+
rescue Vpim::InvalidEncodingError
779+
end
773780

774781
if line
775782
return line.value
@@ -955,11 +962,16 @@ def telephones #:yield:tel
955962

956963
## UID
957964

958-
# The URL value, a Uri. A wrapper around #value('NOTE').
965+
# The URL value, a Attachment::Uri. A wrapper around #value('URL').
959966
def url
960967
value('URL')
961968
end
962969

970+
# The URL values, an Attachment::Uri. A wrapper around #values('URL').
971+
def urls
972+
values('URL')
973+
end
974+
963975
# The VERSION multiplied by 10 as an Integer. For example, a VERSION:2.1
964976
# vCard would have a version of 21, and a VERSION:3.0 vCard would have a
965977
# version of 30.

test/test_vcard.rb

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -755,6 +755,53 @@ def test_slash_in_field_name
755755
assert_equal(card["X-messaging/xmpp-All"], "[email protected]")
756756
end
757757

758+
def test_url_decode
759+
cin=<<'---'
760+
BEGIN:VCARD
761+
URL:www.email.com
762+
URL:www.work.com
763+
END:VCARD
764+
---
765+
card = Vpim::Vcard.decode(cin).first
766+
767+
assert_equal("www.email.com", card.url.uri)
768+
assert_equal("www.email.com", card.url.uri.to_s)
769+
assert_equal("www.email.com", card.urls.first.uri)
770+
assert_equal("www.work.com", card.urls.last.uri)
771+
end
772+
773+
def test_bday_decode
774+
cin=<<'---'
775+
BEGIN:VCARD
776+
BDAY:1970-07-14
777+
END:VCARD
778+
---
779+
card = Vpim::Vcard.decode(cin).first
780+
781+
card.birthday
782+
783+
assert_equal(Date.new(1970, 7, 14), card.birthday)
784+
assert_equal(1, card.values("bday").size)
785+
786+
# Nobody should have multiple bdays, I hope, but its allowed syntactically,
787+
# so test it, along with some variant forms of BDAY
788+
cin=<<'---'
789+
BEGIN:VCARD
790+
BDAY:1970-07-14
791+
BDAY:70-7-14
792+
BDAY:1970-07-15T03:45:12
793+
BDAY:1970-07-15T03:45:12Z
794+
END:VCARD
795+
---
796+
card = Vpim::Vcard.decode(cin).first
797+
798+
assert_equal(Date.new(1970, 7, 14), card.birthday)
799+
assert_equal(4, card.values("bday").size)
800+
assert_equal(Date.new(1970, 7, 14), card.values("bday").first)
801+
assert_equal(Date.new(Time.now.year, 7, 14), card.values("bday")[1])
802+
assert_equal(DateTime.new(1970, 7, 15, 3, 45, 12).to_s, card.values("bday")[2].to_s)
803+
assert_equal(DateTime.new(1970, 7, 15, 3, 45, 12).to_s, card.values("bday").last.to_s)
804+
end
758805

759806
def utf_name_test(c)
760807

0 commit comments

Comments
 (0)