我得到以下错误:
Vpim::InvalidEncodingError (email@email.net):
2011-06-07T01:37:06+00:00 app[web.1]: .bundle/gems/ruby/1.8/gems/vpim-0.695/lib/vpim/field.rb:110:in `decode0'它在其他vcards上运行得很好。数据看起来是对的--应该是一封电子邮件:
这是一个示例电子名片,当有一个email...what我已经做了修复它手动删除第二封电子邮件,但这是一个痛苦的:
BEGIN:VCARD
VERSION:2.1
N:Roberts;Paul;;;
FN:Paul Roberts
ORG:Sonoma Technology Inc
TITLE:EVP Business Dev/Chief Scientific Officer
TEL;WORK;VOICE:707-665-9900
TEL;WORK;FAX:707-665-9800
ADR;WORK;ENCODING=QUOTED-PRINTABLE:;;1455 N McDowell Blvd Suite D;Petaluma;CA;94954;USA
LABEL;WORK;ENCODING=QUOTED-PRINTABLE:1455 N McDowell Blvd Suite D=0D=0APetaluma, CA 94954=0D=0AUSA
URL:http://www.sonomatech.com
URL:http://www.sonomatech.com
EMAIL;PREF;INTERNET:paul@sonomatech.com
paul@sonomatech.com
NOTE;ENCODING=QUOTED-PRINTABLE:=0D=0A Data provided by Lead411, http://www.lead411.com/=0D=0A =0D=0A
END:VCARD这是我的控制器,使用回形针和vpim:
68 unless @contact.vcard.path.blank?
69
70 paperclip_vcard = File.new(@contact.vcard.path)
71
72 # try to scrub the vcard
73 scrub_vcf(paperclip_vcard)
74
75 @vcard = Vpim::Vcard.decode(paperclip_vcard).first
76 @contact.title = @vcard.title
77 @contact.email = @vcard.email
78 @contact.first_name = @vcard.name.given
79 @contact.last_name = @vcard.name.family
80 @contact.phone = @vcard.telephones[0]
81 @contact.fax = @vcard.telephones[1]
82
83 @contact.address.street1 = @vcard.address.street
84 @contact.address.city = @vcard.address.locality
85 @contact.address.state = @vcard.address.region
86 @contact.address.zip = @vcard.address.postalcode
87 @contact.company_name = @vcard.org.fetch(0)
88
89 end发布于 2011-06-21 00:28:35
您需要查看Vcards是如何创建的;第14行的第二封电子邮件不是有效的属性定义,这是导致解析器搞砸的原因(这也是如果您手动删除它,解析器会成功解析的原因)。
您可以在the Vcard 2.1 specification的第2节中阅读有关属性定义的内容(here中提供了RTF版本,它的可读性更好)。
从您提供的信息来看,这看起来不像是Vpim在解码方面的问题,而是您的Vcards是如何创建的。如果你自己创建Vcards,我会看看你的编码逻辑。如果您从外部接收它们,那么您可能希望编写一些自定义清理逻辑来消除不正确的属性定义,这样您就不必手动删除它们。
通过对每一行进行快速正则表达式检查,您应该能够很容易地做到这一点:
def scrub_vcf(vcard)
line_arr = File.readlines(vcard)
line_arr.delete_if { |line| line.match(/^.+\:.+$/).nil? }
File.open(vcard, "w") do |f|
line_arr.each{|line| f.puts(line)}
end
end
# use the scrubbed vcf with vpim当然,将其保存在数组中可能比将其写回文件FYI更快。
希望这能有所帮助。
更新:如果你不想保留文件,你可以返回一个字符串,它是Vpim can decode而不是文件:
def scrub_vcf(vcard)
line_arr = File.readlines(vcard)
line_arr.delete_if { |line| line.match(/^.+\:.+$/).nil? }
return line_arr.join
end
# use the scrubbed vcf with vpim #=> Vpim::Vcard.decode(scrub_vcf(vcard))请注意,在运行Ruby1.9.x时,我在使用带有Vpim::Vcard.decode的字符串时遇到了问题,这是因为String类不再具有each方法。不过,Ruby 1.8.7运行得很好。Vpim看起来从2008/2009年就没有升级过,所以它可能还没有升级到ruby 1.9.x上。
又更新了一次:这是一个用于Ruby1.9.x的更新版本(精确地修复了我之前遇到的问题):https://github.com/sam-github/vpim
https://stackoverflow.com/questions/6260294
复制相似问题