4

我正在使用Vpim生成 .vcf 文件,然后用户可以将其导入到他们的地址簿中。我遇到的问题是他们下载的信息是针对公司而不是针对个人的,因此我需要将卡标记为此类。有没有办法使用 Vpim 来做到这一点,或者我可以用另一个 gem 来完成这个?

def to_vcf
  card = Vpim::Vcard::Maker.make2 do |maker|
  ...
  end
end

地址簿中的名片来源

BEGIN:VCARD
VERSION:3.0
N:;;;;
FN:The Flow Skatepark
ORG:The Flow Skatepark;
item1.TEL;type=pref:(614) 864-1610
item1.X-ABLabel:Work #
item2.ADR;type=WORK;type=pref:;;4252 Groves Rd;Columbus;OH;43232;USA
item2.X-ABADR:us
BDAY;value=date:2001-07-06
X-ABShowAs:COMPANY
X-ABUID:5F7349CB-369F-4EAC-AB65-49ED902BEF9F\:ABPerson
END:VCARD

地址簿中非名片的来源

BEGIN:VCARD
VERSION:3.0
N:;The Flow Skatepark;;;
FN:The Flow Skatepark
item1.TEL;type=pref:(614) 864-1610
item1.X-ABLabel:Work #
item2.ADR;type=WORK;type=pref:;;4252 Groves Rd;Columbus;OH;43232;USA
item2.X-ABADR:us
BDAY;value=date:2001-07-06
X-ABUID:5F7349CB-369F-4EAC-AB65-49ED902BEF9F\:ABPerson
END:VCARD

显然,这些代码示例有两个主要区别:

  • ORG:流动滑板公园;
  • X-ABSShowAs:公司

但是,我不知道这如何转化为 Vpim。

enter image description here

4

2 回答 2

3

Quick and dirty implementation, I hope that I understood you correctly:

require 'vpim/vcard'

vcards = <<VCARD
BEGIN:VCARD
VERSION:3.0
N:;;;;
FN:The Flow Skatepark
ORG:The Flow Skatepark;
item1.TEL;type=pref:(614) 864-1610
item1.X-ABLabel:Work #
item2.ADR;type=WORK;type=pref:;;4252 Groves Rd;Columbus;OH;43232;USA
item2.X-ABADR:us
BDAY;value=date:2001-07-06
X-ABShowAs:COMPANY
X-ABUID:5F7349CB-369F-4EAC-AB65-49ED902BEF9F\:ABPerson
END:VCARD
BEGIN:VCARD
VERSION:3.0
N:;The Flow Skatepark;;;
FN:The Flow Skatepark
item1.TEL;type=pref:(614) 864-1610
item1.X-ABLabel:Work #
item2.ADR;type=WORK;type=pref:;;4252 Groves Rd;Columbus;OH;43232;USA
item2.X-ABADR:us
BDAY;value=date:2001-07-06
X-ABUID:5F7349CB-369F-4EAC-AB65-49ED902BEF9F\:ABPerson
END:VCARD
VCARD

contacts = []
Vpim::Vcard.decode(vcards).each do |vcard|
  contacts << {
    first_name:        vcard.name      ? vcard.name.given         : '',
    last_name:         vcard.name      ? vcard.name.family        : '',
    organisation_name: vcard.org       ? vcard.org.first          : '',
  }
end

def to_vcard(card)
  Vpim::Vcard::Maker.make2 do |maker|
    maker.add_name do |name|
      name.given = card[:first_name] unless card[:first_name].empty? || card[:first_name].nil?
      name.family = card[:last_name] unless card[:last_name].empty?  || card[:last_name].nil?
    end
    maker.org = card[:organisation_name] unless card[:organisation_name].empty? || card[:organisation_name].nil?
  end
end

contacts.each_with_index do |contact, idx|
  File.open("contact#{idx}.vcf", 'w') {|f| f.write(to_vcard(contact)) }  
end

Normal vs Company

于 2011-07-11T19:34:21.273 回答
0

Looks like the maker object has a org= method, which you can use to set ORG. As for X-ABShowAs, maker has an add_field method. You can probably create your own field (http://vpim.rubyforge.org/classes/Vpim/DirectoryInfo/Field.html) for that.

于 2011-07-10T09:25:21.230 回答