Vpim :: Vcard - Parsing vcard 2.1

My code should analyze the Vcard 2.1 format. I am using vpim (no other libs)

When I run Vpim::Vcard.decode(data) , I get an 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 tried to run gem install vcard and require the vcard directive (config.gem does not include this file after gem vpim) after initializing RailsApp. ( config.gem 'vpim' included in environment.rb) So 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 Vpim::InvalidEncodingError )

Vcard code I'm trying to parse:

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

Interestingly, the second error is that I am decoding inside the Rails model. When I try to decode directly from script / console (after installing vcard gem and directly turning on "vcard"), I successfully get a 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 an 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 can I disassemble Vcard 2.1 in rails?

+6
ruby vcard
source share
1 answer

The problem is that String#each was removed in Ruby 1.9.1. (Its functionality is stored in String#each_line ). To fix / work around this error, do one of the following:

  • Launch the rails app on Ruby 1.8.7.
  • You can edit the Vpim code to use String#each_line accordingly (and send the vpim patch to the author).
  • You can defuse the String class by specifying each

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

I recommend the 4th option.

+11
source share

All Articles