brewster / vcardigan

Ruby vCard Builder/Parser
MIT License
71 stars 23 forks source link

How do I add multiple emails / phone numbers? #6

Open cberkom opened 10 years ago

cberkom commented 10 years ago

The following code only includes the last TEL attribute:

vcard.tel "+1 222 333 4445"
vcard.tel "+1 333 444 5555"

Is there a way to add multiple phone numbers using this gem?

anthonator commented 9 years ago

:+1:

@cberkom Were you able to figure this out?

runa commented 9 years ago

this works for me, am I missing something?

anthonator commented 9 years ago

Could you provide some example code of it working for you?

mustmodify commented 9 years ago

According to Wikipedia, I want one of these:

BEGIN:VCARD
VERSION:2.1
FN:Forrest Gump
ORG:Bubba Gump Shrimp Co.
TEL;WORK;VOICE:(111) 555-1212
TEL;HOME;VOICE:(404) 555-1212
BEGIN:VCARD
VERSION:3.0
FN:Forrest Gump
ORG:Bubba Gump Shrimp Co.
TITLE:Shrimp Man
TEL;TYPE=WORK,VOICE:(111) 555-1212
TEL;TYPE=HOME,VOICE:(404) 555-1212
ADR;TYPE=WORK:;;100 Waters Edge;Baytown;LA;30314;United States of America
BEGIN:VCARD
VERSION:4.0
FN:Forrest Gump
ORG:Bubba Gump Shrimp Co.
TITLE:Shrimp Man
TEL;TYPE=work,voice;VALUE=uri:tel:+1-111-555-1212
TEL;TYPE=home,voice;VALUE=uri:tel:+1-404-555-1212
ADR;TYPE=work;LABEL="100 Waters Edge\nBaytown, LA 30314\nUnited States of America":;;100 Waters Edge;Baytown;LA;30314;United States of America

When I did this:

    VCardigan.create(version: 3).tap do |vcard|
      vcard.fullname "Big Bird"
      vcard.email "yellow@feathers.com"
      vcard.tel 'Work', "123 456 7890"
      vcard.tel 'Cell', 
    end.to_s

Version 3 seems like a good mix between machine-sensible and human-readable, so I'm aiming for that, or whichever is easiest.

Looking through the code, it seems like VCardigan::Property is where the magic is happening:

vcard.whatever hits method_missing assuming it has arguments, it goes to add which creates a property VCardigan::Property.new(vcard, 'tel', ['Work', '123 456 7890']) examine the name. Is it namespaced / aka grouped?

So it seems like property often takes a hash. Here's what I get for that:

puts VCardigan.create(version: 3).tap {|v| v.fullname "bird"; v.tel(type:'Work', voice:'1234567890');}.to_s
BEGIN:VCARD
VERSION:3
FN:bird
TEL;TYPE=Work;VOICE=1234567890:
END:VCARD

Close, but no good: I want an = between TYPE and Work, but a : between VOICE and the number.

Looking at the code, there's something special about the key preferred but that didn't seem to help me.

Well, what about version 2? I'm looking for TEL;WORK;VOICE:(111) 555-1212 so ;;:

puts VCardigan.create(version: 2.1).tap {|v| v.fullname "bird"; v.tel('Work', '1234567890');}.to_s
BEGIN:VCARD
VERSION:2.1
FN:bird
TEL:Work;1234567890
END:VCARD

or

puts VCardigan.create(version: 2.1).tap {|v| v.fullname "bird"; v.tel('Work'=> '1234567890');}.to_s
BEGIN:VCARD
VERSION:2.1
FN:bird
TEL;WORK=1234567890:

END:VCARD

or

 puts VCardigan.create(version: 2.1).tap {|v| v.fullname "bird"; v.tel('WORK:1234567890');}.to_s
BEGIN:VCARD
VERSION:2.1
FN:bird
TEL:WORK:1234567890
END:VCARD

or

puts VCardigan.create(version: 2.1).tap {|v| v.fullname "bird"; v.tel('WORK;VOICE:1234567890');}.to_s
BEGIN:VCARD
VERSION:2.1
FN:bird
TEL:WORK;VOICE:1234567890
END:VCARD

so the only time I got a ; after TEL was with a hash... let's look at that.

hashes get a param and a value

Not helpful enough.

At this point it makes more sense to hand-code the VCard than to use this gem.

anthonator commented 9 years ago

Check out vCardio