Class: Tilia::VObject::VCardConverter

Inherits:
Object
  • Object
show all
Defined in:
lib/tilia/v_object/v_card_converter.rb

Overview

This utility converts vcards from one version to another.

Instance Method Summary collapse

Instance Method Details

#convert(input, target_version) ⇒ Object

Converts a vCard object to a new version.

targetVersion must be one of:

Document::VCARD21
Document::VCARD30
Document::VCARD40

Currently only 3.0 and 4.0 as input and output versions.

2.1 has some minor support for the input version, it’s incomplete at the moment though.

If input and output version are identical, a clone is returned.

Parameters:



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/tilia/v_object/v_card_converter.rb', line 22

def convert(input, target_version)
  input_version = input.document_type
  return input.dup if input_version == target_version

  unless [Tilia::VObject::Document::VCARD21, Tilia::VObject::Document::VCARD30, Tilia::VObject::Document::VCARD40].include? input_version
    fail ArgumentError, 'Only vCard 2.1, 3.0 and 4.0 are supported for the input data'
  end
  unless [Tilia::VObject::Document::VCARD30, Tilia::VObject::Document::VCARD40].include? target_version
    fail ArgumentError, 'You can only use vCard 3.0 or 4.0 for the target version'
  end

  new_version = target_version == Tilia::VObject::Document::VCARD40 ? '4.0' : '3.0'

  output = Tilia::VObject::Component::VCard.new('VERSION' => new_version)

  # We might have generated a default UID. Remove it!
  output.delete('UID')

  input.children.each do |property|
    convert_property(input, output, property, target_version)
  end

  output
end