valerie 0.0.8 → 1.0.0
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 +4 -4
- data/README.md +30 -13
- data/Rakefile +24 -0
- data/lib/valerie/address.rb +47 -6
- data/lib/valerie/card.rb +134 -17
- data/lib/valerie/collection/address_collection.rb +37 -5
- data/lib/valerie/collection/email_collection.rb +22 -2
- data/lib/valerie/collection/phone_collection.rb +21 -2
- data/lib/valerie/core/parser.rb +37 -14
- data/lib/valerie/email.rb +40 -13
- data/lib/valerie/name.rb +40 -6
- data/lib/valerie/phone.rb +45 -1
- data/lib/valerie.rb +43 -7
- data/test/address_test.rb +93 -0
- data/test/card_test.rb +129 -16
- data/test/collection/address_collection_test.rb +97 -0
- data/test/email_test.rb +4 -4
- data/test/phone_test.rb +1 -1
- metadata +19 -3
data/test/card_test.rb
CHANGED
|
@@ -1,73 +1,186 @@
|
|
|
1
1
|
require_relative 'test_helper'
|
|
2
|
+
require 'date'
|
|
2
3
|
|
|
3
4
|
class VCardTest < Minitest::Test
|
|
4
5
|
def initialize(name)
|
|
5
6
|
super
|
|
6
7
|
@card = Valerie::Card.new
|
|
7
8
|
end
|
|
8
|
-
|
|
9
|
+
|
|
9
10
|
def test_setting_name_with_hash_parameter
|
|
10
11
|
@card.name = { first_name: 'John', last_name: 'Doe' }
|
|
11
|
-
|
|
12
|
+
|
|
12
13
|
assert_equal(@card.name.first_name, 'John')
|
|
13
14
|
assert_equal(@card.name.last_name, 'Doe')
|
|
14
15
|
end
|
|
15
|
-
|
|
16
|
+
|
|
16
17
|
def test_setting_name_with_string_parameter
|
|
17
18
|
@card.name = 'Adam Hull'
|
|
18
|
-
|
|
19
|
+
|
|
19
20
|
assert_equal(@card.name.first_name, 'Adam')
|
|
20
21
|
assert_equal(@card.name.last_name, 'Hull')
|
|
21
22
|
end
|
|
22
|
-
|
|
23
|
+
|
|
23
24
|
def test_setting_organization_with_hash
|
|
24
25
|
@card.organization = { name: 'Hellotext', department: 'Product Development' }
|
|
25
|
-
|
|
26
|
+
|
|
26
27
|
assert_equal(@card.organization.name, 'Hellotext')
|
|
27
28
|
assert_equal(@card.organization.department, 'Product Development')
|
|
28
29
|
end
|
|
29
|
-
|
|
30
|
+
|
|
30
31
|
def test_setting_organization_with_string
|
|
31
32
|
@card.organization = ['Hellotext', 'Product Development']
|
|
32
|
-
|
|
33
|
+
|
|
33
34
|
assert_equal(@card.organization.name, 'Hellotext')
|
|
34
35
|
assert_equal(@card.organization.department, 'Product Development')
|
|
35
36
|
end
|
|
36
|
-
|
|
37
|
+
|
|
37
38
|
def test_setting_gender_with_valid_identifier
|
|
38
39
|
assert_runs_without_errors do
|
|
39
40
|
@card.gender = 'Male'
|
|
40
41
|
assert_equal(@card.gender.sex, :male)
|
|
41
42
|
end
|
|
42
43
|
end
|
|
43
|
-
|
|
44
|
+
|
|
44
45
|
def test_setting_gender_with_invalid_identifier
|
|
45
46
|
assert_raises(ArgumentError) do
|
|
46
47
|
@card.gender = 'invalid-identifier'
|
|
47
48
|
end
|
|
48
49
|
end
|
|
49
|
-
|
|
50
|
+
|
|
50
51
|
def test_parsing_array
|
|
51
52
|
data = ['PRODID:-//Hellotext', 'N:Doe;John;;;', "ORG:HelloText;", "TEL;type=CELL:+598 94 000 000"]
|
|
52
|
-
|
|
53
|
+
|
|
53
54
|
Valerie::Card.parse(data).tap do |vcard|
|
|
54
55
|
assert_equal(vcard.name.first_name, 'John')
|
|
55
56
|
assert_equal(vcard.name.last_name, 'Doe')
|
|
56
|
-
|
|
57
|
+
|
|
57
58
|
assert_equal(vcard.organization.name, 'HelloText')
|
|
58
|
-
|
|
59
|
+
|
|
59
60
|
assert_equal(vcard.phones.first.number, '+598 94 000 000')
|
|
60
61
|
assert_equal(vcard.phones.first.options[:type], 'cell')
|
|
61
62
|
end
|
|
62
63
|
end
|
|
63
|
-
|
|
64
|
+
|
|
64
65
|
def test_parsing_string
|
|
65
66
|
data = "BEGIN:VCARD\r\nVERSION:3.0\r\nPRODID:-//Hellotext www.hellotext.com//EN\r\nN:Rosenbaum;Shira;;;\r\nTEL;:+598 94 987 924\r\nEND:VCARD"
|
|
66
|
-
|
|
67
|
+
|
|
67
68
|
Valerie::Card.parse(data).first.tap do |vcard|
|
|
68
69
|
assert_equal(vcard.name.first_name, 'Shira')
|
|
69
70
|
assert_equal(vcard.name.last_name, 'Rosenbaum')
|
|
70
71
|
assert_equal(vcard.phones.first.number, '+598 94 987 924')
|
|
71
72
|
end
|
|
72
73
|
end
|
|
74
|
+
|
|
75
|
+
def test_formatted_name_auto_generation
|
|
76
|
+
@card.name = { first_name: 'Jane', last_name: 'Smith' }
|
|
77
|
+
|
|
78
|
+
assert_equal(@card.formatted_name, 'Jane Smith')
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
def test_formatted_name_setter
|
|
82
|
+
@card.formatted_name = 'Dr. John Doe Jr.'
|
|
83
|
+
|
|
84
|
+
assert_equal(@card.formatted_name, 'Dr. John Doe Jr.')
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def test_formatted_name_in_output
|
|
88
|
+
@card.name = { first_name: 'John', last_name: 'Doe' }
|
|
89
|
+
@card.formatted_name = 'Dr. John Doe Jr.'
|
|
90
|
+
|
|
91
|
+
output = @card.to_s
|
|
92
|
+
assert_equal(output.include?('FN:Dr. John Doe Jr.'), true)
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
def test_formatted_name_auto_generation_in_output
|
|
96
|
+
@card.name = { first_name: 'Jane', last_name: 'Smith' }
|
|
97
|
+
|
|
98
|
+
output = @card.to_s
|
|
99
|
+
assert_equal(output.include?('FN:Jane Smith'), true)
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
def test_parsing_formatted_name
|
|
103
|
+
data = "BEGIN:VCARD\r\nVERSION:3.0\r\nN:Doe;John;;;\r\nFN:Dr. John Doe Jr.\r\nEND:VCARD"
|
|
104
|
+
|
|
105
|
+
vcard = Valerie::Card.parse(data).first
|
|
106
|
+
assert_equal(vcard.formatted_name, 'Dr. John Doe Jr.')
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
def test_parsing_address_from_string
|
|
110
|
+
data = "BEGIN:VCARD\r\nVERSION:3.0\r\nN:Smith;Jane;;;\r\nADR;TYPE=work:;;123 Main St;New York;NY;10001;USA\r\nEND:VCARD"
|
|
111
|
+
|
|
112
|
+
vcard = Valerie::Card.parse(data).first
|
|
113
|
+
assert_equal(vcard.addresses.count, 1)
|
|
114
|
+
assert_equal(vcard.addresses.first.to_h[:street_address], '123 Main St')
|
|
115
|
+
assert_equal(vcard.addresses.first.to_h[:locality], 'New York')
|
|
116
|
+
assert_equal(vcard.addresses.first.to_h[:region], 'NY')
|
|
117
|
+
assert_equal(vcard.addresses.first.to_h[:postal_code], '10001')
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
def test_complete_vcard_generation
|
|
121
|
+
@card.name = { first_name: 'John', last_name: 'Doe', middle_name: 'Michael' }
|
|
122
|
+
@card.formatted_name = 'John Michael Doe'
|
|
123
|
+
@card.organization = { name: 'Acme Corp', department: 'Engineering' }
|
|
124
|
+
@card.birthday = Date.new(1990, 5, 15)
|
|
125
|
+
@card.gender = 'male'
|
|
126
|
+
|
|
127
|
+
@card.emails.add('john@example.com', type: 'work')
|
|
128
|
+
@card.emails.add('john.doe@personal.com', type: 'home', position: 2)
|
|
129
|
+
|
|
130
|
+
@card.phones.add('+1-555-123-4567', type: 'work')
|
|
131
|
+
@card.phones.add('+1-555-987-6543', type: 'cell', position: 2)
|
|
132
|
+
|
|
133
|
+
@card.addresses.add(
|
|
134
|
+
post_office_box: '',
|
|
135
|
+
extended_address: 'Suite 100',
|
|
136
|
+
street_address: '123 Business Ave',
|
|
137
|
+
locality: 'New York',
|
|
138
|
+
region: 'NY',
|
|
139
|
+
postal_code: '10001',
|
|
140
|
+
country: 'USA'
|
|
141
|
+
)
|
|
142
|
+
|
|
143
|
+
output = @card.to_s
|
|
144
|
+
|
|
145
|
+
# Verify all fields are present
|
|
146
|
+
assert_equal(output.include?('N:Doe;John;Michael;;'), true)
|
|
147
|
+
assert_equal(output.include?('FN:John Michael Doe'), true)
|
|
148
|
+
assert_equal(output.include?('ORG:Acme Corp;Engineering'), true)
|
|
149
|
+
assert_equal(output.include?('BDAY:1990-05-15'), true)
|
|
150
|
+
assert_equal(output.include?('GENDER:M'), true)
|
|
151
|
+
assert_equal(output.include?('EMAIL'), true)
|
|
152
|
+
assert_equal(output.include?('TEL'), true)
|
|
153
|
+
assert_equal(output.include?('ADR'), true)
|
|
154
|
+
assert_equal(output.include?('123 Business Ave'), true)
|
|
155
|
+
end
|
|
156
|
+
|
|
157
|
+
def test_parsing_complete_vcard
|
|
158
|
+
data = <<~VCARD.gsub("\n", "\r\n")
|
|
159
|
+
BEGIN:VCARD
|
|
160
|
+
VERSION:3.0
|
|
161
|
+
PRODID:-//Valerie www.hellotext.com//EN
|
|
162
|
+
N:Doe;John;Michael;;
|
|
163
|
+
FN:John Michael Doe
|
|
164
|
+
ORG:Acme Corp;Engineering
|
|
165
|
+
BDAY:1990-05-15
|
|
166
|
+
GENDER:M
|
|
167
|
+
EMAIL;PREF=1;TYPE=work:john@example.com
|
|
168
|
+
TEL;PREF=1;TYPE=work:+1-555-123-4567
|
|
169
|
+
ADR;TYPE=work:;Suite 100;123 Business Ave;New York;NY;10001;USA
|
|
170
|
+
END:VCARD
|
|
171
|
+
VCARD
|
|
172
|
+
|
|
173
|
+
vcard = Valerie::Card.parse(data).first
|
|
174
|
+
|
|
175
|
+
assert_equal(vcard.name.first_name, 'John')
|
|
176
|
+
assert_equal(vcard.name.last_name, 'Doe')
|
|
177
|
+
assert_equal(vcard.formatted_name, 'John Michael Doe')
|
|
178
|
+
assert_equal(vcard.organization.name, 'Acme Corp')
|
|
179
|
+
assert_equal(vcard.organization.department, 'Engineering')
|
|
180
|
+
assert_equal(vcard.birthday.to_date, Date.new(1990, 5, 15))
|
|
181
|
+
assert_equal(vcard.gender.sex, :male)
|
|
182
|
+
assert_equal(vcard.emails.count, 1)
|
|
183
|
+
assert_equal(vcard.phones.count, 1)
|
|
184
|
+
assert_equal(vcard.addresses.count, 1)
|
|
185
|
+
end
|
|
73
186
|
end
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
require_relative '../test_helper'
|
|
2
|
+
|
|
3
|
+
class AddressCollectionTest < Minitest::Test
|
|
4
|
+
def setup
|
|
5
|
+
@collection = Valerie::Collection::AddressCollection.new
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
def test_add_address_as_hash
|
|
9
|
+
address = @collection.add(
|
|
10
|
+
post_office_box: '',
|
|
11
|
+
extended_address: '',
|
|
12
|
+
street_address: '123 Main St',
|
|
13
|
+
locality: 'New York',
|
|
14
|
+
region: 'NY',
|
|
15
|
+
postal_code: '10001',
|
|
16
|
+
country: 'USA'
|
|
17
|
+
)
|
|
18
|
+
|
|
19
|
+
assert_instance_of(Valerie::Address, address)
|
|
20
|
+
assert_equal(address.to_h[:street_address], '123 Main St')
|
|
21
|
+
assert_equal(address.to_h[:locality], 'New York')
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def test_add_address_as_address_object
|
|
25
|
+
address = Valerie::Address.new(
|
|
26
|
+
post_office_box: '',
|
|
27
|
+
extended_address: '',
|
|
28
|
+
street_address: '456 Oak Ave',
|
|
29
|
+
locality: 'Los Angeles',
|
|
30
|
+
region: 'CA',
|
|
31
|
+
postal_code: '90001',
|
|
32
|
+
country: 'USA'
|
|
33
|
+
)
|
|
34
|
+
|
|
35
|
+
added = @collection.add(address)
|
|
36
|
+
|
|
37
|
+
assert_equal(added.to_h[:street_address], '456 Oak Ave')
|
|
38
|
+
assert_equal(@collection.count, 1)
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def test_add_multiple_addresses_with_positions
|
|
42
|
+
# Add first address without position (gets position 1)
|
|
43
|
+
@collection.add(
|
|
44
|
+
post_office_box: '',
|
|
45
|
+
extended_address: '',
|
|
46
|
+
street_address: '456 Oak Ave',
|
|
47
|
+
locality: 'Los Angeles',
|
|
48
|
+
region: 'CA',
|
|
49
|
+
postal_code: '90001',
|
|
50
|
+
country: 'USA',
|
|
51
|
+
position: 2
|
|
52
|
+
)
|
|
53
|
+
|
|
54
|
+
# Add second address with position 1 (should be first)
|
|
55
|
+
@collection.add(
|
|
56
|
+
post_office_box: '',
|
|
57
|
+
extended_address: '',
|
|
58
|
+
street_address: '123 Main St',
|
|
59
|
+
locality: 'New York',
|
|
60
|
+
region: 'NY',
|
|
61
|
+
postal_code: '10001',
|
|
62
|
+
country: 'USA',
|
|
63
|
+
position: 1
|
|
64
|
+
)
|
|
65
|
+
|
|
66
|
+
assert_equal(@collection.count, 2)
|
|
67
|
+
# Second address should be first due to position: 1
|
|
68
|
+
assert_equal(@collection.first.to_h[:street_address], '123 Main St')
|
|
69
|
+
assert_equal(@collection.first.position, 1)
|
|
70
|
+
assert_equal(@collection.to_a.last.to_h[:street_address], '456 Oak Ave')
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def test_collection_enumerable
|
|
74
|
+
@collection.add(
|
|
75
|
+
post_office_box: '',
|
|
76
|
+
extended_address: '',
|
|
77
|
+
street_address: '123 Main St',
|
|
78
|
+
locality: 'New York',
|
|
79
|
+
region: 'NY',
|
|
80
|
+
postal_code: '10001',
|
|
81
|
+
country: 'USA'
|
|
82
|
+
)
|
|
83
|
+
|
|
84
|
+
@collection.add(
|
|
85
|
+
post_office_box: '',
|
|
86
|
+
extended_address: '',
|
|
87
|
+
street_address: '456 Oak Ave',
|
|
88
|
+
locality: 'Los Angeles',
|
|
89
|
+
region: 'CA',
|
|
90
|
+
postal_code: '90001',
|
|
91
|
+
country: 'USA'
|
|
92
|
+
)
|
|
93
|
+
|
|
94
|
+
cities = @collection.map { |addr| addr.to_h[:locality] }
|
|
95
|
+
assert_equal(cities, ['New York', 'Los Angeles'])
|
|
96
|
+
end
|
|
97
|
+
end
|
data/test/email_test.rb
CHANGED
|
@@ -6,19 +6,19 @@ class EmailTest < Minitest::Test
|
|
|
6
6
|
assert_equal(email.to_s.start_with?('EMAIL;TYPE=work:'), true)
|
|
7
7
|
end
|
|
8
8
|
end
|
|
9
|
-
|
|
9
|
+
|
|
10
10
|
def test_with_invalid_position
|
|
11
11
|
error = assert_raises(ArgumentError) do
|
|
12
12
|
Valerie::Email.new(address: 'ahmed@hellotext.com', position: -1)
|
|
13
13
|
end
|
|
14
|
-
|
|
14
|
+
|
|
15
15
|
assert_equal(error.message, 'Invalid Position')
|
|
16
16
|
end
|
|
17
|
-
|
|
17
|
+
|
|
18
18
|
def test_to_s_with_preferred
|
|
19
19
|
Valerie::Email.new(address: 'ahmed@hellotext.com', type: :work, position: 1).tap do |email|
|
|
20
20
|
assert_equal(
|
|
21
|
-
email.to_s.include?(';
|
|
21
|
+
email.to_s.include?(';PREF=1;'),
|
|
22
22
|
true
|
|
23
23
|
)
|
|
24
24
|
end
|
data/test/phone_test.rb
CHANGED
|
@@ -23,7 +23,7 @@ class PhoneTest < Minitest::Test
|
|
|
23
23
|
|
|
24
24
|
def test_to_s_with_position
|
|
25
25
|
Valerie::Phone.new('01000000000', type: :voice, position: 1).tap do |phone|
|
|
26
|
-
assert_equal(phone.to_s, 'TEL;
|
|
26
|
+
assert_equal(phone.to_s, 'TEL;PREF=1;TYPE=voice:01000000000')
|
|
27
27
|
end
|
|
28
28
|
end
|
|
29
29
|
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: valerie
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.0
|
|
4
|
+
version: 1.0.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Hellotext
|
|
8
8
|
- Ahmed Khattab
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date:
|
|
11
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: minitest
|
|
@@ -38,6 +38,20 @@ dependencies:
|
|
|
38
38
|
- - "~>"
|
|
39
39
|
- !ruby/object:Gem::Version
|
|
40
40
|
version: '13.0'
|
|
41
|
+
- !ruby/object:Gem::Dependency
|
|
42
|
+
name: yard
|
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
|
44
|
+
requirements:
|
|
45
|
+
- - "~>"
|
|
46
|
+
- !ruby/object:Gem::Version
|
|
47
|
+
version: '0.9'
|
|
48
|
+
type: :development
|
|
49
|
+
prerelease: false
|
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
51
|
+
requirements:
|
|
52
|
+
- - "~>"
|
|
53
|
+
- !ruby/object:Gem::Version
|
|
54
|
+
version: '0.9'
|
|
41
55
|
description: VCard (Contact Card) parser and generator.
|
|
42
56
|
executables: []
|
|
43
57
|
extensions: []
|
|
@@ -60,8 +74,10 @@ files:
|
|
|
60
74
|
- lib/valerie/ordered.rb
|
|
61
75
|
- lib/valerie/organization.rb
|
|
62
76
|
- lib/valerie/phone.rb
|
|
77
|
+
- test/address_test.rb
|
|
63
78
|
- test/birthday_test.rb
|
|
64
79
|
- test/card_test.rb
|
|
80
|
+
- test/collection/address_collection_test.rb
|
|
65
81
|
- test/collection/email_collection_test.rb
|
|
66
82
|
- test/collection/phone_collection_test.rb
|
|
67
83
|
- test/email_test.rb
|
|
@@ -88,7 +104,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
88
104
|
- !ruby/object:Gem::Version
|
|
89
105
|
version: '0'
|
|
90
106
|
requirements: []
|
|
91
|
-
rubygems_version: 3.6.
|
|
107
|
+
rubygems_version: 3.6.7
|
|
92
108
|
specification_version: 4
|
|
93
109
|
summary: Easily parse and generate VCard (Contact Card) objects that can be exported
|
|
94
110
|
to other systems with ease.
|