vcard_mate 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,87 @@
1
+ module VCardMate
2
+
3
+ class Property
4
+
5
+ attr_accessor :params
6
+ attr_accessor :values
7
+ attr_accessor :group
8
+
9
+ def initialize(name, *args)
10
+ @params = {}
11
+ @values = []
12
+ @group = nil
13
+
14
+ # Determine whether this property name has a group
15
+ name_parts = name.to_s.split('.', 2)
16
+
17
+ # If it has a group, set it
18
+ if name_parts.length > 1
19
+ @group = name_parts.first
20
+ end
21
+
22
+ # Set the name
23
+ @name = name_parts.last.downcase
24
+
25
+ # Build out the values/params from the passed arguments
26
+ args.each do |arg|
27
+ if arg.is_a? Hash
28
+ arg.each do |key, value|
29
+ @params[key.to_s.downcase] = value.to_s
30
+ end
31
+ else
32
+ @values.push arg.to_s
33
+ end
34
+ end
35
+ end
36
+
37
+ def self.parse(data)
38
+ # Gather the parts
39
+ data = data.strip
40
+ parts = data.split(':', 2)
41
+ values = parts.last.split(';')
42
+ params = parts.first.split(';')
43
+ name = params.shift
44
+
45
+ # Create argument array beginning with name
46
+ args = [name]
47
+
48
+ # Add values to args
49
+ args.concat(values)
50
+
51
+ # Add params to args
52
+ params.each do |param|
53
+ keyval = param.split('=')
54
+ hash = Hash[keyval.first, keyval.last]
55
+ args.push(hash)
56
+ end
57
+
58
+ # Instantiate a new class with the args
59
+ new(*args)
60
+ end
61
+
62
+ def to_s
63
+ # Name/Group
64
+ name = @name.upcase
65
+ property = @group ? "#{@group}.#{name}" : name.upcase
66
+
67
+ # Params
68
+ @params.each_with_index do |(key, value), idx|
69
+ property << ';' if idx === 0
70
+ property << key.upcase << '=' << value
71
+ end
72
+
73
+ # Split with colon
74
+ property << ':'
75
+
76
+ # Values
77
+ @values.each_with_index do |value, idx|
78
+ property << ';' unless idx === 0
79
+ property << value
80
+ end
81
+
82
+ return property
83
+ end
84
+
85
+ end
86
+
87
+ end
@@ -0,0 +1,110 @@
1
+ module VCardMate
2
+
3
+ class VCard
4
+
5
+ VCARD_PATTERN = /BEGIN:VCARD\s+(.*?)VERSION:(.+?)\s+(.+?)END:VCARD/m;
6
+
7
+ attr_accessor :version
8
+
9
+ def initialize(version = '4.0')
10
+ @version = version
11
+ @fields = {}
12
+ @group = nil
13
+ end
14
+
15
+ def parse(data)
16
+ match = VCARD_PATTERN.match(data)
17
+ if match
18
+ # Set version number
19
+ @version = match[2]
20
+ lines = "#{match[1]}#{match[3]}"
21
+
22
+ # Add the parsed properties to this vCard
23
+ lines.each_line do |line|
24
+ property = VCardMate::Property.parse(line)
25
+ add_prop(property)
26
+ end
27
+ end
28
+ self
29
+ end
30
+
31
+ def [](group)
32
+ @group = group
33
+ self
34
+ end
35
+
36
+ def method_missing(method, *args)
37
+ if args.empty?
38
+ # Return the property/properties when no arguments are passed
39
+ get_prop(method)
40
+ else
41
+ # If there's a group, add it
42
+ if @group
43
+ method = "#{@group}.#{method}"
44
+ end
45
+
46
+ # Add property to vCard
47
+ property = VCardMate::Property.new(method, *args)
48
+ add_prop(property)
49
+ end
50
+ end
51
+
52
+ def add_prop(property)
53
+ name = property.instance_variable_get(:@name)
54
+
55
+ # Create a field on the fields hash, if not already present, to house
56
+ # the property
57
+ unless @fields.has_key? name
58
+ @fields[name] = []
59
+ end
60
+
61
+ # Add the property to the field array
62
+ @fields[name].push(property)
63
+ end
64
+
65
+ def get_prop(name)
66
+ field = @fields[name.to_s.downcase]
67
+ case field.length
68
+ when 0
69
+ nil
70
+ when 1
71
+ field.first
72
+ else
73
+ field
74
+ end
75
+ end
76
+
77
+ def to_s
78
+ # Start vCard
79
+ vcard = VCardMate::Property.new(:begin, 'VCARD').to_s << "\n"
80
+
81
+ # Add version
82
+ vcard << VCardMate::Property.new(:version, @version).to_s << "\n"
83
+
84
+ # Add the properties
85
+ @fields.each do |field, properties|
86
+ properties.each do |property|
87
+ vcard << property.to_s << "\n"
88
+ end
89
+ end
90
+
91
+ # END
92
+ vcard << VCardMate::Property.new(:end, 'VCARD').to_s << "\n"
93
+
94
+ # Return vCard
95
+ return vcard
96
+ end
97
+
98
+ # Aliases ##########
99
+
100
+ def name(*args)
101
+ n(*args)
102
+ end
103
+
104
+ def fullname(*args)
105
+ fn(*args)
106
+ end
107
+
108
+ end
109
+
110
+ end
@@ -0,0 +1,5 @@
1
+ module VCardMate
2
+
3
+ VERSION = '0.0.1'
4
+
5
+ end
data/lib/vcard_mate.rb ADDED
@@ -0,0 +1,19 @@
1
+ require_relative 'vcard_mate/version'
2
+ require_relative 'vcard_mate/vcard'
3
+ require_relative 'vcard_mate/property'
4
+
5
+ module VCardMate
6
+
7
+ class << self
8
+
9
+ def create(*args)
10
+ VCardMate::VCard.new(*args)
11
+ end
12
+
13
+ def parse(*args)
14
+ VCardMate::VCard.new.parse(*args)
15
+ end
16
+
17
+ end
18
+
19
+ end
metadata ADDED
@@ -0,0 +1,50 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: vcard_mate
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Matt Morgan
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-11-15 00:00:00.000000000Z
13
+ dependencies: []
14
+ description: vCard Mate is a ruby library for building and parsing vCards that supports
15
+ both v3.0 and v4.0.
16
+ email: matt@mlmorg.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - lib/vcard_mate/property.rb
22
+ - lib/vcard_mate/vcard.rb
23
+ - lib/vcard_mate/version.rb
24
+ - lib/vcard_mate.rb
25
+ homepage: http://github.com/mlmorg/vcard_mate
26
+ licenses:
27
+ - MIT
28
+ post_install_message:
29
+ rdoc_options: []
30
+ require_paths:
31
+ - lib
32
+ required_ruby_version: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ required_rubygems_version: !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ requirements: []
45
+ rubyforge_project:
46
+ rubygems_version: 1.8.6
47
+ signing_key:
48
+ specification_version: 3
49
+ summary: Ruby vCard Builder/Parser
50
+ test_files: []