2011-07-03 66 views
4

我使用Vpim生成.vcf文件,然後用戶可以將其導入到他們的地址簿中。我遇到的問題是,他們下載的是公司而不是個人的信息,所以我需要將該卡片標記爲這樣。有沒有辦法使用Vpim來做到這一點,或者是否有另一種可以用來實現這一目的的gem?創建公司卡w/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:The Flow Skatepark;
  • ORG:The Flow Skatepark;
  • X-ABShowAs:公司

我不知道但是如何轉換VPIM。

enter image description here

回答

3

快速和骯髒的實施,我希望我理解正確你:

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

0

貌似製造者對象有一個組織=方法,你可以用它來設置ORG。至於X-ABShowAs,製造商有一個add_field方法。您可以創建自己的字段(http://vpim.rubyforge.org/classes/Vpim/DirectoryInfo/Field.html)。