Class: Vcard::Vcard::Email

Inherits:
String
  • Object
show all
Defined in:
lib/vcard/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:



193
194
195
196
197
198
199
# File 'lib/vcard/vcard.rb', line 193

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”



184
185
186
# File 'lib/vcard/vcard.rb', line 184

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.



188
189
190
# File 'lib/vcard/vcard.rb', line 188

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.



191
192
193
# File 'lib/vcard/vcard.rb', line 191

def nonstandard
  @nonstandard
end

#preferredObject

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



181
182
183
# File 'lib/vcard/vcard.rb', line 181

def preferred
  @preferred
end

Class Method Details

.decode(field) ⇒ Object

:nodoc:



230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/vcard/vcard.rb', line 230

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

  if value.length < 1
    raise ::Vcard::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:



210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/vcard/vcard.rb', line 210

def encode #:nodoc:
  value = to_str.strip

  if value.length < 1
    raise ::Vcard::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

  ::Vcard::DirectoryInfo::Field.create("EMAIL", value, paramshash)
end

#inspectObject

:nodoc:



201
202
203
204
205
206
207
208
# File 'lib/vcard/vcard.rb', line 201

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