Vpim::Vcard - 解析 vcard 2.1

发布于 2024-09-13 00:47:25 字数 2953 浏览 1 评论 0原文

我的代码必须解析 Vcard 2.1 格式。 我正在使用 vpim(没有其他库)

当我运行 Vpim::Vcard.decode(data) 时,出现错误:

undefined method `each' for #<String:0x0000000928e778>

StackTrace:

  NoMethodError (undefined method `each' for #<String:0x0000000928e778>):
    vpim (0.695) lib/vpim/rfc2425.rb:82:in `unfold'
    vpim (0.695) lib/vpim/rfc2425.rb:308:in `decode'
    vpim (0.695) lib/vpim/vcard.rb:692:in `decode'
    app/models/event.rb:71:in `block (2 levels) in parse_data'

我尝试运行 gem install vcard并在RailsApp初始化后需要'vcard'目录(config.gem在vpim gem之后不包含此文件)。(config.gem 'vpim'包含在environment.rb中) 通过这种方式,我得到另一个错误:

;=D0=9D=D0=B0=D0=B4=D1=80=D0=B0=20=D0=B1=D0=B0=D0=BD=D0=BA =20=D0=BE=D0=BB=

(异常类是 Vpim::InvalidEncodingError

我尝试解析的 Vcard 代码:

BEGIN:VCARD
VERSION:2.1
REV:20090710T151929Z
TEL;CELL:80954130722
X-CLASS:private
END:VCARD

有趣的是,第二个错误是当我在导轨模型。 当我尝试直接从脚本/控制台解码时(在安装了 vcard gem 并直接包括“vcard”之后),我成功获得了 Vcard 对象。

StackTrace:

   Vpim::InvalidEncodingError (;=D0=9D=D0=B0=D0=B4=D1=80=D0=B0=20=D0=B1=D0=B0=D0=BD=D0=BA=20=D0=BE=D0=BB=):
     vcard (0.1.1) lib/vcard/field.rb:106:in `decode0'
     vcard (0.1.1) lib/vcard/field.rb:172:in `initialize'
     vcard (0.1.1) lib/vcard/field.rb:183:in `new'
     vcard (0.1.1) lib/vcard/field.rb:183:in `decode'
     vcard (0.1.1) lib/vcard/rfc2425.rb:308:in `block in decode'
     vcard (0.1.1) lib/vcard/rfc2425.rb:308:in `collect'
     vcard (0.1.1) lib/vcard/rfc2425.rb:308:in `decode'
     vcard (0.1.1) lib/vcard/vcard.rb:686:in `decode'
     app/models/event.rb:71:in `block (2 levels) in parse_data'

app/models/event.rb:71 :

vcard = Vpim::Vcard.decode(contact.text)

这是来自 irb 的示例:

95-25-164-74:~ smix$ irb
ruby-1.9.2-rc2 > str = <<EOS
ruby-1.9.2-rc2"> BEGIN:VCARD
ruby-1.9.2-rc2"> VERSION:2.1
ruby-1.9.2-rc2"> REV:20090710T151929Z
ruby-1.9.2-rc2"> TEL;CELL:80954130722
ruby-1.9.2-rc2"> X-CLASS:private
ruby-1.9.2-rc2"> END:VCARD
ruby-1.9.2-rc2"> EOS
 => "BEGIN:VCARD\nVERSION:2.1\nREV:20090710T151929Z\nTEL;CELL:80954130722\nX-CLASS:private\nEND:VCARD\n" 
ruby-1.9.2-rc2 > require 'vpim'
 => true 
ruby-1.9.2-rc2 > Vpim::Vcard.decode str
NoMethodError: undefined method `each' for #<String:0x000001010e0428>
    from /Users/smix/.rvm/gems/ruby-1.9.2-rc2/gems/vpim-0.695/lib/vpim/rfc2425.rb:82:in `unfold'
    from /Users/smix/.rvm/gems/ruby-1.9.2-rc2/gems/vpim-0.695/lib/vpim/rfc2425.rb:308:in `decode'
    from /Users/smix/.rvm/gems/ruby-1.9.2-rc2/gems/vpim-0.695/lib/vpim/vcard.rb:692:in `decode'
    from (irb):10
    from /Users/smix/.rvm/rubies/ruby-1.9.2-rc2/bin/irb:17:in `<main>'
ruby-1.9.2-rc2 > 

How is it possible to parse Vcard 2.1 in Rails?

My code must parse Vcard 2.1 format.
I am using vpim(there are no other libs)

When I run Vpim::Vcard.decode(data) I get error:

undefined method `each' for #<String:0x0000000928e778>

StackTrace:

  NoMethodError (undefined method `each' for #<String:0x0000000928e778>):
    vpim (0.695) lib/vpim/rfc2425.rb:82:in `unfold'
    vpim (0.695) lib/vpim/rfc2425.rb:308:in `decode'
    vpim (0.695) lib/vpim/vcard.rb:692:in `decode'
    app/models/event.rb:71:in `block (2 levels) in parse_data'

I tryed to run gem install vcard and to require 'vcard' directry(config.gem does not include this file after vpim gem) after RailsApp initialization.(config.gem 'vpim' is included in environment.rb)
By this way I get another error:

;=D0=9D=D0=B0=D0=B4=D1=80=D0=B0=20=D0=B1=D0=B0=D0=BD=D0=BA=20=D0=BE=D0=BB=

(Exception class is Vpim::InvalidEncodingError)

Vcard code I try parse:

BEGIN:VCARD
VERSION:2.1
REV:20090710T151929Z
TEL;CELL:80954130722
X-CLASS:private
END:VCARD

It is interesting that second error is when I am decoding inside a Rails model.
When I try to decode directly from script/console (after installinv vcard gem and direct including 'vcard') I succesfuly get Vcard object.

StackTrace:

   Vpim::InvalidEncodingError (;=D0=9D=D0=B0=D0=B4=D1=80=D0=B0=20=D0=B1=D0=B0=D0=BD=D0=BA=20=D0=BE=D0=BB=):
     vcard (0.1.1) lib/vcard/field.rb:106:in `decode0'
     vcard (0.1.1) lib/vcard/field.rb:172:in `initialize'
     vcard (0.1.1) lib/vcard/field.rb:183:in `new'
     vcard (0.1.1) lib/vcard/field.rb:183:in `decode'
     vcard (0.1.1) lib/vcard/rfc2425.rb:308:in `block in decode'
     vcard (0.1.1) lib/vcard/rfc2425.rb:308:in `collect'
     vcard (0.1.1) lib/vcard/rfc2425.rb:308:in `decode'
     vcard (0.1.1) lib/vcard/vcard.rb:686:in `decode'
     app/models/event.rb:71:in `block (2 levels) in parse_data'

app/models/event.rb:71 :

vcard = Vpim::Vcard.decode(contact.text)

Here is axample from irb:

95-25-164-74:~ smix$ irb
ruby-1.9.2-rc2 > str = <<EOS
ruby-1.9.2-rc2"> BEGIN:VCARD
ruby-1.9.2-rc2"> VERSION:2.1
ruby-1.9.2-rc2"> REV:20090710T151929Z
ruby-1.9.2-rc2"> TEL;CELL:80954130722
ruby-1.9.2-rc2"> X-CLASS:private
ruby-1.9.2-rc2"> END:VCARD
ruby-1.9.2-rc2"> EOS
 => "BEGIN:VCARD\nVERSION:2.1\nREV:20090710T151929Z\nTEL;CELL:80954130722\nX-CLASS:private\nEND:VCARD\n" 
ruby-1.9.2-rc2 > require 'vpim'
 => true 
ruby-1.9.2-rc2 > Vpim::Vcard.decode str
NoMethodError: undefined method `each' for #<String:0x000001010e0428>
    from /Users/smix/.rvm/gems/ruby-1.9.2-rc2/gems/vpim-0.695/lib/vpim/rfc2425.rb:82:in `unfold'
    from /Users/smix/.rvm/gems/ruby-1.9.2-rc2/gems/vpim-0.695/lib/vpim/rfc2425.rb:308:in `decode'
    from /Users/smix/.rvm/gems/ruby-1.9.2-rc2/gems/vpim-0.695/lib/vpim/vcard.rb:692:in `decode'
    from (irb):10
    from /Users/smix/.rvm/rubies/ruby-1.9.2-rc2/bin/irb:17:in `<main>'
ruby-1.9.2-rc2 > 

How is it possible to parse Vcard 2.1 in rails?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

只是我以为 2024-09-20 00:47:25

问题是 String#each 已在 Ruby 1.9.1 中删除。 (其功能在 String#each_line 中保留)。要修复/解决此错误,请执行以下任一操作:

  • 在 Ruby 1.8.7 上运行您的 Rails 应用程序。
  • 您可以编辑 Vpim 代码以适当地使用 String#each_line(并向 vpim 作者发送补丁)
  • 您可以对 String 进行猴子修补 类,通过定义 each 方法

    类字符串
      别名方法:每个,:每个行
    结尾
    

  • 切换到 vcard gem,取自 Vpim 并且支持 Ruby 1.9.1。

我推荐第四个选项。

The problem is that String#each has been removed in Ruby 1.9.1. (Its functionality endures in String#each_line). To fix/work around this bug, do any of the following:

  • Run your rails app on Ruby 1.8.7.
  • You can edit the Vpim code to use String#each_line appropriately (and send the vpim authors a patch)
  • You can monkey-patch the String class by defining the each method

    class String
      alias_method :each, :each_line
    end
    
  • Switch to the vcard gem, which is taken from Vpim and which has Ruby 1.9.1 support.

I recommend the 4th option.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文