vcard 0.2.12 → 0.3.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
- SHA1:
3
- metadata.gz: 2e912a9dfe65f2058669088fcb34f1be53147564
4
- data.tar.gz: 3aad9b7c03a9f32a2173d3386985d075bb264fe6
2
+ SHA256:
3
+ metadata.gz: a1a9b1a2c5e533fee3acd4659c5baa7234b36eea86da7082f372fd6415d83e31
4
+ data.tar.gz: 245a9b44434e2fdfbb6c69e32edb81aa43864a7f7f74a4504bac53a1c8148fda
5
5
  SHA512:
6
- metadata.gz: dbfb602829bfe1c2de00a99950c8783dd930fcd3de4099b84ab2089dda1b96f80ff2af39646e12f044ef235b248257290a07eafc6b5b042ba7d61129b98186bd
7
- data.tar.gz: 33afb5ec28ca3e61b8077bec041a5b42e0cc850253fbf82238d68a044606cadf5dc3a50dc930dadc2585cddd9949256b3d8dcdcf1d3975ee42a432a04ff5dd8a
6
+ metadata.gz: 56df5e3fbce3b326c4daebd7ab9a8dfc7580fe5bd436c0207cd081dc6aa1f0c37df183dd4289651d49fc15f1d1ef0d7ff8b06b01374a55c8a5d1ea8a83f7ecad
7
+ data.tar.gz: f0fde0a4f77ec00a5eedd124de538c7d0fecd6b9bbcc5d05f96ab1545c9a1de83f8f5c2a9bfa4f9f0475e2e901f395f3868b68b7f715251c8a7ff1ad56d9ea40
@@ -1 +1 @@
1
- ruby-2.0.0-p195
1
+ ruby-2.6.5
@@ -1,16 +1,9 @@
1
1
  language: ruby
2
2
  script: bundle exec rake
3
+ before_install:
4
+ - gem install bundler
3
5
  rvm:
4
- - 1.8.7
5
- - 1.9.2
6
- - 1.9.3
7
- # - ruby-head
8
- # - ree
9
- # - rbx-18mode
10
- # - rbx-19mode
11
- # - jruby-18mode
12
- # - jruby-19mode
13
- # - jruby-head
14
- matrix:
15
- allow_failures:
16
- - rvm: 1.8.7
6
+ - 2.3
7
+ - 2.4
8
+ - 2.5
9
+ - 2.6
@@ -0,0 +1,184 @@
1
+ # Changelog
2
+
3
+ ## [Unreleased](https://github.com/qoobaa/vcard/tree/HEAD)
4
+
5
+ [Full Changelog](https://github.com/qoobaa/vcard/compare/v0.3.0...HEAD)
6
+
7
+ ## [v0.3.0](https://github.com/qoobaa/vcard/tree/v0.3.0) (2020-11-20)
8
+
9
+ **Closed issues:**
10
+
11
+ - New release with ruby25 and ruby26 support [\#30](https://github.com/qoobaa/vcard/issues/30)
12
+
13
+ **Merged pull requests:**
14
+
15
+ - Add an `nl` option to encode\(\) and remove caching [\#35](https://github.com/qoobaa/vcard/pull/35) ([knu](https://github.com/knu))
16
+ - Use the default argument in encode\(\) [\#34](https://github.com/qoobaa/vcard/pull/34) ([knu](https://github.com/knu))
17
+ - Do not modify the fields argument in Vcard::Vcard.create\(\) [\#33](https://github.com/qoobaa/vcard/pull/33) ([knu](https://github.com/knu))
18
+ - Wrap lines exactly by a given width \(75 by default\) [\#32](https://github.com/qoobaa/vcard/pull/32) ([knu](https://github.com/knu))
19
+
20
+ ## [v0.2.16](https://github.com/qoobaa/vcard/tree/v0.2.16) (2019-12-01)
21
+
22
+ [Full Changelog](https://github.com/qoobaa/vcard/compare/v0.2.15...v0.2.16)
23
+
24
+ **Merged pull requests:**
25
+
26
+ - Update supported ruby versions [\#31](https://github.com/qoobaa/vcard/pull/31) ([senzpo](https://github.com/senzpo))
27
+
28
+ ## [v0.2.15](https://github.com/qoobaa/vcard/tree/v0.2.15) (2016-12-01)
29
+
30
+ [Full Changelog](https://github.com/qoobaa/vcard/compare/v0.2.14...v0.2.15)
31
+
32
+ **Merged pull requests:**
33
+
34
+ - Fix incorrect scope of Vcard.configure and Vcard.configuration methods [\#29](https://github.com/qoobaa/vcard/pull/29) ([solenko](https://github.com/solenko))
35
+
36
+ ## [v0.2.14](https://github.com/qoobaa/vcard/tree/v0.2.14) (2016-11-22)
37
+
38
+ [Full Changelog](https://github.com/qoobaa/vcard/compare/v0.2.13...v0.2.14)
39
+
40
+ **Closed issues:**
41
+
42
+ - Method for releasing a new gem version [\#27](https://github.com/qoobaa/vcard/issues/27)
43
+
44
+ **Merged pull requests:**
45
+
46
+ - Allow to ignore invalid fields [\#28](https://github.com/qoobaa/vcard/pull/28) ([solenko](https://github.com/solenko))
47
+
48
+ ## [v0.2.13](https://github.com/qoobaa/vcard/tree/v0.2.13) (2016-09-23)
49
+
50
+ [Full Changelog](https://github.com/qoobaa/vcard/compare/v0.2.12...v0.2.13)
51
+
52
+ **Closed issues:**
53
+
54
+ - vCard v2.1 ADR fields with quoted-printable encoding not decoded [\#22](https://github.com/qoobaa/vcard/issues/22)
55
+ - License missing from gemspec [\#17](https://github.com/qoobaa/vcard/issues/17)
56
+
57
+ **Merged pull requests:**
58
+
59
+ - Fix tests [\#26](https://github.com/qoobaa/vcard/pull/26) ([brendon](https://github.com/brendon))
60
+ - Fix 2.2 2.3 & Remove 1.9.2 [\#25](https://github.com/qoobaa/vcard/pull/25) ([brendon](https://github.com/brendon))
61
+ - Fix travis bundler [\#24](https://github.com/qoobaa/vcard/pull/24) ([brendon](https://github.com/brendon))
62
+ - Add Ruby 2.x for testing [\#23](https://github.com/qoobaa/vcard/pull/23) ([brendon](https://github.com/brendon))
63
+
64
+ ## [v0.2.12](https://github.com/qoobaa/vcard/tree/v0.2.12) (2013-10-30)
65
+
66
+ [Full Changelog](https://github.com/qoobaa/vcard/compare/v0.2.11...v0.2.12)
67
+
68
+ **Merged pull requests:**
69
+
70
+ - Add work-around for JRuby Regexp issue [\#20](https://github.com/qoobaa/vcard/pull/20) ([dgolombek](https://github.com/dgolombek))
71
+ - Change field's to accept symbols as values [\#19](https://github.com/qoobaa/vcard/pull/19) ([dgolombek](https://github.com/dgolombek))
72
+
73
+ ## [v0.2.11](https://github.com/qoobaa/vcard/tree/v0.2.11) (2013-10-02)
74
+
75
+ [Full Changelog](https://github.com/qoobaa/vcard/compare/v0.2.10...v0.2.11)
76
+
77
+ ## [v0.2.10](https://github.com/qoobaa/vcard/tree/v0.2.10) (2013-10-01)
78
+
79
+ [Full Changelog](https://github.com/qoobaa/vcard/compare/v0.2.9...v0.2.10)
80
+
81
+ ## [v0.2.9](https://github.com/qoobaa/vcard/tree/v0.2.9) (2013-09-27)
82
+
83
+ [Full Changelog](https://github.com/qoobaa/vcard/compare/v0.2.8...v0.2.9)
84
+
85
+ **Closed issues:**
86
+
87
+ - Failing Tests [\#11](https://github.com/qoobaa/vcard/issues/11)
88
+
89
+ **Merged pull requests:**
90
+
91
+ - Fix decoding of multi-line quoted-printable fields [\#16](https://github.com/qoobaa/vcard/pull/16) ([dgolombek](https://github.com/dgolombek))
92
+
93
+ ## [v0.2.8](https://github.com/qoobaa/vcard/tree/v0.2.8) (2013-06-20)
94
+
95
+ [Full Changelog](https://github.com/qoobaa/vcard/compare/v0.2.7...v0.2.8)
96
+
97
+ ## [v0.2.7](https://github.com/qoobaa/vcard/tree/v0.2.7) (2013-06-20)
98
+
99
+ [Full Changelog](https://github.com/qoobaa/vcard/compare/v0.2.6...v0.2.7)
100
+
101
+ **Closed issues:**
102
+
103
+ - Encoding line breaks for things like Address and Notes [\#9](https://github.com/qoobaa/vcard/issues/9)
104
+
105
+ **Merged pull requests:**
106
+
107
+ - Add back support for multiple ROLEs [\#15](https://github.com/qoobaa/vcard/pull/15) ([brendon](https://github.com/brendon))
108
+
109
+ ## [v0.2.6](https://github.com/qoobaa/vcard/tree/v0.2.6) (2013-06-19)
110
+
111
+ [Full Changelog](https://github.com/qoobaa/vcard/compare/v0.2.5...v0.2.6)
112
+
113
+ **Merged pull requests:**
114
+
115
+ - Eliminate carriage returns when encoding text [\#14](https://github.com/qoobaa/vcard/pull/14) ([m1foley](https://github.com/m1foley))
116
+
117
+ ## [v0.2.5](https://github.com/qoobaa/vcard/tree/v0.2.5) (2013-06-19)
118
+
119
+ [Full Changelog](https://github.com/qoobaa/vcard/compare/v0.2.4...v0.2.5)
120
+
121
+ **Closed issues:**
122
+
123
+ - Role is missing [\#8](https://github.com/qoobaa/vcard/issues/8)
124
+
125
+ **Merged pull requests:**
126
+
127
+ - Newlines bug [\#13](https://github.com/qoobaa/vcard/pull/13) ([brendon](https://github.com/brendon))
128
+ - ROLE can occur multiple times [\#12](https://github.com/qoobaa/vcard/pull/12) ([brendon](https://github.com/brendon))
129
+
130
+ ## [v0.2.4](https://github.com/qoobaa/vcard/tree/v0.2.4) (2013-06-18)
131
+
132
+ [Full Changelog](https://github.com/qoobaa/vcard/compare/v0.2.3...v0.2.4)
133
+
134
+ **Closed issues:**
135
+
136
+ - Using Ruby 2.0 there is invalid multibyte escape error [\#6](https://github.com/qoobaa/vcard/issues/6)
137
+
138
+ **Merged pull requests:**
139
+
140
+ - Added native support for ROLE with escaping [\#10](https://github.com/qoobaa/vcard/pull/10) ([brendon](https://github.com/brendon))
141
+
142
+ ## [v0.2.3](https://github.com/qoobaa/vcard/tree/v0.2.3) (2013-01-31)
143
+
144
+ [Full Changelog](https://github.com/qoobaa/vcard/compare/v0.2.2...v0.2.3)
145
+
146
+ **Closed issues:**
147
+
148
+ - Birthday decoding broken in 0.2.0 [\#4](https://github.com/qoobaa/vcard/issues/4)
149
+
150
+ **Merged pull requests:**
151
+
152
+ - Code patch to correct invalid multibyte escape [\#7](https://github.com/qoobaa/vcard/pull/7) ([seth-macpherson](https://github.com/seth-macpherson))
153
+
154
+ ## [v0.2.2](https://github.com/qoobaa/vcard/tree/v0.2.2) (2012-12-17)
155
+
156
+ [Full Changelog](https://github.com/qoobaa/vcard/compare/v0.2.1...v0.2.2)
157
+
158
+ **Merged pull requests:**
159
+
160
+ - Regex refactor, includes date parsing fix [\#5](https://github.com/qoobaa/vcard/pull/5) ([m1foley](https://github.com/m1foley))
161
+
162
+ ## [v0.2.1](https://github.com/qoobaa/vcard/tree/v0.2.1) (2012-12-16)
163
+
164
+ [Full Changelog](https://github.com/qoobaa/vcard/compare/v0.2.0...v0.2.1)
165
+
166
+ **Merged pull requests:**
167
+
168
+ - Fix wrong scope, add Travis [\#3](https://github.com/qoobaa/vcard/pull/3) ([rngtng](https://github.com/rngtng))
169
+
170
+ ## [v0.2.0](https://github.com/qoobaa/vcard/tree/v0.2.0) (2012-11-26)
171
+
172
+ [Full Changelog](https://github.com/qoobaa/vcard/compare/46a2517b0f9bd073b00f233eeae6f106069ddce5...v0.2.0)
173
+
174
+ **Closed issues:**
175
+
176
+ - incompatible encoding regexp match \(ASCII-8BIT regexp with UTF-8 string\) [\#1](https://github.com/qoobaa/vcard/issues/1)
177
+
178
+ **Merged pull requests:**
179
+
180
+ - remove superfluous encoding detection, which errors with ruby-1.9 [\#2](https://github.com/qoobaa/vcard/pull/2) ([hannesm](https://github.com/hannesm))
181
+
182
+
183
+
184
+ <!-- This Changelog was originally generated by [github_changelog_generator](https://github.com/github-changelog-generator/github-changelog-generator) -->
data/Gemfile CHANGED
@@ -2,5 +2,5 @@ source "https://rubygems.org"
2
2
 
3
3
  gemspec
4
4
 
5
-
6
5
  gem "rake"
6
+ gem 'test-unit'
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # Vcard
1
+ # Vcard [![Build Status](https://travis-ci.org/qoobaa/vcard.svg?branch=master)](https://travis-ci.org/qoobaa/vcard)
2
2
 
3
3
  Vcard gem extracts Vcard support from Vpim gem.
4
4
 
@@ -15,3 +15,31 @@ And then execute:
15
15
  Or install it yourself as:
16
16
 
17
17
  $ gem install vcard
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
+
43
+ ## Upgrade Notes
44
+
45
+ We are no longer testing against Ruby 1.8.7.
@@ -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"
@@ -26,7 +27,7 @@ module Vcard
26
27
  unfolded = []
27
28
 
28
29
  prior_line = nil
29
- card.lines do |line|
30
+ card.each_line do |line|
30
31
  line.chomp!
31
32
  # If it's a continuation line, add it to the last.
32
33
  # If it's an empty line, drop it from the input.
@@ -285,4 +286,13 @@ module Vcard
285
286
  end
286
287
  return outer, inner
287
288
  end
289
+
290
+ def self.configuration
291
+ @configuration ||= Configuration.new
292
+ end
293
+
294
+ def self.configure
295
+ yield configuration
296
+ end
297
+
288
298
  end
@@ -0,0 +1,26 @@
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
+ set_default_values
12
+ end
13
+
14
+ def reset
15
+ set_default_values
16
+ end
17
+
18
+ private
19
+
20
+ def set_default_values
21
+ @raise_on_invalid_line = true
22
+ @ignore_invalid_vcards = true
23
+ end
24
+
25
+ end
26
+ end
@@ -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
@@ -81,10 +92,11 @@ module Vcard
81
92
 
82
93
  if profile
83
94
  p = profile.to_str
84
- f = [ Field.create("BEGIN", p) ]
85
- f.concat fields
86
- f.push Field.create("END", p)
87
- fields = f
95
+ fields = [
96
+ Field.create("BEGIN", p),
97
+ *fields,
98
+ Field.create("END", p)
99
+ ]
88
100
  end
89
101
 
90
102
  new(fields, profile)
@@ -181,9 +193,8 @@ module Vcard
181
193
  Enumerator.new(self, cond )
182
194
  end
183
195
 
184
- # Force card to be reencoded from the fields.
196
+ # Obsoleted; force card to be reencoded from the fields.
185
197
  def dirty #:nodoc:
186
- #string = nil
187
198
  end
188
199
 
189
200
  # Append +field+ to the fields. Note that it won't be literally appended
@@ -230,13 +241,12 @@ module Vcard
230
241
  self
231
242
  end
232
243
 
244
+ LF= "\n"
245
+
233
246
  # The string encoding of the DirectoryInfo. See Field#encode for information
234
- # about the width parameter.
235
- def encode(width=nil)
236
- unless @string
237
- @string = @fields.collect { |f| f.encode(width) } . join ""
238
- end
239
- @string
247
+ # about the parameters.
248
+ def encode(width = 75, nl: LF)
249
+ @fields.collect { |f| f.encode(width, nl: nl) }.join
240
250
  end
241
251
 
242
252
  alias to_s encode
@@ -249,10 +259,10 @@ module Vcard
249
259
  raise "No fields to check"
250
260
  end
251
261
  unless @fields.first.name? "BEGIN"
252
- raise "Needs BEGIN, found: #{@fields.first.encode nil}"
262
+ raise "Needs BEGIN, found: #{@fields.first.encode}"
253
263
  end
254
264
  unless @fields.last.name? "END"
255
- raise "Needs END, found: #{@fields.last.encode nil}"
265
+ raise "Needs END, found: #{@fields.last.encode}"
256
266
  end
257
267
  unless @fields.last.value? @fields.first.value
258
268
  raise "BEGIN/END mismatch: (#{@fields.first.value} != #{@fields.last.value}"
@@ -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)
@@ -218,9 +228,18 @@ module Vcard
218
228
  Marshal.load(Marshal.dump(self))
219
229
  end
220
230
 
221
- # The String encoding of the Field. The String will be wrapped to a
222
- # maximum line width of +width+, where +0+ means no wrapping, and nil is
223
- # to accept the default wrapping (75, recommended by RFC2425).
231
+ LF = "\n"
232
+
233
+ # The String encoding of the Field. The String will be wrapped
234
+ # to a maximum line width of +width+, where +0+ means no
235
+ # wrapping, and omitting it is to accept the default wrapping
236
+ # (75, recommended by RFC2425).
237
+ #
238
+ # The +nl+ parameter specifies the line delimiter, which is
239
+ # defaulted to LF ("\n") for historical reasons. Relevant RFC's
240
+ # all say it should be CRLF, so it is highly recommended that
241
+ # you specify "\r\n" if you care about maximizing
242
+ # interoperability and interchangeability.
224
243
  #
225
244
  # Note: AddressBook.app 3.0.3 neither understands to unwrap lines when it
226
245
  # imports vCards (it treats them as raw new-line characters), nor wraps
@@ -229,15 +248,16 @@ module Vcard
229
248
  #
230
249
  # FIXME - breaks round-trip encoding, need to change this to not wrap
231
250
  # fields that are already wrapped.
232
- def encode(width=nil)
233
- width = 75 unless width
234
- l = @line
235
- # Wrap to width, unless width is zero.
236
- if width > 0
237
- l = l.gsub(/.{#{width},#{width}}/) { |m| m + "\n " }
251
+ def encode(width = 75, nl: LF)
252
+ l = @line.rstrip
253
+ if width.zero?
254
+ l + nl
255
+ elsif width <= 1
256
+ raise ArgumentError, "#{width} is too narrow"
257
+ else
258
+ # Wrap to width
259
+ l.scan(/\A.{,#{width}}|.{1,#{width - 1}}/).join("#{nl} ") + nl
238
260
  end
239
- # Make sure it's terminated with no more than a single NL.
240
- l.gsub(/\s*\z/, "") + "\n"
241
261
  end
242
262
 
243
263
  alias to_s encode
@@ -323,7 +343,7 @@ module Vcard
323
343
 
324
344
  # Is the #name of this Field +name+? Names are case insensitive.
325
345
  def name?(name)
326
- @name.casecmp(name) == 0
346
+ @name.to_s.casecmp(name) == 0
327
347
  end
328
348
 
329
349
  # Is the #group of this field +group+? Group names are case insensitive.
@@ -590,7 +610,7 @@ module Vcard
590
610
  # new fields, not old fields.
591
611
  def mutate(g, n, p, v) #:nodoc:
592
612
  line = Field.encode0(g, n, p, v)
593
- @group, @name, @params, @value = Field.decode0(line)
613
+ @valid, @group, @name, @params, @value = Field.decode0(line)
594
614
  @line = line
595
615
  self
596
616
  rescue ::Vcard::InvalidEncodingError => e
@@ -608,9 +608,8 @@ module Vcard
608
608
 
609
609
  # Create a vCard 3.0 object with the minimum required fields, plus any
610
610
  # +fields+ you want in the card (they can also be added later).
611
- def self.create(fields = [] )
612
- fields.unshift Field.create("VERSION", "3.0")
613
- super(fields, "VCARD")
611
+ def self.create(fields = [])
612
+ super([Field.create("VERSION", "3.0"), *fields], "VCARD")
614
613
  end
615
614
 
616
615
  # Decode a collection of vCards into an array of Vcard objects.
@@ -659,7 +658,8 @@ module Vcard
659
658
  vcards = []
660
659
 
661
660
  for e in entities
662
- vcards.push(new(e.flatten, "VCARD"))
661
+ vcard = new(e.flatten, "VCARD")
662
+ vcards.push(vcard) if vcard.valid? || !::Vcard.configuration.ignore_invalid_vcards?
663
663
  end
664
664
 
665
665
  vcards
@@ -1,3 +1,3 @@
1
1
  module Vcard
2
- VERSION = "0.2.12"
2
+ VERSION = "0.3.0"
3
3
  end
@@ -0,0 +1,23 @@
1
+ require "test_helper"
2
+
3
+ class ConfigurationTest < Test::Unit::TestCase
4
+ def test_should_be_an_instance_of_configuration
5
+ assert Vcard.configuration.is_a?(::Vcard::Configuration)
6
+ end
7
+
8
+ def test_have_default_values
9
+ Vcard.configuration.reset
10
+ assert_equal(Vcard.configuration.raise_on_invalid_line, true)
11
+ assert_equal(Vcard.configuration.ignore_invalid_vcards, true)
12
+ end
13
+
14
+ def test_allow_configuration_with_block
15
+ Vcard.configuration.reset
16
+ Vcard.configure do |config|
17
+ config.raise_on_invalid_line = false
18
+ config.ignore_invalid_vcards = false
19
+ end
20
+ assert_equal(Vcard.configuration.raise_on_invalid_line, false)
21
+ assert_equal(Vcard.configuration.ignore_invalid_vcards, false)
22
+ end
23
+ end
@@ -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)
@@ -150,8 +162,14 @@ class FieldTest < Test::Unit::TestCase
150
162
  assert_equal("0:xx\n", Vcard::DirectoryInfo::Field.create("0", "x" * 2).encode(4))
151
163
  assert_equal("0:xx\n x\n", Vcard::DirectoryInfo::Field.create("0", "x" * 3).encode(4))
152
164
  assert_equal("0:xx\n xx\n", Vcard::DirectoryInfo::Field.create("0", "x" * 4).encode(4))
153
- assert_equal("0:xx\n xxxx\n", Vcard::DirectoryInfo::Field.create("0", "x" * 6).encode(4))
154
- assert_equal("0:xx\n xxxx\n x\n", Vcard::DirectoryInfo::Field.create("0", "x" * 7).encode(4))
165
+ assert_equal("0:xx\n xxx\n x\n", Vcard::DirectoryInfo::Field.create("0", "x" * 6).encode(4))
166
+ assert_equal("0:xx\n xxx\n xx\n", Vcard::DirectoryInfo::Field.create("0", "x" * 7).encode(4))
167
+ assert_equal("0:xxxxxxx\n", Vcard::DirectoryInfo::Field.create("0", "x" * 7).encode(0))
168
+ assert_equal("0:xxxxxxx\n", Vcard::DirectoryInfo::Field.create("0", "x" * 7).encode())
169
+ assert_equal("0:#{"x" * 73}\n #{"x" * 74}\n #{"x" * 53}\n", Vcard::DirectoryInfo::Field.create("0", "x" * 200).encode())
155
170
  end
156
- end
157
171
 
172
+ def test_nl
173
+ assert_equal("test:#{"value" * 14}\r\n #{"value" * 6}\r\n", Vcard::DirectoryInfo::Field.create("test", "value" * 20).encode(nl: "\r\n"))
174
+ end
175
+ end
@@ -97,6 +97,20 @@ class VcardTest < Test::Unit::TestCase
97
97
  assert_equal("CATEGORIES: Amis/Famille", card[ "note" ])
98
98
  end
99
99
 
100
+ def test_nl
101
+ lf_card = crlf_card = nil
102
+ assert_nothing_thrown {
103
+ lf_card = Vcard::Vcard.decode(vcard(:ex3)).first
104
+ crlf_encoded = lf_card.encode(nl: "\r\n")
105
+ lf_encoded = lf_card.encode
106
+ assert_equal(crlf_encoded.split(/\r\n/), lf_encoded.split(/\n/))
107
+ crlf_card = Vcard::Vcard.decode(crlf_encoded).first
108
+ }
109
+
110
+ assert_equal(lf_card["note"], crlf_card["note"])
111
+ assert_equal(lf_card["home.label"], crlf_card["home.label"])
112
+ end
113
+
100
114
  def test_nickname
101
115
  assert_equal(nil, Vcard::Vcard.decode(vcard(:nickname0)).first.nickname)
102
116
  assert_equal(nil, Vcard::Vcard.decode(vcard(:nickname1)).first.nickname)
@@ -169,17 +183,61 @@ EOF
169
183
  end
170
184
 
171
185
  def test_bad
172
- # FIXME: this should THROW, it's badly encoded!
186
+ Vcard::configuration.raise_on_invalid_line = true
173
187
  assert_raises(::Vcard::InvalidEncodingError) do
174
188
  Vcard::Vcard.decode("BEGIN:VCARD\nVERSION:3.0\nKEYencoding=b:this could be \nmy certificate\n\nEND:VCARD\n")
175
189
  end
176
190
  end
177
191
 
192
+ def test_not_raise_error_if_configured_to_ignore
193
+ Vcard::configuration.raise_on_invalid_line = false
194
+ Vcard::configuration.ignore_invalid_vcards = false
195
+ assert_nothing_raised do
196
+ Vcard::Vcard.decode("BEGIN:VCARD\nVERSION:3.0\nKEYencoding=b:this could be \nmy certificate\n\nEND:VCARD\n")
197
+ end
198
+ end
199
+
200
+ def test_ignore_vcards_with_invalid_fields
201
+ Vcard::configuration.raise_on_invalid_line = false
202
+ Vcard::configuration.ignore_invalid_vcards = true
203
+ src = <<'EOF'
204
+ BEGIN:VCARD
205
+ VERSION:3.0
206
+ KEYencoding=b:this could be
207
+ my certificate
208
+ EMAIL:valid@field.value
209
+ END:VCARD
210
+ BEGIN:VCARD
211
+ VERSION:3.0
212
+ EMAIL:valid@field.value
213
+ END:VCARD
214
+ EOF
215
+
216
+ cards = Vcard::Vcard.decode(src)
217
+ assert_equal 1, cards.size
218
+ end
219
+
220
+ def test_ignore_only_invalid_fields
221
+ Vcard::configuration.raise_on_invalid_line = false
222
+ Vcard::configuration.ignore_invalid_vcards = false
223
+ email = 'test@example.com'
224
+ cards = Vcard::Vcard.decode("BEGIN:VCARD\nVERSION:3.0\nKEYencoding=b:this could be \nmy certificate\nEMAIL:#{email}\n\nEND:VCARD\n")
225
+ assert_equal email, cards.first.email
226
+ # [BEGIN, VERSION, EMAIL, END].size == 4
227
+ assert_equal 4, cards.first.fields.size
228
+ end
229
+
178
230
  def test_create
179
231
  card = Vcard::Vcard.create
180
232
  key = Vcard::DirectoryInfo.decode("key;type=x509;encoding=B:dGhpcyBjb3VsZCBiZSAKbXkgY2VydGlmaWNhdGUK\n")['key']
181
233
  card << Vcard::DirectoryInfo::Field.create('key', key, 'encoding' => :b64)
182
234
  assert_equal(key, card['key'])
235
+
236
+ field = Vcard::DirectoryInfo::Field.create('key', 'value')
237
+ card = assert_nothing_raised {
238
+ Vcard::Vcard.create([field].freeze)
239
+ }
240
+ assert_equal('value', card['key'])
183
241
  end
184
242
 
185
243
  def test_decode_date
@@ -318,7 +376,7 @@ EOF
318
376
  assert_equal('', card.name.given)
319
377
  assert_equal('', card.name.fullname)
320
378
 
321
- assert_raises(TypeError, RuntimeError) do
379
+ assert_raises do
322
380
  card.name.given = 'given'
323
381
  end
324
382
 
@@ -466,7 +524,7 @@ EOF
466
524
  m.add_role "Office Manager\r\n;Something Else"
467
525
  end
468
526
  assert_equal "Office Manager\n;Something Else", card.role
469
- assert_match /Office Manager\\n\\;Something Else/, card.to_s
527
+ assert_match(/Office Manager\\n\\;Something Else/, card.to_s)
470
528
  card = Vcard::Vcard.decode(card.encode).first
471
529
  assert_equal "Office Manager\n;Something Else", card.role
472
530
  end
@@ -477,10 +535,10 @@ EOF
477
535
  n.given = "John"
478
536
  n.family = "Woe"
479
537
  end
480
- m.add_note note = "line1\r\n;line2"
538
+ m.add_note "line1\r\n;line2"
481
539
  end
482
540
  assert_equal "line1\n;line2", card.note
483
- assert_match /line1\\n\\;line2/, card.to_s
541
+ assert_match(/line1\\n\\;line2/, card.to_s)
484
542
  card = Vcard::Vcard.decode(card.encode).first
485
543
  assert_equal "line1\n;line2", card.note
486
544
  end
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.12
4
+ version: 0.3.0
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: 2013-10-30 00:00:00.000000000 Z
11
+ date: 2020-11-19 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Vcard extracted from Vpim
14
14
  email:
@@ -17,10 +17,11 @@ executables: []
17
17
  extensions: []
18
18
  extra_rdoc_files: []
19
19
  files:
20
- - .gitignore
21
- - .ruby-gemset
22
- - .ruby-version
23
- - .travis.yml
20
+ - ".gitignore"
21
+ - ".ruby-gemset"
22
+ - ".ruby-version"
23
+ - ".travis.yml"
24
+ - CHANGELOG.md
24
25
  - Gemfile
25
26
  - LICENSE
26
27
  - README.md
@@ -28,12 +29,14 @@ files:
28
29
  - lib/vcard.rb
29
30
  - lib/vcard/attachment.rb
30
31
  - lib/vcard/bnf.rb
32
+ - lib/vcard/configuration.rb
31
33
  - lib/vcard/dirinfo.rb
32
34
  - lib/vcard/enumerator.rb
33
35
  - lib/vcard/errors.rb
34
36
  - lib/vcard/field.rb
35
37
  - lib/vcard/vcard.rb
36
38
  - lib/vcard/version.rb
39
+ - test/configuration_test.rb
37
40
  - test/field_test.rb
38
41
  - test/fixtures/bday_decode.vcard
39
42
  - test/fixtures/bday_decode_2.vcard
@@ -74,21 +77,21 @@ require_paths:
74
77
  - lib
75
78
  required_ruby_version: !ruby/object:Gem::Requirement
76
79
  requirements:
77
- - - '>='
80
+ - - ">="
78
81
  - !ruby/object:Gem::Version
79
82
  version: '0'
80
83
  required_rubygems_version: !ruby/object:Gem::Requirement
81
84
  requirements:
82
- - - '>='
85
+ - - ">="
83
86
  - !ruby/object:Gem::Version
84
87
  version: '0'
85
88
  requirements: []
86
- rubyforge_project:
87
- rubygems_version: 2.0.3
89
+ rubygems_version: 3.0.3
88
90
  signing_key:
89
91
  specification_version: 4
90
92
  summary: Vcard extracted from Vpim
91
93
  test_files:
94
+ - test/configuration_test.rb
92
95
  - test/field_test.rb
93
96
  - test/fixtures/bday_decode.vcard
94
97
  - test/fixtures/bday_decode_2.vcard