lib/vcard/field.rb in vcard-0.2.1 vs lib/vcard/field.rb in vcard-0.2.2

- old
+ new

@@ -98,11 +98,11 @@ line end # Decode a field. def Field.decode0(atline) # :nodoc: - unless atline =~ %r{#{Bnf::LINE}}i + if !(atline =~ Bnf::LINE) raise ::Vcard::InvalidEncodingError, atline end atgroup = $1.upcase atname = $2.upcase @@ -124,11 +124,11 @@ # Collect the params, if any. if paramslist.size > 1 # v3.0 and v2.1 params - paramslist.scan( %r{#{Bnf::PARAM}}i ) do + paramslist.scan( Bnf::PARAM ) do # param names are case-insensitive, and multi-valued name = $1.upcase params = $3 @@ -154,11 +154,11 @@ # hash values instead of this. unless atparams.key? name atparams[name] = [] end - params.scan( %r{#{Bnf::PVALUE}} ) do + params.scan( Bnf::PVALUE ) do atparams[name] << ($1 || $2) end end end @@ -405,11 +405,11 @@ # unspecified. def kind v = param("VALUE") if v if v.size > 1 - raise InvalidEncodingError, "multi-valued param 'VALUE' (#{values})" + raise ::Vcard::InvalidEncodingError, "multi-valued param 'VALUE' (#{values})" end v = v.first.downcase end v end @@ -425,54 +425,50 @@ # are generating calendars saying Canada Day started in 1753! # That's just wrong! So, what to do? I add a message # saying what the year is that breaks, so they at least know that # its ridiculous! I think I need my own DateTime variant. def to_time - begin - ::Vcard.decode_date_time_list(value).collect do |d| - # We get [ year, month, day, hour, min, sec, usec, tz ] - begin - if(d.pop == "Z") - Time.gm(*d) - else - Time.local(*d) - end - rescue ArgumentError => e - raise ::Vcard::InvalidEncodingError, "Time.gm(#{d.join(', ')}) failed with #{e.message}" - end - end - rescue ::Vcard::InvalidEncodingError - ::Vcard.decode_date_list(value).collect do |d| - # We get [ year, month, day ] - begin + ::Vcard.decode_date_time_list(value).collect do |d| + # We get [ year, month, day, hour, min, sec, usec, tz ] + begin + if(d.pop == "Z") Time.gm(*d) - rescue ArgumentError => e - raise ::Vcard::InvalidEncodingError, "Time.gm(#{d.join(', ')}) failed with #{e.message}" + else + Time.local(*d) end + rescue ArgumentError => e + raise ::Vcard::InvalidEncodingError, "Time.gm(#{d.join(', ')}) failed with #{e.message}" end end + rescue ::Vcard::InvalidEncodingError + ::Vcard.decode_date_list(value).collect do |d| + # We get [ year, month, day ] + begin + Time.gm(*d) + rescue ArgumentError => e + raise ::Vcard::InvalidEncodingError, "Time.gm(#{d.join(', ')}) failed with #{e.message}" + end + end end # The value as an array of Date objects (all times and dates in # RFC2425 are lists, even where it might not make sense, such as a # birthday). # # The field value may be a list of either DATE or DATE-TIME values, # decoding is tried first as a DATE-TIME, then as a DATE, if neither # works an InvalidEncodingError will be raised. def to_date - begin - ::Vcard.decode_date_time_list(value).collect do |d| - # We get [ year, month, day, hour, min, sec, usec, tz ] - Date.new(d[0], d[1], d[2]) - end - rescue ::Vcard::InvalidEncodingError - ::Vcard.decode_date_list(value).collect do |d| - # We get [ year, month, day ] - Date.new(*d) - end + ::Vcard.decode_date_time_list(value).collect do |d| + # We get [ year, month, day, hour, min, sec, usec, tz ] + Date.new(d[0], d[1], d[2]) end + rescue ::Vcard::InvalidEncodingError + ::Vcard.decode_date_list(value).collect do |d| + # We get [ year, month, day ] + Date.new(*d) + end end # The value as text. Text can have escaped newlines, commas, and escape # characters, this method will strip them, if present. # @@ -589,17 +585,14 @@ # FIXME - should change this so it doesn't assign to @line here, so @line # is used to preserve original encoding. That way, #encode can only wrap # new fields, not old fields. def mutate(g, n, p, v) #:nodoc: line = Field.encode0(g, n, p, v) - - begin - @group, @name, @params, @value = Field.decode0(line) - @line = line - rescue ::Vcard::InvalidEncodingError => e - raise ArgumentError, e.to_s - end + @group, @name, @params, @value = Field.decode0(line) + @line = line self + rescue ::Vcard::InvalidEncodingError => e + raise ArgumentError, e.to_s end private :mutate end end