vcard_parser 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/ChangeLog ADDED
@@ -0,0 +1,4 @@
1
+
2
+ # 0.0.2
3
+ - VERSION can now be anywhere in the vCard (was required to be first)
4
+ - When generating the vCard user can limit the fields exposed
data/Gemfile CHANGED
@@ -6,7 +6,7 @@ gem 'rake'
6
6
 
7
7
  group(:development) do
8
8
  gem 'simplecov'
9
- gem 'guard-bacon', '~> 1.1.0'
9
+ gem 'guard-bacon'
10
10
  gem 'rb-fsevent'
11
11
  gem 'growl'
12
12
  end
@@ -20,16 +20,19 @@ module VCardParser
20
20
 
21
21
  def self.parse(data)
22
22
  data.scan(VCARD_FORMAT).map do |vcard_data|
23
- # fetch the version to choose the correct parser
23
+ # find the version to choose the correct parser
24
24
  lines = vcard_data[0].each_line.map(&:strip)
25
-
26
- key, version = lines[0].split(':')
27
- if key != "VERSION"
28
- raise MalformedInput, "VERSION expected, got #{key}"
25
+
26
+ version_line_index = lines.index{|str| str.start_with?('VERSION:') }
27
+
28
+ unless version_line_index
29
+ raise MalformedInput, "Unable to find VERSION field"
29
30
  end
30
31
 
31
- # remove begin and version
32
- lines.slice!(0, 1)
32
+ # remove version field
33
+ version_line = lines.delete_at(version_line_index)
34
+
35
+ key, version = version_line.split(':')
33
36
 
34
37
  new(version).tap do |card|
35
38
  lines.each do |line|
@@ -66,11 +69,17 @@ module VCardParser
66
69
  @fields.each(&block)
67
70
  end
68
71
 
69
- def vcard
70
- ret = ["BEGIN:VCARD", "VERSION:#{@version}"]
72
+ def vcard(wanted_fields = [])
73
+ ret = ["BEGIN:VCARD"]
74
+
75
+ if wanted_fields.empty? || wanted_fields.include?('VERSION')
76
+ ret << "VERSION:#{@version}"
77
+ end
71
78
 
72
79
  @fields.each do |f|
73
- ret << f.to_s
80
+ if wanted_fields.empty? || wanted_fields.include?(f.name)
81
+ ret << f.to_s
82
+ end
74
83
  end
75
84
 
76
85
  ret << "END:VCARD\n"
@@ -1,3 +1,3 @@
1
1
  module VcardParser
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -33,6 +33,27 @@ describe 'VCard' do
33
33
  vcards[0]["UID"].value.should == "11"
34
34
  end
35
35
 
36
+ should 'parse simple card with UID as first field' do
37
+ data = <<-EOS
38
+ BEGIN:VCARD
39
+ UID:20121108T134354Z-1348-27ECEFA3-2-53E3B7B2.vcf
40
+ VERSION:3.0
41
+ PRODID:-//SOGoSync 0.5.0//NONSGML SOGoSync AddressBook//EN
42
+ FN:Pp, Tt
43
+ N:Pp;Tt
44
+ END:VCARD
45
+ EOS
46
+
47
+ vcards = VCardParser::VCard.parse(data)
48
+ vcards.size.should == 1
49
+
50
+ vcards[0].version.should == "3.0"
51
+ vcards[0]['PRODID'].value.should == '-//SOGoSync 0.5.0//NONSGML SOGoSync AddressBook//EN'
52
+ vcards[0]['UID'].value.should == '20121108T134354Z-1348-27ECEFA3-2-53E3B7B2.vcf'
53
+ vcards[0]['FN'].value.should == 'Pp, Tt'
54
+ vcards[0]['N'].value.should == 'Pp;Tt'
55
+ end
56
+
36
57
  should 'parse multiple cards' do
37
58
  vcards = VCardParser::VCard.parse( data_file('two_vcard3.0.vcf') )
38
59
  vcards.size.should == 2
@@ -185,6 +206,20 @@ END:VCARD
185
206
  vcard.vcard.should == data
186
207
  vcard.to_s.should == data
187
208
  end
209
+
210
+ should 'generate partial vCard' do
211
+ data = data_file('vcard3.0.vcf')
212
+ vcard = VCardParser::VCard.parse(data).first
213
+ vcard.vcard.should == data
214
+ vcard.to_s(%w(VERSION UID NICKNAME EMAIL FN)).should == <<-EOS
215
+ BEGIN:VCARD
216
+ VERSION:3.0
217
+ FN:Christophe Durand
218
+ UID:11
219
+ END:VCARD
220
+ EOS
221
+ end
222
+
188
223
  end
189
224
 
190
225
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vcard_parser
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-11-02 00:00:00.000000000 Z
12
+ date: 2012-11-08 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: A vCard parser
15
15
  email:
@@ -20,6 +20,7 @@ extra_rdoc_files: []
20
20
  files:
21
21
  - .gitignore
22
22
  - .travis.yml
23
+ - ChangeLog
23
24
  - Gemfile
24
25
  - Guardfile
25
26
  - LICENSE
@@ -50,7 +51,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
50
51
  version: '0'
51
52
  segments:
52
53
  - 0
53
- hash: -4310828650863439750
54
+ hash: 1834945485564550677
54
55
  required_rubygems_version: !ruby/object:Gem::Requirement
55
56
  none: false
56
57
  requirements:
@@ -59,7 +60,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
59
60
  version: '0'
60
61
  segments:
61
62
  - 0
62
- hash: -4310828650863439750
63
+ hash: 1834945485564550677
63
64
  requirements: []
64
65
  rubyforge_project:
65
66
  rubygems_version: 1.8.24