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:



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

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”



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

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.



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

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.



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

def nonstandard
  @nonstandard
end

#preferredObject

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



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

def preferred
  @preferred
end

Class Method Details

.decode(field) ⇒ Object

:nodoc:



229
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
# File 'lib/vcard/vcard.rb', line 229

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:



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

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:



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

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