Class: VCardio::VCard

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(version = '3.0', properties = [], &block) ⇒ VCard

Returns a new instance of VCard.



3
4
5
6
7
8
# File 'lib/vcardio/vcard.rb', line 3

def initialize(version = '3.0', properties = [], &block)
  @properties = properties
  @version    = version

  builder(&block) if block_given?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object



63
64
65
66
# File 'lib/vcardio/vcard.rb', line 63

def method_missing(method_name, *args, &block)
  return super if respond_to?(method_name)
  get(method_name)
end

Instance Attribute Details

#propertiesObject (readonly)

Returns the value of attribute properties.



10
11
12
# File 'lib/vcardio/vcard.rb', line 10

def properties
  @properties
end

#versionObject (readonly)

Returns the value of attribute version.



10
11
12
# File 'lib/vcardio/vcard.rb', line 10

def version
  @version
end

Class Method Details

.parse(file) ⇒ Object



68
69
70
71
72
73
74
75
76
# File 'lib/vcardio/vcard.rb', line 68

def self.parse(file)
  if file.is_a?(String)
    VCardio::Parser::DocumentParser.call(file)
  elsif file.is_a?(File)
    VCardio::Parser::DocumentParser.call(file.read)
  else
    fail(VCardio::Error, 'Could not parse. Expecting string or file.')
  end
end

Instance Method Details

#==(other) ⇒ Object



57
58
59
60
61
# File 'lib/vcardio/vcard.rb', line 57

def ==(other)
  other.is_a?(VCardio::VCard) &&
    other.properties == properties &&
    other.version    == version
end

#get(property_name) ⇒ Object



12
13
14
# File 'lib/vcardio/vcard.rb', line 12

def get(property_name)
  @properties.select { |p| p.name == property_name.to_s.upcase }
end

#separatorObject



16
17
18
19
20
21
22
# File 'lib/vcardio/vcard.rb', line 16

def separator
  if spec == :rfc2426
    "\n"
  elsif spec == :rfc6350
    "\r\n"
  end
end

#specObject



24
25
26
27
28
29
30
# File 'lib/vcardio/vcard.rb', line 24

def spec
  if @version == '3.0'
    :rfc2426
  elsif @version == '4.0'
    :rfc6350
  end
end

#to_abnfObject Also known as: to_s



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/vcardio/vcard.rb', line 32

def to_abnf
  abnf = []
  abnf << 'BEGIN:VCARD'
  abnf << "VERSION:#{version}"

  @properties.each do |property|
    abnf << "#{Manilla.fold(property.to_abnf(spec), 75, "\r\n\s")}"
  end

  abnf << 'END:VCARD'

  abnf.join(separator)
end

#to_file(path) ⇒ Object



47
48
49
50
51
52
53
54
55
# File 'lib/vcardio/vcard.rb', line 47

def to_file(path)
  file = File.new(path, 'w')

  file.write(to_abnf)
  file.flush
  file.close

  file
end