vcard 0.2.13 → 0.2.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8cd694341dcc9a8deb453629cdcadb62beb83d2c
4
- data.tar.gz: 8f02acb17425244c540188442c18db6a35936334
3
+ metadata.gz: dab62931019aa50589848685e0ece97fce22a816
4
+ data.tar.gz: da5844c2ec7d8fff390055b6ae247845d0c4111f
5
5
  SHA512:
6
- metadata.gz: dc5f7ad6dd56250ceaf3f76315cc51b8ce5eca0664b63f1c28ef9b6e5a534cc84b7b5eb5b043c08ff9981b12ab8b5f42167af614159e9d6c297ab4b5f1920566
7
- data.tar.gz: b7452473ea636bf33f0f01cf6a21856ae112e7bbccc2c5591b5ed72f33eff58307fd83aaec7a379d80d49a832868797f4cb2af1c5bbe3724d5c9e7e1228c4745
6
+ metadata.gz: cfd6decb35dcd2a61aea730f5ded93c7555c1a5cb1796a98fcf897172f8f3b0e4116f4021efb4924d803132223ed875833dc96c55504058b7bf30cb46703a22c
7
+ data.tar.gz: 986f18eb24dea4885a53916969fe7161dc8dd05f0b291614250a1106ebc09453c742f45210a4fcc2b2fb22cd80425ef41f139878a679b4db6e9f45da9cca3773
data/README.md CHANGED
@@ -16,6 +16,30 @@ Or install it yourself as:
16
16
 
17
17
  $ gem install vcard
18
18
 
19
+ ## Configuration
20
+
21
+ You can configure how to deal with invalid lines. The gem supports three behaviours:
22
+
23
+ 1. `raise_on_invalid_line = true`
24
+
25
+ Vcard::InvalidEncodingError will be raised if any invalid line is found.
26
+
27
+ 2. `raise_on_invalid_line = false, ignore_invalid_vcards = true`
28
+
29
+ If the vcard source has an invalid line, this vcard object will be ignored.
30
+ If you have only one vcard object in your source string, an empty array will be returned from `Vcard.decode`.
31
+
32
+ 3. `raise_on_invalid_line = false, ignore_invalid_vcards = false`
33
+
34
+ If the vcard is marked as invalid, invalid fields will be ignored, but the vcard will be present in the results of `Vcard#decode`.
35
+
36
+ ```
37
+ Vcard.configure do |config|
38
+ config.raise_on_invalid_line = false # default true
39
+ config.ignore_invalid_vcards = false # default true
40
+ end
41
+ ```
42
+
19
43
  ## Upgrade Notes
20
44
 
21
45
  We are no longer testing against Ruby 1.8.7.
data/lib/vcard.rb CHANGED
@@ -7,6 +7,7 @@ require "date"
7
7
  require "open-uri"
8
8
  require "stringio"
9
9
 
10
+ require "vcard/configuration"
10
11
  require "vcard/attachment"
11
12
  require "vcard/bnf"
12
13
  require "vcard/dirinfo"
@@ -285,4 +286,13 @@ module Vcard
285
286
  end
286
287
  return outer, inner
287
288
  end
289
+
290
+ def configuration
291
+ @configuration ||= Configuration.new
292
+ end
293
+
294
+ def configure
295
+ yield configuration
296
+ end
297
+
288
298
  end
@@ -0,0 +1,16 @@
1
+ module Vcard
2
+ class Configuration
3
+
4
+ attr_accessor :raise_on_invalid_line
5
+ alias_method :raise_on_invalid_line?, :raise_on_invalid_line
6
+
7
+ attr_accessor :ignore_invalid_vcards
8
+ alias_method :ignore_invalid_vcards?, :ignore_invalid_vcards
9
+
10
+ def initialize
11
+ @raise_on_invalid = true
12
+ @ignore_invalid_vcard = true
13
+ end
14
+
15
+ end
16
+ end
data/lib/vcard/dirinfo.rb CHANGED
@@ -31,16 +31,27 @@ module Vcard
31
31
  # Initialize a DirectoryInfo object from +fields+. If +profile+ is
32
32
  # specified, check the BEGIN/END fields.
33
33
  def initialize(fields, profile = nil) #:nodoc:
34
- if fields.detect { |f| ! f.kind_of? DirectoryInfo::Field }
35
- raise ArgumentError, "fields must be an array of DirectoryInfo::Field objects"
34
+ @valid = true
35
+ @fields = []
36
+
37
+ fields.each do |f|
38
+ raise ArgumentError, "fields must be an array of DirectoryInfo::Field objects" unless f.kind_of? DirectoryInfo::Field
39
+ if f.valid?
40
+ @fields << f
41
+ else
42
+ @valid = false
43
+ end
36
44
  end
37
45
 
38
46
  @string = nil # this is used as a flag to indicate that recoding will be necessary
39
- @fields = fields
40
47
 
41
48
  check_begin_end(profile) if profile
42
49
  end
43
50
 
51
+ def valid?
52
+ @valid
53
+ end
54
+
44
55
  # Decode +card+ into a DirectoryInfo object.
45
56
  #
46
57
  # +card+ may either be a something that is convertible to a string using
data/lib/vcard/field.rb CHANGED
@@ -101,10 +101,12 @@ module Vcard
101
101
  line
102
102
  end
103
103
 
104
+
104
105
  # Decode a field.
105
106
  def Field.decode0(atline) # :nodoc:
106
107
  if !(atline =~ Bnf::LINE)
107
- raise ::Vcard::InvalidEncodingError, atline
108
+ raise(::Vcard::InvalidEncodingError, atline) if ::Vcard.configuration.raise_on_invalid_line?
109
+ return false
108
110
  end
109
111
 
110
112
  atgroup = $1.upcase
@@ -165,19 +167,27 @@ module Vcard
165
167
  end
166
168
  end
167
169
 
168
- [ atgroup, atname, atparams, atvalue ]
170
+ [ true, atgroup, atname, atparams, atvalue ]
169
171
  end
170
172
 
171
173
  def initialize(line) # :nodoc:
172
174
  @line = line.to_str
173
- @group, @name, @params, @value = Field.decode0(@line)
175
+ @valid, @group, @name, @params, @value = Field.decode0(@line)
174
176
 
175
- @params.each do |pname,pvalues|
176
- pvalues.freeze
177
+ if valid?
178
+ @params.each do |pname,pvalues|
179
+ pvalues.freeze
180
+ end
181
+ else
182
+ @group = @name = ''
177
183
  end
178
184
  self
179
185
  end
180
186
 
187
+ def valid?
188
+ @valid
189
+ end
190
+
181
191
  # Create a field by decoding +line+, a String which must already be
182
192
  # unfolded. Decoded fields are frozen, but see #copy().
183
193
  def Field.decode(line)
@@ -323,7 +333,7 @@ module Vcard
323
333
 
324
334
  # Is the #name of this Field +name+? Names are case insensitive.
325
335
  def name?(name)
326
- @name.casecmp(name) == 0
336
+ @name.to_s.casecmp(name) == 0
327
337
  end
328
338
 
329
339
  # Is the #group of this field +group+? Group names are case insensitive.
@@ -590,7 +600,7 @@ module Vcard
590
600
  # new fields, not old fields.
591
601
  def mutate(g, n, p, v) #:nodoc:
592
602
  line = Field.encode0(g, n, p, v)
593
- @group, @name, @params, @value = Field.decode0(line)
603
+ @valid, @group, @name, @params, @value = Field.decode0(line)
594
604
  @line = line
595
605
  self
596
606
  rescue ::Vcard::InvalidEncodingError => e
data/lib/vcard/vcard.rb CHANGED
@@ -659,7 +659,8 @@ module Vcard
659
659
  vcards = []
660
660
 
661
661
  for e in entities
662
- vcards.push(new(e.flatten, "VCARD"))
662
+ vcard = new(e.flatten, "VCARD")
663
+ vcards.push(vcard) if vcard.valid? || !::Vcard.configuration.ignore_invalid_vcards?
663
664
  end
664
665
 
665
666
  vcards
data/lib/vcard/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Vcard
2
- VERSION = "0.2.13"
2
+ VERSION = "0.2.14"
3
3
  end
data/test/field_test.rb CHANGED
@@ -21,31 +21,31 @@ class FieldTest < Test::Unit::TestCase
21
21
  def test_field4
22
22
  line = "t;e=a,b: 4 "
23
23
  part = Field.decode0(line)
24
- assert_equal("4", part[ 3 ])
24
+ assert_equal("4", part[ 4 ])
25
25
  end
26
26
 
27
27
  def test_field3
28
28
  line = "t;e=a,b:4"
29
29
  part = Field.decode0(line)
30
- assert_equal("4", part[ 3 ])
31
- assert_equal( {"E" => [ "a","b" ] }, part[ 2 ])
30
+ assert_equal("4", part[ 4 ])
31
+ assert_equal( {"E" => [ "a","b" ] }, part[ 3 ])
32
32
  end
33
33
 
34
34
  def test_field2
35
35
  line = "tel;type=work,voice,msg:+1 313 747-4454"
36
36
  part = Field.decode0(line)
37
- assert_equal("+1 313 747-4454", part[ 3 ])
38
- assert_equal( {"TYPE" => [ "work","voice","msg" ] }, part[ 2 ])
37
+ assert_equal("+1 313 747-4454", part[ 4 ])
38
+ assert_equal( {"TYPE" => [ "work","voice","msg" ] }, part[ 3 ])
39
39
  end
40
40
 
41
41
  def test_field1
42
42
  line = 'ORGANIZER;CN="xxxx, xxxx [SC100:370:EXCH]":MAILTO:xxxx@americasm01.nt.com'
43
43
  parts = Field.decode0(line)
44
44
 
45
- assert_equal(nil, parts[0])
46
- assert_equal("ORGANIZER", parts[1])
47
- assert_equal({ "CN" => [ "xxxx, xxxx [SC100:370:EXCH]" ] }, parts[2])
48
- assert_equal("MAILTO:xxxx@americasm01.nt.com", parts[3])
45
+ assert_equal(nil, parts[1])
46
+ assert_equal("ORGANIZER", parts[2])
47
+ assert_equal({ "CN" => [ "xxxx, xxxx [SC100:370:EXCH]" ] }, parts[3])
48
+ assert_equal("MAILTO:xxxx@americasm01.nt.com", parts[4])
49
49
  end
50
50
 
51
51
  =begin this can not be done :-(
@@ -67,20 +67,21 @@ class FieldTest < Test::Unit::TestCase
67
67
 
68
68
  def test_field0
69
69
  assert_equal("name:", line = Field.encode0(nil, "name"))
70
- assert_equal([ nil, "NAME", {}, ""], Field.decode0(line))
70
+ assert_equal([ true, nil, "NAME", {}, ""], Field.decode0(line))
71
71
 
72
72
  assert_equal("name:value", line = Field.encode0(nil, "name", {}, "value"))
73
- assert_equal([ nil, "NAME", {}, "value"], Field.decode0(line))
73
+ assert_equal([ true, nil, "NAME", {}, "value"], Field.decode0(line))
74
74
 
75
75
  assert_equal("name;encoding=B:dmFsdWU=", line = Field.encode0(nil, "name", { "encoding"=>:b64 }, "value"))
76
- assert_equal([ nil, "NAME", { "ENCODING"=>["B"]}, ["value"].pack("m").chomp ], Field.decode0(line))
76
+ assert_equal([ true, nil, "NAME", { "ENCODING"=>["B"]}, ["value"].pack("m").chomp ], Field.decode0(line))
77
77
 
78
78
  line = Field.encode0("group", "name", {}, "value")
79
79
  assert_equal "group.name:value", line
80
- assert_equal [ "GROUP", "NAME", {}, "value"], Field.decode0(line)
80
+ assert_equal [ true, "GROUP", "NAME", {}, "value"], Field.decode0(line)
81
81
  end
82
82
 
83
- def test_invalid_fields
83
+ def test_invalid_fields_wih_raise_error
84
+ Vcard::configuration.raise_on_invalid_line = true
84
85
  [
85
86
  "g.:",
86
87
  ":v",
@@ -89,6 +90,16 @@ class FieldTest < Test::Unit::TestCase
89
90
  end
90
91
  end
91
92
 
93
+ def test_invalid_fields_wihout_raise_error
94
+ Vcard.configuration.raise_on_invalid_line = false
95
+ [
96
+ "g.:",
97
+ ":v",
98
+ ].each do |line|
99
+ assert_nothing_raised { Field.decode0(line) }
100
+ end
101
+ end
102
+
92
103
  def test_date_encode
93
104
  assert_equal("DTSTART:20040101\n", Field.create("DTSTART", Date.new(2004, 1, 1) ).to_s)
94
105
  assert_equal("DTSTART:20040101\n", Field.create("DTSTART", [Date.new(2004, 1, 1)]).to_s)
@@ -97,6 +108,7 @@ class FieldTest < Test::Unit::TestCase
97
108
  def test_field_modify
98
109
  f = Field.create("name")
99
110
 
111
+
100
112
  assert_equal("", f.value)
101
113
  f.value = ""
102
114
  assert_equal("", f.value)
data/test/vcard_test.rb CHANGED
@@ -169,12 +169,50 @@ EOF
169
169
  end
170
170
 
171
171
  def test_bad
172
- # FIXME: this should THROW, it's badly encoded!
172
+ Vcard::configuration.raise_on_invalid_line = true
173
173
  assert_raises(::Vcard::InvalidEncodingError) do
174
174
  Vcard::Vcard.decode("BEGIN:VCARD\nVERSION:3.0\nKEYencoding=b:this could be \nmy certificate\n\nEND:VCARD\n")
175
175
  end
176
176
  end
177
177
 
178
+ def test_not_raise_error_if_configured_to_ignore
179
+ Vcard::configuration.raise_on_invalid_line = false
180
+ Vcard::configuration.ignore_invalid_vcards = false
181
+ assert_nothing_raised do
182
+ Vcard::Vcard.decode("BEGIN:VCARD\nVERSION:3.0\nKEYencoding=b:this could be \nmy certificate\n\nEND:VCARD\n")
183
+ end
184
+ end
185
+
186
+ def test_ignore_vcards_with_invalid_fields
187
+ Vcard::configuration.raise_on_invalid_line = false
188
+ Vcard::configuration.ignore_invalid_vcards = true
189
+ src = <<'EOF'
190
+ BEGIN:VCARD
191
+ VERSION:3.0
192
+ KEYencoding=b:this could be
193
+ my certificate
194
+ EMAIL:valid@field.value
195
+ END:VCARD
196
+ BEGIN:VCARD
197
+ VERSION:3.0
198
+ EMAIL:valid@field.value
199
+ END:VCARD
200
+ EOF
201
+
202
+ cards = Vcard::Vcard.decode(src)
203
+ assert_equal 1, cards.size
204
+ end
205
+
206
+ def test_ignore_only_invalid_fields
207
+ Vcard::configuration.raise_on_invalid_line = false
208
+ Vcard::configuration.ignore_invalid_vcards = false
209
+ email = 'test@example.com'
210
+ cards = Vcard::Vcard.decode("BEGIN:VCARD\nVERSION:3.0\nKEYencoding=b:this could be \nmy certificate\nEMAIL:#{email}\n\nEND:VCARD\n")
211
+ assert_equal email, cards.first.email
212
+ # [BEGIN, VERSION, EMAIL, END].size == 4
213
+ assert_equal 4, cards.first.fields.size
214
+ end
215
+
178
216
  def test_create
179
217
  card = Vcard::Vcard.create
180
218
  key = Vcard::DirectoryInfo.decode("key;type=x509;encoding=B:dGhpcyBjb3VsZCBiZSAKbXkgY2VydGlmaWNhdGUK\n")['key']
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vcard
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.13
4
+ version: 0.2.14
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kuba Kuźma
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-09-23 00:00:00.000000000 Z
11
+ date: 2016-11-22 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Vcard extracted from Vpim
14
14
  email:
@@ -28,6 +28,7 @@ files:
28
28
  - lib/vcard.rb
29
29
  - lib/vcard/attachment.rb
30
30
  - lib/vcard/bnf.rb
31
+ - lib/vcard/configuration.rb
31
32
  - lib/vcard/dirinfo.rb
32
33
  - lib/vcard/enumerator.rb
33
34
  - lib/vcard/errors.rb