Class: SimpleVpim

Inherits:
Object
  • Object
show all
Defined in:
lib/simplevpim.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(s) ⇒ SimpleVpim

Returns a new instance of SimpleVpim.



13
14
15
16
17
18
19
20
21
# File 'lib/simplevpim.rb', line 13

def initialize(s)

  h = SimpleConfig.new(s).to_h

  if h[:name] then
    @to_vcard = make_vcard h
  end

end

Instance Attribute Details

#to_vcardObject (readonly) Also known as: to_vcf

Returns the value of attribute to_vcard.



11
12
13
# File 'lib/simplevpim.rb', line 11

def to_vcard
  @to_vcard
end

Instance Method Details

#make_vcard(h) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/simplevpim.rb', line 25

def make_vcard(h)

  prefix = h[:prefix]
  suffix = h[:suffix]
  a = h[:name].split

  if a.length == 2 then
    firstname, lastname = a 
  elsif a[0][/^Mrs?|Ms|Miss|Dr/] then

    prefix = a.shift
    
    if a.length == 2 then
      firstname, lastname = a 
    else
      firstname, middlename, lastname = a
      fullname = a.join ' '
    end
  else
    firstname, middlename, lastname = a
    fullname = a.join ' '
  end

  card = Vpim::Vcard::Maker.make2 do |maker|
    
    def  maker.add(field_name, value, params={})        
      
      field = Vpim::DirectoryInfo::Field.create field_name, value, params
      add_field field
      
    end
    
    maker.add_name do |name|
      name.prefix = prefix if prefix
      name.given = firstname
      name.family = lastname
      name.suffix = suffix if suffix
      name.fullname = fullname if fullname
    end

    # -- email -----------------------------
    
    e = h[:email]
    
    if e then
      
      if e.is_a? String then
        maker.add_email e
      else
        eh = h[:email][:home]
        ew = h[:email][:work]
        maker.add_email(ew) { |e| e.location = 'work' } if ew
        maker.add_email(eh) { |e| e.location = 'home' } if eh
      end
    end
    
    # -- urls ---------------------------------
    
    h[:url] ||= h[:urls]
    
    if h[:url] then
      
      if h[:url].is_a? String then
        maker.add_url h[:url] 
      else
        
        # unfortunately vPim doesn't use a block with the add_url method
        #maker.add_url (h[:url][:work]){|e| e.location = 'work'} if h[:url][:work]
        
        h[:url][:items].each {|url| maker.add_url url }
        
      end
      
    end
    
    # -- photos
    
    maker.add_photo {|photo| photo.link = h[:photo] } if h[:photo]
    
    # -- telephone
    
    tel = h[:tel]
    
    if tel then
    
      if tel.is_a? String then
        
        maker.add_tel tel
        
      else
        th = h[:tel][:home]
        tw = h[:tel][:work]
        maker.add_tel(tw) { |e| e.location = 'work' } if tw
        maker.add_tel(th) { |e| e.location = 'home' } if th
      end
    end
    
    # -- categories ------------      
    maker.add 'CATEGORIES', h[:categories].split(/\s*,\s*/) if h[:categories]
    
    # -- source ------------      
    maker.add 'SOURCE', h[:source] if h[:source]

    # -- Twitter ------------      
    maker.add 'X-TWITTER', h[:twitter] if h[:twitter]
    
    # -- XMPP
    xmpp = h[:jabber] || h[:xmpp]
    maker.add 'X-JABBER', xmpp, 'TYPE' => 'im' if xmpp
    
  end
end