protocol-caldav 1.0.2 → 1.1.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.
@@ -1,8 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "bundler/setup"
4
- require "scampi"
5
-
6
3
  require "protocol/caldav"
7
4
 
8
5
  module Protocol
@@ -12,77 +9,77 @@ module Protocol
12
9
  module_function
13
10
 
14
11
  def parse(text)
15
- return nil if text.nil? || text.strip.empty?
16
-
17
- text = text.sub(/\A\xEF\xBB\xBF/, '')
18
-
19
- lines = ContentLine.unfold(text).split("\n").map(&:strip).reject(&:empty?)
20
- props = []
21
- inside = false
22
-
23
- lines.each do |line|
24
- parsed = ContentLine.parse_line(line)
25
- next unless parsed
26
-
27
- name, params, value = parsed
28
-
29
- if name.casecmp?('BEGIN') && value.strip.casecmp?('VCARD')
30
- inside = true
31
- elsif name.casecmp?('END') && value.strip.casecmp?('VCARD')
32
- break
33
- elsif inside
34
- props << Ical::Property.new(name: name, params: params, value: value)
12
+ if text.nil? || text.strip.empty?
13
+ nil
14
+ else
15
+ text = text.sub(/\A\xEF\xBB\xBF/, '')
16
+ lines = ContentLine.unfold(text).split("\n").map(&:strip).reject(&:empty?)
17
+ props = []
18
+ inside = false
19
+ lines.each do |line|
20
+ parsed = ContentLine.parse_line(line)
21
+ unless parsed
22
+ next
23
+ end
24
+
25
+ name, params, value = parsed
26
+
27
+ if name.casecmp?('BEGIN') && value.strip.casecmp?('VCARD')
28
+ inside = true
29
+ elsif name.casecmp?('END') && value.strip.casecmp?('VCARD')
30
+ break
31
+ elsif inside
32
+ props << Ical::Property.new(name: name, params: params, value: value)
33
+ end
35
34
  end
35
+ props.empty? ? nil : Card.new(properties: props)
36
36
  end
37
-
38
- props.empty? ? nil : Card.new(properties: props)
39
37
  end
40
38
  end
41
39
  end
42
40
  end
43
41
  end
44
42
 
43
+ __END__
45
44
 
46
- test do
47
- describe "Protocol::Caldav::Vcard::Parser" do
48
- def parse(text)
49
- Protocol::Caldav::Vcard::Parser.parse(text)
50
- end
45
+ describe "Protocol::Caldav::Vcard::Parser" do
46
+ def parse(text)
47
+ Protocol::Caldav::Vcard::Parser.parse(text)
48
+ end
51
49
 
52
- it "parses a flat BEGIN:VCARD / END:VCARD" do
53
- card = parse("BEGIN:VCARD\r\nVERSION:3.0\r\nFN:John\r\nEND:VCARD")
54
- card.should.not.be.nil
55
- card.find_property("FN").value.should.equal "John"
56
- end
50
+ it "parses a flat BEGIN:VCARD / END:VCARD" do
51
+ card = parse("BEGIN:VCARD\r\nVERSION:3.0\r\nFN:John\r\nEND:VCARD")
52
+ card.should.not.be.nil
53
+ card.find_property("FN").value.should.equal "John"
54
+ end
57
55
 
58
- it "handles VERSION:3.0 and VERSION:4.0 without distinction" do
59
- card3 = parse("BEGIN:VCARD\r\nVERSION:3.0\r\nFN:A\r\nEND:VCARD")
60
- card4 = parse("BEGIN:VCARD\r\nVERSION:4.0\r\nFN:A\r\nEND:VCARD")
61
- card3.find_property("VERSION").value.should.equal "3.0"
62
- card4.find_property("VERSION").value.should.equal "4.0"
63
- end
56
+ it "handles VERSION:3.0 and VERSION:4.0 without distinction" do
57
+ card3 = parse("BEGIN:VCARD\r\nVERSION:3.0\r\nFN:A\r\nEND:VCARD")
58
+ card4 = parse("BEGIN:VCARD\r\nVERSION:4.0\r\nFN:A\r\nEND:VCARD")
59
+ card3.find_property("VERSION").value.should.equal "3.0"
60
+ card4.find_property("VERSION").value.should.equal "4.0"
61
+ end
64
62
 
65
- it "handles structured values in N property as raw string" do
66
- card = parse("BEGIN:VCARD\r\nN:Doe;John;;;Jr.\r\nEND:VCARD")
67
- card.find_property("N").value.should.equal "Doe;John;;;Jr."
68
- end
63
+ it "handles structured values in N property as raw string" do
64
+ card = parse("BEGIN:VCARD\r\nN:Doe;John;;;Jr.\r\nEND:VCARD")
65
+ card.find_property("N").value.should.equal "Doe;John;;;Jr."
66
+ end
69
67
 
70
- it "returns nil for empty input" do
71
- parse("").should.be.nil
72
- end
68
+ it "returns nil for empty input" do
69
+ parse("").should.be.nil
70
+ end
73
71
 
74
- it "returns nil for nil input" do
75
- parse(nil).should.be.nil
76
- end
72
+ it "returns nil for nil input" do
73
+ parse(nil).should.be.nil
74
+ end
77
75
 
78
- it "tolerates LF-only line endings" do
79
- card = parse("BEGIN:VCARD\nFN:Test\nEND:VCARD")
80
- card.find_property("FN").value.should.equal "Test"
81
- end
76
+ it "tolerates LF-only line endings" do
77
+ card = parse("BEGIN:VCARD\nFN:Test\nEND:VCARD")
78
+ card.find_property("FN").value.should.equal "Test"
79
+ end
82
80
 
83
- it "tolerates BOM" do
84
- card = parse("\xEF\xBB\xBFBEGIN:VCARD\r\nFN:Test\r\nEND:VCARD")
85
- card.should.not.be.nil
86
- end
81
+ it "tolerates BOM" do
82
+ card = parse("\xEF\xBB\xBFBEGIN:VCARD\r\nFN:Test\r\nEND:VCARD")
83
+ card.should.not.be.nil
87
84
  end
88
85
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Protocol
4
4
  module Caldav
5
- VERSION = "1.0.2"
5
+ VERSION = "1.1.0"
6
6
  end
7
7
  end
@@ -1,122 +1,126 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "bundler/setup"
4
- require "scampi"
5
-
6
3
  module Protocol
7
4
  module Caldav
8
5
  module Xml
9
6
  module_function
10
7
 
11
8
  def escape(str)
12
- return '' unless str
13
-
14
- str.to_s
15
- .gsub('&', '&amp;')
16
- .gsub('<', '&lt;')
17
- .gsub('>', '&gt;')
18
- .gsub('"', '&quot;')
9
+ if str
10
+ str.to_s
11
+ .gsub('&', '&amp;')
12
+ .gsub('<', '&lt;')
13
+ .gsub('>', '&gt;')
14
+ .gsub('"', '&quot;')
15
+ else
16
+ ''
17
+ end
19
18
  end
20
19
 
21
20
  def extract_value(xml, tag)
22
- return nil if xml.nil? || xml.empty?
23
-
24
- match = xml.match(/<[^>]*#{Regexp.escape(tag)}[^>]*>([^<]*)</)
25
- return nil unless match
26
-
27
- value = match[1].strip
28
- value.empty? ? nil : value
21
+ if xml.nil? || xml.empty?
22
+ nil
23
+ else
24
+ match = xml.match(/<[^>]*#{Regexp.escape(tag)}[^>]*>([^<]*)</)
25
+ if match
26
+ value = match[1].strip
27
+ value.empty? ? nil : value
28
+ else
29
+ nil
30
+ end
31
+ end
29
32
  end
30
33
 
31
34
  def extract_attr(xml, tag, attr)
32
- return nil if xml.nil? || xml.empty?
33
-
34
- match = xml.match(/<[^>]*#{Regexp.escape(tag)}[^>]*#{Regexp.escape(attr)}="([^"]*)"/)
35
- match ? match[1] : nil
35
+ if xml.nil? || xml.empty?
36
+ nil
37
+ else
38
+ match = xml.match(/<[^>]*#{Regexp.escape(tag)}[^>]*#{Regexp.escape(attr)}="([^"]*)"/)
39
+ match ? match[1] : nil
40
+ end
36
41
  end
37
42
  end
38
43
  end
39
44
  end
40
45
 
46
+ __END__
41
47
 
42
- test do
43
- describe "Protocol::Caldav::Xml" do
44
- describe ".escape" do
45
- it "escapes ampersand" do
46
- Protocol::Caldav::Xml.escape("a&b").should.equal "a&amp;b"
47
- end
48
+ describe "Protocol::Caldav::Xml" do
49
+ describe ".escape" do
50
+ it "escapes ampersand" do
51
+ Protocol::Caldav::Xml.escape("a&b").should.equal "a&amp;b"
52
+ end
48
53
 
49
- it "escapes less-than" do
50
- Protocol::Caldav::Xml.escape("a<b").should.equal "a&lt;b"
51
- end
54
+ it "escapes less-than" do
55
+ Protocol::Caldav::Xml.escape("a<b").should.equal "a&lt;b"
56
+ end
52
57
 
53
- it "escapes greater-than" do
54
- Protocol::Caldav::Xml.escape("a>b").should.equal "a&gt;b"
55
- end
58
+ it "escapes greater-than" do
59
+ Protocol::Caldav::Xml.escape("a>b").should.equal "a&gt;b"
60
+ end
56
61
 
57
- it "escapes double-quote" do
58
- Protocol::Caldav::Xml.escape('a"b').should.equal "a&quot;b"
59
- end
62
+ it "escapes double-quote" do
63
+ Protocol::Caldav::Xml.escape('a"b').should.equal "a&quot;b"
64
+ end
60
65
 
61
- it "escapes all five entities together" do
62
- Protocol::Caldav::Xml.escape('&<>"').should.equal '&amp;&lt;&gt;&quot;'
63
- end
66
+ it "escapes all five entities together" do
67
+ Protocol::Caldav::Xml.escape('&<>"').should.equal '&amp;&lt;&gt;&quot;'
68
+ end
64
69
 
65
- it "returns empty string for nil" do
66
- Protocol::Caldav::Xml.escape(nil).should.equal ''
67
- end
70
+ it "returns empty string for nil" do
71
+ Protocol::Caldav::Xml.escape(nil).should.equal ''
72
+ end
68
73
 
69
- it "returns empty string for empty string" do
70
- Protocol::Caldav::Xml.escape('').should.equal ''
71
- end
74
+ it "returns empty string for empty string" do
75
+ Protocol::Caldav::Xml.escape('').should.equal ''
76
+ end
72
77
 
73
- it "passes through plain text unchanged" do
74
- Protocol::Caldav::Xml.escape("hello world").should.equal "hello world"
75
- end
78
+ it "passes through plain text unchanged" do
79
+ Protocol::Caldav::Xml.escape("hello world").should.equal "hello world"
76
80
  end
81
+ end
77
82
 
78
- describe ".extract_value" do
79
- it "extracts text content from a tag" do
80
- Protocol::Caldav::Xml.extract_value('<d:displayname>Work</d:displayname>', 'displayname').should.equal 'Work'
81
- end
83
+ describe ".extract_value" do
84
+ it "extracts text content from a tag" do
85
+ Protocol::Caldav::Xml.extract_value('<d:displayname>Work</d:displayname>', 'displayname').should.equal 'Work'
86
+ end
82
87
 
83
- it "returns nil for empty content" do
84
- Protocol::Caldav::Xml.extract_value('<d:displayname></d:displayname>', 'displayname').should.be.nil
85
- end
88
+ it "returns nil for empty content" do
89
+ Protocol::Caldav::Xml.extract_value('<d:displayname></d:displayname>', 'displayname').should.be.nil
90
+ end
86
91
 
87
- it "returns nil when tag not found" do
88
- Protocol::Caldav::Xml.extract_value('<d:other>x</d:other>', 'displayname').should.be.nil
89
- end
92
+ it "returns nil when tag not found" do
93
+ Protocol::Caldav::Xml.extract_value('<d:other>x</d:other>', 'displayname').should.be.nil
94
+ end
90
95
 
91
- it "returns nil for nil input" do
92
- Protocol::Caldav::Xml.extract_value(nil, 'displayname').should.be.nil
93
- end
96
+ it "returns nil for nil input" do
97
+ Protocol::Caldav::Xml.extract_value(nil, 'displayname').should.be.nil
98
+ end
94
99
 
95
- it "returns nil for empty input" do
96
- Protocol::Caldav::Xml.extract_value('', 'displayname').should.be.nil
97
- end
100
+ it "returns nil for empty input" do
101
+ Protocol::Caldav::Xml.extract_value('', 'displayname').should.be.nil
102
+ end
98
103
 
99
- it "strips whitespace from value" do
100
- Protocol::Caldav::Xml.extract_value('<d:displayname> Work </d:displayname>', 'displayname').should.equal 'Work'
101
- end
104
+ it "strips whitespace from value" do
105
+ Protocol::Caldav::Xml.extract_value('<d:displayname> Work </d:displayname>', 'displayname').should.equal 'Work'
102
106
  end
107
+ end
103
108
 
104
- describe ".extract_attr" do
105
- it "extracts attribute value from a tag" do
106
- Protocol::Caldav::Xml.extract_attr('<c:comp-filter name="VCALENDAR">', 'comp-filter', 'name').should.equal 'VCALENDAR'
107
- end
109
+ describe ".extract_attr" do
110
+ it "extracts attribute value from a tag" do
111
+ Protocol::Caldav::Xml.extract_attr('<c:comp-filter name="VCALENDAR">', 'comp-filter', 'name').should.equal 'VCALENDAR'
112
+ end
108
113
 
109
- it "returns nil when tag not found" do
110
- Protocol::Caldav::Xml.extract_attr('<c:other name="x">', 'comp-filter', 'name').should.be.nil
111
- end
114
+ it "returns nil when tag not found" do
115
+ Protocol::Caldav::Xml.extract_attr('<c:other name="x">', 'comp-filter', 'name').should.be.nil
116
+ end
112
117
 
113
- it "returns nil for nil input" do
114
- Protocol::Caldav::Xml.extract_attr(nil, 'comp-filter', 'name').should.be.nil
115
- end
118
+ it "returns nil for nil input" do
119
+ Protocol::Caldav::Xml.extract_attr(nil, 'comp-filter', 'name').should.be.nil
120
+ end
116
121
 
117
- it "returns nil for empty input" do
118
- Protocol::Caldav::Xml.extract_attr('', 'comp-filter', 'name').should.be.nil
119
- end
122
+ it "returns nil for empty input" do
123
+ Protocol::Caldav::Xml.extract_attr('', 'comp-filter', 'name').should.be.nil
120
124
  end
121
125
  end
122
126
  end
@@ -10,7 +10,7 @@ module Protocol
10
10
  "xmlns:c" => "urn:ietf:params:xml:ns:caldav",
11
11
  "xmlns:cr" => "urn:ietf:params:xml:ns:carddav",
12
12
  "xmlns:cs" => "http://calendarserver.org/ns/",
13
- "xmlns:x" => "http://apple.com/ns/ical/"
13
+ "xmlns:x" => "http://apple.com/ns/ical/",
14
14
  }.freeze
15
15
 
16
16
  module_function
@@ -27,7 +27,9 @@ module Protocol
27
27
  def response(xml, href:)
28
28
  xml.tag!("d:response") do
29
29
  xml.tag!("d:href", href)
30
- yield xml if block_given?
30
+ if block_given?
31
+ yield xml
32
+ end
31
33
  end
32
34
  end
33
35
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: protocol-caldav
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nathan K
@@ -37,20 +37,34 @@ dependencies:
37
37
  - - ">="
38
38
  - !ruby/object:Gem::Version
39
39
  version: '0'
40
+ - !ruby/object:Gem::Dependency
41
+ name: rubocop
42
+ requirement: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '1.88'
47
+ type: :development
48
+ prerelease: false
49
+ version_requirements: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '1.88'
40
54
  - !ruby/object:Gem::Dependency
41
55
  name: scampi
42
56
  requirement: !ruby/object:Gem::Requirement
43
57
  requirements:
44
58
  - - "~>"
45
59
  - !ruby/object:Gem::Version
46
- version: 0.1.7
47
- type: :runtime
60
+ version: '1.0'
61
+ type: :development
48
62
  prerelease: false
49
63
  version_requirements: !ruby/object:Gem::Requirement
50
64
  requirements:
51
65
  - - "~>"
52
66
  - !ruby/object:Gem::Version
53
- version: 0.1.7
67
+ version: '1.0'
54
68
  description: |
55
69
  Pure protocol code for CalDAV (RFC 4791) and CardDAV (RFC 6352).
56
70
  No rack, no async, no I/O. Pair with async-caldav for a server.