Mã của tôi phải phân tích cú pháp định dạng Vcard 2.1. Tôi đang sử dụng vpim (không có libs khác)Vpim :: Vcard - Parsing vcard 2.1
Khi tôi chạy Vpim::Vcard.decode(data)
tôi nhận được lỗi:
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'
Tôi tryed để chạy gem install vcard
và require 'vcard 'directry (config.gem không bao gồm tệp này sau vpim gem) sau khi khởi tạo RailsApp. (config.gem 'vpim'
được bao gồm trong environment.rb) Bằng cách này, tôi gặp phải một lỗi khác:
;=D0=9D=D0=B0=D0=B4=D1=80=D0=B0=20=D0=B1=D0=B0=D0=BD=D0=BA=20=D0=BE=D0=BB=
(lớp ngoại lệ là Vpim::InvalidEncodingError
) mã
vCard tôi cố gắng phân tích cú pháp:
BEGIN:VCARD
VERSION:2.1
REV:20090710T151929Z
TEL;CELL:80954130722
X-CLASS:private
END:VCARD
Điều thú vị là lỗi thứ hai là khi tôi giải mã bên trong một mô hình Rails. Khi tôi cố gắng giải mã trực tiếp từ tập lệnh/bàn điều khiển (sau khi cài đặt gem vcard vCard và trực tiếp bao gồm 'vcard'), tôi thành công nhận đối tượng 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)
Đây là axample từ 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 >
Làm thế nào là nó có thể phân tích cú pháp vCard 2.1 trong đường ray?
bạn có thể đăng thêm mã của mình không? bạn đã thử phân tích cú pháp đối tượng io thay vì một chuỗi? bạn đang sử dụng phiên bản ruby nào? – elmac
bạn có thể đăng toàn bộ stacktrace không? –
Tôi đã cập nhật câu hỏi. –