vcard_parser 1.0.1 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4a9accfba5bf860e975f97d990d2cae54f17eca6
4
- data.tar.gz: cc74fb2db60fb1c9278e4fde2981424137d62e74
3
+ metadata.gz: 334aa49432c9d0b544d32ab4434698c2d21246b0
4
+ data.tar.gz: 3dfa1f540320638b7f4f58d839aded48e2ac3663
5
5
  SHA512:
6
- metadata.gz: fd5ef6ee7cf1341976a8b2211f26ceb35c09d03cd4dab9dea2f9cd12a6e040eeddd9040d3d447dd5c40fb02d81d6dadf96a478fa1f1fffa67fa216ffbdfe3ee8
7
- data.tar.gz: 212256186ca48cc2bc29eca1a999b4ba88c37863b722eee4b4ae7b29e9e4b16e209f9c09cd58bbdf7f3752ade3fc93f5a858686142d113d8c69645ae73e5bf29
6
+ metadata.gz: ed688420fa62e9947f9e566be969859a0e23bfdf1d77c26ebedc2d735fd1c32d128d51fc0664a9d54a31895d95fdb8cfab5e98a53db13268984c632b2c32e013
7
+ data.tar.gz: cc03578a116017cfa7e6f2fbe4211ea7ccf549a58a849bab8b1676ecb31ebcaebce60611a342729866b6f5ea279277826a0aae4322c47a8768085f0f35d65b59
@@ -5,7 +5,7 @@ module VCardParser
5
5
  MalformedInput = Class.new(ParsingError)
6
6
 
7
7
  class VCard
8
- attr_reader :version, :fields
8
+ attr_reader :version, :fields, :groups
9
9
 
10
10
  VCARD_FORMAT = /
11
11
  BEGIN:VCARD\s+
@@ -16,6 +16,7 @@ module VCardParser
16
16
  def initialize(version, fields = [])
17
17
  @version = version
18
18
  @fields = fields
19
+ @groups = Set.new
19
20
  end
20
21
 
21
22
  def self.parse(data)
@@ -40,20 +41,30 @@ module VCardParser
40
41
  card.add_field_from_string(line)
41
42
  end
42
43
  end
43
-
44
44
  end
45
45
  end
46
46
 
47
47
  def add_field_from_string(line)
48
48
  f = field_class.parse(line)
49
+ @groups << f.group if f.group
49
50
  @fields << f
50
51
  end
51
52
 
52
53
  def add_field(*args)
53
54
  f = field_class.new(*args)
55
+ @groups << f.group if f.group
54
56
  @fields << f
55
57
  end
56
58
 
59
+ def get_fields_by_group(name)
60
+ ret = {}
61
+ @fields.select{|f| f.group == name }.each do |f|
62
+ ret[f.name.downcase] = f
63
+ end
64
+
65
+ ret
66
+ end
67
+
57
68
  def values(key, group = nil)
58
69
  @fields.select do |f|
59
70
  (f.name == key) &&
@@ -61,6 +72,17 @@ module VCardParser
61
72
  end
62
73
  end
63
74
 
75
+ # retrieve first match and remove fields
76
+ def delete(key, group = nil)
77
+ @fields.each.with_index do |f, n|
78
+ if (f.name == key) && (!group || (f.group == group))
79
+ return @fields.delete_at(n)
80
+ end
81
+ end
82
+
83
+ return nil
84
+ end
85
+
64
86
  def [](key, group = nil)
65
87
  v = values(key, group)
66
88
  v.empty? ? nil : v[0]
@@ -88,6 +110,16 @@ module VCardParser
88
110
  end
89
111
 
90
112
  alias :to_s :vcard
113
+
114
+
115
+
116
+ def lastname
117
+ self['N'].value.split(';')[0]
118
+ end
119
+
120
+ def firstname
121
+ self['N'].value.split(';')[1]
122
+ end
91
123
 
92
124
 
93
125
  private
@@ -1,3 +1,3 @@
1
1
  module VcardParser
2
- VERSION = "1.0.1"
2
+ VERSION = "1.1.0"
3
3
  end
@@ -78,6 +78,14 @@ END:VCARD
78
78
  vcards[1]["UID"].value.should == "12"
79
79
  end
80
80
 
81
+ should 'return firstname, lastname' do
82
+ vcards = VCardParser::VCard.parse( data_file('two_vcard3.0.vcf') )
83
+ vcards.size.should == 2
84
+
85
+ vcards[0].firstname.should == "Christophe"
86
+ vcards[0].lastname.should == "Durand"
87
+ end
88
+
81
89
  should 'enumerate attributes 1' do
82
90
  vcards = VCardParser::VCard.parse( data_file('vcard3.0.vcf') )
83
91
  vcards.size.should == 1
@@ -91,6 +99,30 @@ END:VCARD
91
99
  attrs.size.should == 12
92
100
  end
93
101
 
102
+ should 'list groups' do
103
+ vcards = VCardParser::VCard.parse( data_file('vcard3.0.vcf') )
104
+ vcards.size.should == 1
105
+
106
+ vcards[0].groups.to_a.should == %w(item1 item2)
107
+ end
108
+
109
+ should 'return fields for given group' do
110
+ vcards = VCardParser::VCard.parse( data_file('vcard3.0.vcf') )
111
+ vcards.size.should == 1
112
+
113
+ fields = vcards[0].get_fields_by_group('item2')
114
+ fields.size.should == 2
115
+
116
+ fields['tel'].tap do |f|
117
+ f.name.should == "TEL"
118
+ f.value.should == "5 66"
119
+ end
120
+
121
+ fields['adr'].tap do |f|
122
+ f.name.should == "ADR"
123
+ end
124
+ end
125
+
94
126
  should 'enumerate attributes 2' do
95
127
  vcards = VCardParser::VCard.parse( data_file('vcard3.0.vcf') )
96
128
  vcards.size.should == 1
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vcard_parser
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Julien Ammous
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-12-15 00:00:00.000000000 Z
11
+ date: 2016-03-10 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: A vCard parser
14
14
  email:
@@ -56,7 +56,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
56
56
  version: '0'
57
57
  requirements: []
58
58
  rubyforge_project:
59
- rubygems_version: 2.2.2
59
+ rubygems_version: 2.4.5.1
60
60
  signing_key:
61
61
  specification_version: 4
62
62
  summary: A simple vCard parser