Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vpim::Vcard - Parsing vcard 2.1

Tags:

ruby

vcf-vcard

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?

like image 371
Andrey Kuznetsov Avatar asked Aug 08 '10 04:08

Andrey Kuznetsov


1 Answers

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.

like image 175
Ken Bloom Avatar answered Nov 01 '22 02:11

Ken Bloom