lib/vcard.rb in vcard-0.2.1 vs lib/vcard.rb in vcard-0.2.2
- old
+ new
@@ -52,11 +52,11 @@
list
end
# Convert a RFC 2425 date into an array of [year, month, day].
def self.decode_date(v) # :nodoc:
- unless v =~ %r{^\s*#{Bnf::DATE}\s*$}
+ if !(v =~ Bnf::DATE)
raise ::Vcard::InvalidEncodingError, "date not valid (#{v})"
end
[$1.to_i, $2.to_i, $3.to_i]
end
@@ -83,36 +83,34 @@
"%0.4d%0.2d%0.2dT%0.2d%0.2d%0.2d" % [ d.year, d.mon, d.day, d.hour, d.min, d.sec ]
end
# Convert a RFC 2425 time into an array of [hour,min,sec,secfrac,timezone]
def self.decode_time(v) # :nodoc:
- unless match = %r{^\s*#{Bnf::TIME}\s*$}.match(v)
+ if !(match = Bnf::TIME.match(v))
raise ::Vcard::InvalidEncodingError, "time '#{v}' not valid"
end
hour, min, sec, secfrac, tz = match.to_a[1..5]
[hour.to_i, min.to_i, sec.to_i, secfrac ? secfrac.to_f : 0, tz]
end
def self.array_datetime_to_time(dtarray) #:nodoc:
- # We get [ year, month, day, hour, min, sec, usec, tz ]
- begin
- tz = (dtarray.pop == "Z") ? :gm : :local
- Time.send(tz, *dtarray)
- rescue ArgumentError => e
- raise ::Vcard::InvalidEncodingError, "#{tz} #{e} (#{dtarray.join(', ')})"
- end
+ # We get [ year, month, day, hour, min, sec, usec, tz ]
+ tz = (dtarray.pop == "Z") ? :gm : :local
+ Time.send(tz, *dtarray)
+ rescue ArgumentError => e
+ raise ::Vcard::InvalidEncodingError, "#{tz} #{e} (#{dtarray.join(', ')})"
end
# Convert a RFC 2425 time into an array of Time objects.
def self.decode_time_to_time(v) # :nodoc:
array_datetime_to_time(decode_date_time(v))
end
# Convert a RFC 2425 date-time into an array of [year,mon,day,hour,min,sec,secfrac,timezone]
def self.decode_date_time(v) # :nodoc:
- unless match = %r{^\s*#{Bnf::DATE}T#{Bnf::TIME}\s*$}.match(v)
+ if !(match = Bnf::DATE_TIME.match(v))
raise ::Vcard::InvalidEncodingError, "date-time '#{v}' not valid"
end
year, month, day, hour, min, sec, secfrac, tz = match.to_a[1..8]
[
@@ -135,11 +133,11 @@
#
# float_list
# Convert an RFC2425 INTEGER value into an Integer
def self.decode_integer(v) # :nodoc:
- unless %r{\s*#{Bnf::INTEGER}\s*}.match(v)
+ if !(v =~ Bnf::INTEGER)
raise ::Vcard::InvalidEncodingError, "integer not valid (#{v})"
end
v.to_i
end
@@ -225,28 +223,25 @@
# param-value = paramtext / quoted-string
# paramtext = *SAFE-CHAR
# quoted-string = DQUOTE *QSAFE-CHAR DQUOTE
def self.encode_paramtext(value)
- case value
- when %r{\A#{Bnf::SAFECHAR}*\z}
+ if value =~ Bnf::ALL_SAFECHARS
value
else
raise ::Vcard::Unencodable, "paramtext #{value.inspect}"
end
end
def self.encode_paramvalue(value)
- case value
- when %r{\A#{Bnf::SAFECHAR}*\z}
+ if value =~ Bnf::ALL_SAFECHARS
value
- when %r{\A#{Bnf::QSAFECHAR}*\z}
+ elsif value =~ Bnf::ALL_QSAFECHARS
'"' + value + '"'
else
raise ::Vcard::Unencodable, "param-value #{value.inspect}"
end
end
-
# Unfold the lines in +card+, then return an array of one Field object per
# line.
def self.decode(card) #:nodoc:
unfold(card).collect { |line| DirectoryInfo::Field.decode(line) }