Class: Mapi::Message::VcardConverter

Inherits:
Object
  • Object
show all
Includes:
Vpim
Defined in:
lib/mapi/convert/contact.rb

Constant Summary collapse

VCARD_MAP =

a very incomplete mapping, but its a start… can’t find where to set a lot of stuff, like zipcode, jobtitle etc

{
	# these are all standard mapi properties
	:name => [
		{
			:given		=> :given_name,
			:family		=> :surname,
			:fullname	=> :subject
		}
	],
	# outlook seems to eschew the mapi properties this time,
	# like postal_address, street_address, home_address_city
	# so we use the named properties
	:addr => [
		{
			:location	=> 'work',
			:street		=> :business_address_street,
			:locality	=> proc do |props|
				[props.business_address_city, props.business_address_state].compact * ', '
			end
		}
	],

	# right type? maybe date
	:birthday	=> :birthday,
	:nickname	=> :nickname

	# photo available?
	# FIXME finish, emails, telephones etc
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(msg) ⇒ VcardConverter

Returns a new instance of VcardConverter.



57
58
59
# File 'lib/mapi/convert/contact.rb', line 57

def initialize msg
	@msg = msg
end

Instance Attribute Details

#msgObject (readonly)

Returns the value of attribute msg.



56
57
58
# File 'lib/mapi/convert/contact.rb', line 56

def msg
  @msg
end

Instance Method Details

#convertObject



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/mapi/convert/contact.rb', line 95

def convert
	Vpim::Vcard::Maker.make2 do |m|
		# handle name
		[:name, :addr].each do |type|
			VCARD_MAP[type].each do |hash|
				next unless props = get_properties(hash)
				m.send "add_#{type}" do |n|
					props.each { |key, value| n.send "#{key}=", value }
				end
			end
		end

		(VCARD_MAP.keys - [:name, :addr]).each do |key|
			value = get_property VCARD_MAP[key]
			m.send "#{key}=", value if value
		end

		# the rest of the stuff is custom

		url = get_property(:webpage) || get_property(:business_home_page)
		m.add_field field('URL', url) if url
		m.add_field field('X-EVOLUTION-FILE-AS', get_property(:file_under)) if get_property(:file_under)

		addr = get_property(:email_email_address) || get_property(:email_original_display_name)
		if addr
			m.add_email addr do |e|
				e.format ='x400' unless msg.props.email_addr_type == 'SMTP'
			end
		end

		if org = get_property(:company_name)
			m.add_field field('ORG', get_property(:company_name))
		end

		# TODO: imaddress
	end
end

#field(name, *args) ⇒ Object



61
62
63
# File 'lib/mapi/convert/contact.rb', line 61

def field name, *args
	DirectoryInfo::Field.create name, Vpim.encode_text_list(args)
end

#get_properties(hash) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/mapi/convert/contact.rb', line 80

def get_properties hash
	constants = {}
	others = {}
	hash.each do |to, from|
		if String === from
			constants[to] = from
		else
			value = get_property from
			others[to] = value if value
		end
	end
	return nil if others.empty?
	others.merge constants
end

#get_property(key) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/mapi/convert/contact.rb', line 65

def get_property key
	if String === key
		return key
	elsif key.respond_to? :call
		value = key.call msg.props
	else
		value = msg.props[key]
	end
	if String === value and value.empty?
		nil
	else
		value
	end
end