lib/vpim/maker/vcard.rb in vpim-0.17 vs lib/vpim/maker/vcard.rb in vpim-0.323
- old
+ new
@@ -14,10 +14,11 @@
#
# Examples:
# - link:ex_mkvcard.txt: example of creating a vCard
# - link:ex_cpvcard.txt: example of copying and them modifying a vCard
# - link:ex_mkv21vcard.txt: example of creating version 2.1 vCard
+ # - link:ex_mkyourown.txt: example of adding support for new fields to Maker::Vcard
class Vcard
# Make a vCard.
#
# Yields +maker+, a Vpim::Maker::Vcard which allows fields to be added to
# +card+, and returns +card+, a Vpim::Vcard.
@@ -78,20 +79,17 @@
# - prefix: such as "Ms." or "Dr."
# - suffix: such as "BFA", or "Sensei"
#
# All attributes are optional.
#
- # Warning: This is the only mandatory field besides the full name, FN,
- # but FN: can be set in #make, and if not set will be constucted as the
- # string "#{prefix} #{given} #{additional} #{family}, #{suffix}".
- #
- # FIXME: is it possible to deduce given/family from the full_name?
- #
- # FIXME: Each attribute can currently only have a single String value.
- #
- # FIXME: Need to escape specials in the String.
+ # Warning: This is the only mandatory field besides the full name, FN.
+ # FN: can be set in #make, or by #fullname=, and if not set will be
+ # constucted as the string "#{prefix} #{given} #{additional} #{family},
+ # #{suffix}".
def add_name # :yield: n
+ # FIXME: Each attribute can currently only have a single String value.
+ # FIXME: Need to escape specials in the String.
x = Struct.new(:family, :given, :additional, :prefix, :suffix).new
yield x
@card << Vpim::DirectoryInfo::Field.create(
'N',
x.map { |s| s ? s.to_str : '' }
@@ -99,12 +97,12 @@
self
end
# Add a full name field, FN.
#
- # Normally the FN field value is derived from the N: field value, but
- # it can be explicitly set.
+ # Normally the FN field value is derived from the N: field value, see
+ # #add_name, but it can be explicitly set.
def fullname=(fullname)
if @card.field('FN')
raise Vpim::InvalidEncodingError, "Not allowed to add more than one FN field to a vCard."
end
@card << Vpim::DirectoryInfo::Field.create( 'FN', fullname );
@@ -128,29 +126,28 @@
#
# All attributes are optional. #location and #home can be set to arrays of
# strings.
#
# TODO - Add #label to support LABEL.
- #
- # FIXME - Need to escape specials in the String.
def add_addr # :yield: adr
+ # FIXME - Need to escape specials in the String.
x = Struct.new(
:location, :preferred, :delivery,
:pobox, :extended, :street, :locality, :region, :postalcode, :country
).new
yield x
values = x.to_a[3, 7].map { |s| s ? s.to_str : '' }
# All these attributes go into the TYPE parameter.
params = [ x[:location], x[:delivery] ]
- params << 'pref' if x[:preferred]
+ params << 'PREF' if x[:preferred]
params = params.flatten.uniq.compact.map { |s| s.to_str }
paramshash = {}
- paramshash['type'] = params if params.first
+ paramshash['TYPE'] = params if params.first
@card << Vpim::DirectoryInfo::Field.create( 'ADR', values, paramshash)
self
end
@@ -170,15 +167,15 @@
if block_given?
x = Struct.new( :location, :preferred, :capability ).new
yield x
- x[:preferred] = 'pref' if x[:preferred]
+ x[:preferred] = 'PREF' if x[:preferred]
types = x.to_a.flatten.uniq.compact.map { |s| s.to_str }
- params['type'] = types if types.first
+ params['TYPE'] = types if types.first
end
@card << Vpim::DirectoryInfo::Field.create( 'TEL', number, params)
self
end
@@ -196,15 +193,15 @@
if block_given?
x = Struct.new( :location, :preferred, :protocol ).new
yield x
- x[:preferred] = 'pref' if x[:preferred]
+ x[:preferred] = 'PREF' if x[:preferred]
types = x.to_a.flatten.uniq.compact.map { |s| s.to_str }
- params['type'] = types if types.first
+ params['TYPE'] = types if types.first
end
@card << Vpim::DirectoryInfo::Field.create( 'EMAIL', email, params)
self
end
@@ -224,10 +221,11 @@
if !birthday.respond_to? :month
raise Vpim::InvalidEncodingError, 'birthday doesn\'t have #month, so it is not a date or time object.'
end
@card << Vpim::DirectoryInfo::Field.create( 'BDAY', birthday );
end
+
=begin
TODO - need text=() implemented in Field
# Add a note field, NOTE. It can contain newlines, they will be escaped.
def note=(note)
@@ -267,15 +265,15 @@
if block_given?
x = Struct.new( :location, :preferred, :purpose ).new
yield x
- x[:preferred] = 'pref' if x[:preferred]
+ x[:preferred] = 'PREF' if x[:preferred]
- types = x.to_a.flatten.uniq.compact.map { |s| s.to_str }
+ types = x.to_a.flatten.uniq.compact.map { |s| s.upcase }
- params['type'] = types if types.first
+ params['TYPE'] = types if types.first
end
@card << Vpim::DirectoryInfo::Field.create( 'IMPP', url, params)
self
end
@@ -297,15 +295,15 @@
if block_given?
x = Struct.new( :location, :preferred ).new
yield x
- x[:preferred] = 'pref' if x[:preferred]
+ x[:preferred] = 'PREF' if x[:preferred]
- types = x.to_a.flatten.uniq.compact.map { |s| s.to_str }
+ types = x.to_a.flatten.uniq.compact.map { |s| s.upcase }
- params['type'] = types if types.first
+ params['TYPE'] = types if types.first
end
@card << Vpim::DirectoryInfo::Field.create( 'X-AIM', xaim, params)
self
end
@@ -344,22 +342,27 @@
end
params = {}
# Don't set type to the empty string.
- params['type'] = x[:type] if( x[:type] && x[:type].length > 0 )
+ params['TYPE'] = x[:type] if( x[:type] && x[:type].length > 0 )
if x[:link]
- params['value'] = 'uri'
+ params['VALUE'] = 'URI'
else # it's inline, base-64 encode it
- params['encoding'] = :b64
+ params['ENCODING'] = :b64
if !x[:type]
raise Vpim::InvalidEncodingError, 'Inline image data must have it\'s type set.'
end
end
@card << Vpim::DirectoryInfo::Field.create( 'PHOTO', value, params )
self
+ end
+
+ # Add a URL field, URL:.
+ def add_url(url)
+ @card << Vpim::DirectoryInfo::Field.create( 'URL', url.to_str );
end
# Add a Field, +field+.
def add_field(field)
fieldname = field.name.upcase