Class: Vpim::Vcard::Email

Inherits:
String
  • Object
show all
Defined in:
lib/vpim/vcard.rb

Overview

Represents the value of an EMAIL field.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(email = '') ⇒ Email

:nodoc:



206
207
208
209
210
211
212
# File 'lib/vpim/vcard.rb', line 206

def initialize(email='') #:nodoc:
  @preferred = false
  @format = 'internet'
  @location = []
  @nonstandard = []
  super(email)
end

Instance Attribute Details

#formatObject

internet, x400 (String): the email address format, rarely specified since the default is ‘internet’



197
198
199
# File 'lib/vpim/vcard.rb', line 197

def format
  @format
end

#locationObject

home, work (Array of String): the location referred to by the address. The inclusion of location parameters in a vCard seems to be non-conformant, strictly speaking, but also seems to be widespread.



201
202
203
# File 'lib/vpim/vcard.rb', line 201

def location
  @location
end

#nonstandardObject (readonly)

nonstandard types, their meaning is undefined (Array of String). These might be found during decoding, but shouldn’t be set during encoding.



204
205
206
# File 'lib/vpim/vcard.rb', line 204

def nonstandard
  @nonstandard
end

#preferredObject

true, false (boolean): whether this is the preferred email address



194
195
196
# File 'lib/vpim/vcard.rb', line 194

def preferred
  @preferred
end

Class Method Details

.decode(field) ⇒ Object

:nodoc:



243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'lib/vpim/vcard.rb', line 243

def Email.decode(field) #:nodoc:
  value = field.to_text.strip

  if value.length < 1
    raise InvalidEncodingError, "EMAIL must have a value"
  end

  eml = Email.new(value)

  params = field.pvalues('TYPE')

  if params
    params.each do |p|
      p.downcase!
      case p
      when 'home', 'work'
        eml.location << p
      when 'pref'
        eml.preferred = true
      when 'x400', 'internet'
        eml.format = p
      else
        eml.nonstandard << p
      end
    end
    # Strip duplicates
    [ eml.location, eml.nonstandard ].each do |a|
      a.uniq!
    end
  end

  eml
end

Instance Method Details

#encodeObject

:nodoc:



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/vpim/vcard.rb', line 223

def encode #:nodoc:
  value = to_str.strip

  if value.length < 1
    raise InvalidEncodingError, "EMAIL must have a value"
  end

  params = [ @location, @nonstandard ]
  params << @format if @format != 'internet'
  params << 'pref'  if @preferred

  params = params.flatten.compact.map { |s| s.to_str.downcase }.uniq

  paramshash = {}

  paramshash['TYPE'] = params if params.first

  Vpim::DirectoryInfo::Field.create( 'EMAIL', value, paramshash)
end

#inspectObject

:nodoc:



214
215
216
217
218
219
220
221
# File 'lib/vpim/vcard.rb', line 214

def inspect #:nodoc:
  s = "#<#{self.class.to_s}: #{to_str.inspect}"
  s << ", pref" if preferred
  s << ", #{format}" if format != 'internet'
  s << ", " << @location.join(", ") if @location.first
  s << ", #{@nonstandard.join(", ")}" if @nonstandard.first
  s
end