tilia-vobject 4.0.0.pre.alpha2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (193) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +19 -0
  3. data/.rubocop.yml +32 -0
  4. data/.simplecov +4 -0
  5. data/.travis.yml +3 -0
  6. data/CHANGELOG.sabre.md +626 -0
  7. data/CONTRIBUTING.md +25 -0
  8. data/Gemfile +17 -0
  9. data/Gemfile.lock +68 -0
  10. data/LICENSE +27 -0
  11. data/LICENSE.sabre +27 -0
  12. data/README.md +63 -0
  13. data/Rakefile +17 -0
  14. data/bin/vobject +7 -0
  15. data/lib/tilia/v_object/birthday_calendar_generator.rb +142 -0
  16. data/lib/tilia/v_object/cli.rb +582 -0
  17. data/lib/tilia/v_object/component/available.rb +107 -0
  18. data/lib/tilia/v_object/component/v_alarm.rb +114 -0
  19. data/lib/tilia/v_object/component/v_availability.rb +128 -0
  20. data/lib/tilia/v_object/component/v_calendar.rb +468 -0
  21. data/lib/tilia/v_object/component/v_card.rb +457 -0
  22. data/lib/tilia/v_object/component/v_event.rb +127 -0
  23. data/lib/tilia/v_object/component/v_free_busy.rb +81 -0
  24. data/lib/tilia/v_object/component/v_journal.rb +75 -0
  25. data/lib/tilia/v_object/component/v_time_zone.rb +51 -0
  26. data/lib/tilia/v_object/component/v_todo.rb +147 -0
  27. data/lib/tilia/v_object/component.rb +591 -0
  28. data/lib/tilia/v_object/date_time_parser.rb +486 -0
  29. data/lib/tilia/v_object/document.rb +218 -0
  30. data/lib/tilia/v_object/element_list.rb +18 -0
  31. data/lib/tilia/v_object/eof_exception.rb +8 -0
  32. data/lib/tilia/v_object/free_busy_data.rb +149 -0
  33. data/lib/tilia/v_object/free_busy_generator.rb +465 -0
  34. data/lib/tilia/v_object/i_tip/broker.rb +909 -0
  35. data/lib/tilia/v_object/i_tip/i_tip_exception.rb +9 -0
  36. data/lib/tilia/v_object/i_tip/message.rb +109 -0
  37. data/lib/tilia/v_object/i_tip/same_organizer_for_all_components_exception.rb +13 -0
  38. data/lib/tilia/v_object/i_tip.rb +10 -0
  39. data/lib/tilia/v_object/node.rb +192 -0
  40. data/lib/tilia/v_object/parameter.rb +327 -0
  41. data/lib/tilia/v_object/parse_exception.rb +7 -0
  42. data/lib/tilia/v_object/parser/json.rb +149 -0
  43. data/lib/tilia/v_object/parser/mime_dir.rb +543 -0
  44. data/lib/tilia/v_object/parser/parser.rb +61 -0
  45. data/lib/tilia/v_object/parser/xml/element/key_value.rb +60 -0
  46. data/lib/tilia/v_object/parser/xml/element.rb +11 -0
  47. data/lib/tilia/v_object/parser/xml.rb +322 -0
  48. data/lib/tilia/v_object/parser.rb +10 -0
  49. data/lib/tilia/v_object/property/binary.rb +96 -0
  50. data/lib/tilia/v_object/property/boolean.rb +57 -0
  51. data/lib/tilia/v_object/property/flat_text.rb +52 -0
  52. data/lib/tilia/v_object/property/float_value.rb +107 -0
  53. data/lib/tilia/v_object/property/i_calendar/cal_address.rb +49 -0
  54. data/lib/tilia/v_object/property/i_calendar/date.rb +15 -0
  55. data/lib/tilia/v_object/property/i_calendar/date_time.rb +330 -0
  56. data/lib/tilia/v_object/property/i_calendar/duration.rb +65 -0
  57. data/lib/tilia/v_object/property/i_calendar/period.rb +124 -0
  58. data/lib/tilia/v_object/property/i_calendar/recur.rb +173 -0
  59. data/lib/tilia/v_object/property/i_calendar.rb +14 -0
  60. data/lib/tilia/v_object/property/integer_value.rb +60 -0
  61. data/lib/tilia/v_object/property/text.rb +352 -0
  62. data/lib/tilia/v_object/property/time.rb +85 -0
  63. data/lib/tilia/v_object/property/unknown.rb +30 -0
  64. data/lib/tilia/v_object/property/uri.rb +78 -0
  65. data/lib/tilia/v_object/property/utc_offset.rb +56 -0
  66. data/lib/tilia/v_object/property/v_card/date.rb +31 -0
  67. data/lib/tilia/v_object/property/v_card/date_and_or_time.rb +343 -0
  68. data/lib/tilia/v_object/property/v_card/date_time.rb +22 -0
  69. data/lib/tilia/v_object/property/v_card/language_tag.rb +41 -0
  70. data/lib/tilia/v_object/property/v_card/time_stamp.rb +74 -0
  71. data/lib/tilia/v_object/property/v_card.rb +13 -0
  72. data/lib/tilia/v_object/property.rb +532 -0
  73. data/lib/tilia/v_object/reader.rb +73 -0
  74. data/lib/tilia/v_object/recur/event_iterator.rb +417 -0
  75. data/lib/tilia/v_object/recur/no_instances_exception.rb +11 -0
  76. data/lib/tilia/v_object/recur/r_date_iterator.rb +138 -0
  77. data/lib/tilia/v_object/recur/r_rule_iterator.rb +717 -0
  78. data/lib/tilia/v_object/recur.rb +10 -0
  79. data/lib/tilia/v_object/settings.rb +32 -0
  80. data/lib/tilia/v_object/splitter/i_calendar.rb +95 -0
  81. data/lib/tilia/v_object/splitter/splitter_interface.rb +31 -0
  82. data/lib/tilia/v_object/splitter/v_card.rb +56 -0
  83. data/lib/tilia/v_object/splitter.rb +9 -0
  84. data/lib/tilia/v_object/string_util.rb +58 -0
  85. data/lib/tilia/v_object/time_zone_data/exchange_zones.rb +96 -0
  86. data/lib/tilia/v_object/time_zone_data/lotus_zones.rb +104 -0
  87. data/lib/tilia/v_object/time_zone_data/php_zones.rb +49 -0
  88. data/lib/tilia/v_object/time_zone_data/windows_zones.rb +121 -0
  89. data/lib/tilia/v_object/time_zone_data.rb +10 -0
  90. data/lib/tilia/v_object/time_zone_util.rb +213 -0
  91. data/lib/tilia/v_object/uuid_util.rb +51 -0
  92. data/lib/tilia/v_object/v_card_converter.rb +354 -0
  93. data/lib/tilia/v_object/version.rb +9 -0
  94. data/lib/tilia/v_object/writer.rb +56 -0
  95. data/lib/tilia/v_object.rb +45 -0
  96. data/lib/tilia/vobject.rb +1 -0
  97. data/resources/schema/xcal.rng +1192 -0
  98. data/resources/schema/xcard.rng +388 -0
  99. data/test/test_helper.rb +56 -0
  100. data/test/v_object/attach_issue_test.rb +19 -0
  101. data/test/v_object/birthday_calendar_generator_test.rb +463 -0
  102. data/test/v_object/cli_mock.rb +19 -0
  103. data/test/v_object/cli_test.rb +460 -0
  104. data/test/v_object/component/available_test.rb +59 -0
  105. data/test/v_object/component/v_alarm_test.rb +160 -0
  106. data/test/v_object/component/v_availability_test.rb +388 -0
  107. data/test/v_object/component/v_calendar_test.rb +646 -0
  108. data/test/v_object/component/v_card_test.rb +258 -0
  109. data/test/v_object/component/v_event_test.rb +85 -0
  110. data/test/v_object/component/v_free_busy_test.rb +59 -0
  111. data/test/v_object/component/v_journal_test.rb +85 -0
  112. data/test/v_object/component/v_time_zone_test.rb +47 -0
  113. data/test/v_object/component/v_todo_test.rb +172 -0
  114. data/test/v_object/component_test.rb +419 -0
  115. data/test/v_object/date_time_parser_test.rb +526 -0
  116. data/test/v_object/document_test.rb +71 -0
  117. data/test/v_object/element_list_test.rb +27 -0
  118. data/test/v_object/em_client_test.rb +53 -0
  119. data/test/v_object/empty_parameter_test.rb +65 -0
  120. data/test/v_object/empty_value_issue_test.rb +25 -0
  121. data/test/v_object/fake_component.rb +21 -0
  122. data/test/v_object/free_busy_data_test.rb +285 -0
  123. data/test/v_object/free_busy_generator_test.rb +637 -0
  124. data/test/v_object/google_colon_escaping_test.rb +27 -0
  125. data/test/v_object/i_calendar/attach_parse_test.rb +24 -0
  126. data/test/v_object/i_tip/broker_attendee_reply_test.rb +1042 -0
  127. data/test/v_object/i_tip/broker_delete_event_test.rb +175 -0
  128. data/test/v_object/i_tip/broker_new_event_test.rb +440 -0
  129. data/test/v_object/i_tip/broker_process_message_test.rb +153 -0
  130. data/test/v_object/i_tip/broker_process_reply_test.rb +402 -0
  131. data/test/v_object/i_tip/broker_tester.rb +71 -0
  132. data/test/v_object/i_tip/broker_update_event_test.rb +763 -0
  133. data/test/v_object/i_tip/evolution_test.rb +2644 -0
  134. data/test/v_object/i_tip/message_test.rb +25 -0
  135. data/test/v_object/issue153.vcf +352 -0
  136. data/test/v_object/issue153_test.rb +12 -0
  137. data/test/v_object/issue26_test.rb +25 -0
  138. data/test/v_object/issue36_work_around_test.rb +37 -0
  139. data/test/v_object/issue40_test.rb +26 -0
  140. data/test/v_object/issue64.vcf +351 -0
  141. data/test/v_object/issue64_test.rb +17 -0
  142. data/test/v_object/issue96_test.rb +22 -0
  143. data/test/v_object/issue_undefined_index_test.rb +24 -0
  144. data/test/v_object/j_cal_test.rb +150 -0
  145. data/test/v_object/j_card_test.rb +192 -0
  146. data/test/v_object/line_folding_issue_test.rb +19 -0
  147. data/test/v_object/mock_document.rb +6 -0
  148. data/test/v_object/parameter_test.rb +109 -0
  149. data/test/v_object/parser/json_test.rb +370 -0
  150. data/test/v_object/parser/mime_dir_test.rb +14 -0
  151. data/test/v_object/parser/quoted_printable_test.rb +78 -0
  152. data/test/v_object/parser/xml_test.rb +2563 -0
  153. data/test/v_object/property/binary_test.rb +12 -0
  154. data/test/v_object/property/boolean_test.rb +18 -0
  155. data/test/v_object/property/compound_test.rb +43 -0
  156. data/test/v_object/property/float_test.rb +20 -0
  157. data/test/v_object/property/i_calendar/cal_address_test.rb +26 -0
  158. data/test/v_object/property/i_calendar/date_time_test.rb +303 -0
  159. data/test/v_object/property/i_calendar/duration_test.rb +14 -0
  160. data/test/v_object/property/i_calendar/recur_test.rb +39 -0
  161. data/test/v_object/property/text_test.rb +81 -0
  162. data/test/v_object/property/v_card/date_and_or_time_test.rb +205 -0
  163. data/test/v_object/property/v_card/language_tag_test.rb +35 -0
  164. data/test/v_object/property_test.rb +338 -0
  165. data/test/v_object/reader_test.rb +403 -0
  166. data/test/v_object/recur/event_iterator/by_month_in_daily_test.rb +52 -0
  167. data/test/v_object/recur/event_iterator/by_set_pos_hang_test.rb +55 -0
  168. data/test/v_object/recur/event_iterator/expand_floating_times_test.rb +109 -0
  169. data/test/v_object/recur/event_iterator/fifth_tuesday_problem_test.rb +45 -0
  170. data/test/v_object/recur/event_iterator/incorrect_expand_test.rb +53 -0
  171. data/test/v_object/recur/event_iterator/infinite_loop_problem_test.rb +75 -0
  172. data/test/v_object/recur/event_iterator/issue48_test.rb +43 -0
  173. data/test/v_object/recur/event_iterator/issue50_test.rb +123 -0
  174. data/test/v_object/recur/event_iterator/main_test.rb +1222 -0
  175. data/test/v_object/recur/event_iterator/missing_overridden_test.rb +55 -0
  176. data/test/v_object/recur/event_iterator/no_instances_test.rb +32 -0
  177. data/test/v_object/recur/event_iterator/override_first_event_test.rb +106 -0
  178. data/test/v_object/recur/r_date_iterator_test.rb +44 -0
  179. data/test/v_object/recur/r_rule_iterator_test.rb +608 -0
  180. data/test/v_object/recurrence_iterator/UntilRespectsTimezoneTest.ics +39 -0
  181. data/test/v_object/slash_r_test.rb +15 -0
  182. data/test/v_object/splitter/i_calendar_test.rb +299 -0
  183. data/test/v_object/splitter/v_card_test.rb +173 -0
  184. data/test/v_object/string_util_test.rb +37 -0
  185. data/test/v_object/test_case.rb +42 -0
  186. data/test/v_object/time_zone_util_test.rb +271 -0
  187. data/test/v_object/uuid_util_test.rb +18 -0
  188. data/test/v_object/v_card21_test.rb +43 -0
  189. data/test/v_object/v_card_converter_test.rb +419 -0
  190. data/test/v_object/version_test.rb +15 -0
  191. data/test/v_object/writer_test.rb +33 -0
  192. data/tilia-vobject.gemspec +17 -0
  193. metadata +308 -0
@@ -0,0 +1,9 @@
1
+ module Tilia
2
+ module VObject
3
+ module ITip
4
+ # This message is emitted in case of serious problems with iTip messages.
5
+ class ITipException < Exception
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,109 @@
1
+ module Tilia
2
+ module VObject
3
+ module ITip
4
+ # This class represents an iTip message.
5
+ #
6
+ # A message holds all the information relevant to the message, including the
7
+ # object itself.
8
+ #
9
+ # It should for the most part be treated as immutable.
10
+ class Message
11
+ # The object's UID.
12
+ #
13
+ # @var string
14
+ attr_accessor :uid
15
+
16
+ # The component type, such as VEVENT.
17
+ #
18
+ # @var string
19
+ attr_accessor :component
20
+
21
+ # Contains the ITip method, which is something like REQUEST, REPLY or
22
+ # CANCEL.
23
+ #
24
+ # @var string
25
+ attr_accessor :method
26
+
27
+ # The current sequence number for the event.
28
+ #
29
+ # @var int
30
+ attr_accessor :sequence
31
+
32
+ # The senders' email address.
33
+ #
34
+ # Note that this does not imply that this has to be used in a From: field
35
+ # if the message is sent by email. It may also be populated in Reply-To:
36
+ # or not at all.
37
+ #
38
+ # @var string
39
+ attr_accessor :sender
40
+
41
+ # The name of the sender. This is often populated from a CN parameter from
42
+ # either the ORGANIZER or ATTENDEE, depending on the message.
43
+ #
44
+ # @var string|null
45
+ attr_accessor :sender_name
46
+
47
+ # The recipient's email address.
48
+ #
49
+ # @var string
50
+ attr_accessor :recipient
51
+
52
+ # The name of the recipient. This is usually populated with the CN
53
+ # parameter from the ATTENDEE or ORGANIZER property, if it's available.
54
+ #
55
+ # @var string|null
56
+ attr_accessor :recipient_name
57
+
58
+ # After the message has been delivered, this should contain a string such
59
+ # as : 1.1;Sent or 1.2;Delivered.
60
+ #
61
+ # In case of a failure, this will hold the error status code.
62
+ #
63
+ # See:
64
+ # http://tools.ietf.org/html/rfc6638#section-7.3
65
+ #
66
+ # @var string
67
+ attr_accessor :schedule_status
68
+
69
+ # The iCalendar / iTip body.
70
+ #
71
+ # @var \Sabre\VObject\Component\VCalendar
72
+ attr_accessor :message
73
+
74
+ # This will be set to true, if the iTip broker considers the change
75
+ # 'significant'.
76
+ #
77
+ # In practice, this means that we'll only mark it true, if for instance
78
+ # DTSTART changed. This allows systems to only send iTip messages when
79
+ # significant changes happened. This is especially useful for iMip, as
80
+ # normally a ton of messages may be generated for normal calendar use.
81
+ #
82
+ # To see the list of properties that are considered 'significant', check
83
+ # out Sabre\VObject\ITip\Broker::significant_change_properties.
84
+ #
85
+ # @var bool
86
+ attr_accessor :significant_change
87
+
88
+ # Returns the schedule status as a string.
89
+ #
90
+ # For example:
91
+ # 1.2
92
+ #
93
+ # @return mixed bool|string
94
+ def schedule_status
95
+ if !@schedule_status
96
+ false
97
+ else
98
+ @schedule_status.split(';').first
99
+ end
100
+ end
101
+
102
+ # TODO: document
103
+ def initialize
104
+ @significant_change = true
105
+ end
106
+ end
107
+ end
108
+ end
109
+ end
@@ -0,0 +1,13 @@
1
+ module Tilia
2
+ module VObject
3
+ module ITip
4
+ # SameOrganizerForAllComponentsException.
5
+ #
6
+ # This exception is emitted when an event is encountered with more than one
7
+ # component (e.g.: exceptions), but the organizer is not identical in every
8
+ # component.
9
+ class SameOrganizerForAllComponentsException < ITipException
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,10 @@
1
+ module Tilia
2
+ module VObject
3
+ module ITip
4
+ require 'tilia/v_object/i_tip/i_tip_exception'
5
+ require 'tilia/v_object/i_tip/same_organizer_for_all_components_exception'
6
+ require 'tilia/v_object/i_tip/message'
7
+ require 'tilia/v_object/i_tip/broker'
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,192 @@
1
+ module Tilia
2
+ module VObject
3
+ # A node is the root class for every element in an iCalendar of vCard object.
4
+ class Node
5
+ include Tilia::Xml::XmlSerializable
6
+ include Enumerable
7
+
8
+ # The following constants are used by the validate method.
9
+ #
10
+ # If REPAIR is set, the validator will attempt to repair any broken data
11
+ # (if possible).
12
+ REPAIR ||= 1
13
+
14
+ # If this option is set, the validator will operate on the vcards on the
15
+ # assumption that the vcards need to be valid for CardDAV.
16
+ #
17
+ # This means for example that the UID is required, whereas it is not for
18
+ # regular vcards.
19
+ PROFILE_CARDDAV ||= 2
20
+
21
+ # If this option is set, the validator will operate on iCalendar objects
22
+ # on the assumption that the vcards need to be valid for CalDAV.
23
+ #
24
+ # This means for example that calendars can only contain objects with
25
+ # identical component types and UIDs.
26
+ PROFILE_CALDAV ||= 4
27
+
28
+ # Reference to the parent object, if this is not the top object.
29
+ #
30
+ # @var Node
31
+ attr_accessor :parent
32
+
33
+ # The root document.
34
+ #
35
+ # @var Component
36
+ # RUBY: attr_accessor :root
37
+
38
+ # Serializes the node into a mimedir format.
39
+ #
40
+ # @return string
41
+ def serialize
42
+ end
43
+
44
+ # This method returns an array, with the representation as it should be
45
+ # encoded in JSON. This is used to create jCard or jCal documents.
46
+ #
47
+ # @return array
48
+ def json_serialize
49
+ end
50
+
51
+ # This method serializes the data into XML. This is used to create xCard or
52
+ # xCal documents.
53
+ #
54
+ # @param Xml\Writer writer XML writer.
55
+ #
56
+ # @return void
57
+ def xml_serialize(_writer)
58
+ end
59
+
60
+ # Validates the node for correctness.
61
+ #
62
+ # The following options are supported:
63
+ # Node::REPAIR - May attempt to automatically repair the problem.
64
+ #
65
+ # This method returns an array with detected problems.
66
+ # Every element has the following properties:
67
+ #
68
+ # * level - problem level.
69
+ # * message - A human-readable string describing the issue.
70
+ # * node - A reference to the problematic node.
71
+ #
72
+ # The level means:
73
+ # 1 - The issue was repaired (only happens if REPAIR was turned on)
74
+ # 2 - An inconsequential issue
75
+ # 3 - A severe issue.
76
+ #
77
+ # @param int options
78
+ #
79
+ # @return array
80
+ def validate(_options = 0)
81
+ []
82
+ end
83
+
84
+ # Returns the iterator for this object.
85
+ #
86
+ # @return ElementList
87
+ def iterator
88
+ return @iterator if @iterator
89
+
90
+ ElementList.new([self])
91
+ end
92
+
93
+ # Sets the overridden iterator.
94
+ #
95
+ # Note that this is not actually part of the iterator interface
96
+ #
97
+ # @param ElementList $iterator
98
+ #
99
+ # @return void
100
+ attr_writer :iterator
101
+
102
+ # Returns the number of elements.
103
+ #
104
+ # @return int
105
+ def size
106
+ it = iterator
107
+ it.size
108
+ end
109
+ alias_method :length, :size
110
+ alias_method :count, :size
111
+
112
+ # Checks if an item exists through ArrayAccess.
113
+ #
114
+ # This method just forwards the request to the inner iterator
115
+ #
116
+ # @param int $offset
117
+ #
118
+ # @return bool
119
+ def key?(offset)
120
+ iterator = self.iterator
121
+ iterator.key?(offset)
122
+ end
123
+
124
+ # Gets an item through ArrayAccess.
125
+ #
126
+ # This method just forwards the request to the inner iterator
127
+ #
128
+ # @param int $offset
129
+ #
130
+ # @return mixed
131
+ def [](offset)
132
+ iterator = self.iterator
133
+ iterator[offset]
134
+ end
135
+
136
+ # Sets an item through ArrayAccess.
137
+ #
138
+ # This method just forwards the request to the inner iterator
139
+ #
140
+ # @param int $offset
141
+ # @param mixed $value
142
+ #
143
+ # @return void
144
+ def []=(offset, value)
145
+ iterator = self.iterator
146
+ iterator[offset] = value
147
+ end
148
+
149
+ # Sets an item through ArrayAccess.
150
+ #
151
+ # This method just forwards the request to the inner iterator
152
+ #
153
+ # @param int $offset
154
+ #
155
+ # @return void
156
+ def delete(offset)
157
+ iterator = self.iterator
158
+ iterator.delete(offset)
159
+ end
160
+
161
+ def initialize
162
+ @root = nil
163
+ end
164
+
165
+ def each
166
+ iterator = self.iterator
167
+ iterator.each { |i| yield(i) }
168
+ end
169
+
170
+ def ==(other)
171
+ return true if other.__id__ == __id__
172
+
173
+ # check class
174
+ return false unless self.class == other.class
175
+
176
+ # Instance variables should be the same
177
+ return false unless instance_variables.sort == other.instance_variables.sort
178
+
179
+ # compare all instance variables
180
+ instance_variables.each do |var|
181
+ if var == :@root && instance_variable_get(var) == self
182
+ # We are our own root
183
+ return false unless other.instance_variable_get(var) == other
184
+ else
185
+ return false unless instance_variable_get(var) == other.instance_variable_get(var)
186
+ end
187
+ end
188
+ true
189
+ end
190
+ end
191
+ end
192
+ end
@@ -0,0 +1,327 @@
1
+ module Tilia
2
+ module VObject
3
+ # VObject Parameter.
4
+ #
5
+ # This class represents a parameter. A parameter is always tied to a property.
6
+ # In the case of:
7
+ # DTSTART;VALUE=DATE:20101108
8
+ # VALUE=DATE would be the parameter name and value.
9
+ class Parameter < Node
10
+ # Parameter name.
11
+ #
12
+ # @var string
13
+ attr_accessor :name
14
+
15
+ # vCard 2.1 allows parameters to be encoded without a name.
16
+ #
17
+ # We can deduce the parameter name based on it's value.
18
+ #
19
+ # @var bool
20
+ attr_accessor :no_name
21
+
22
+ # Parameter value.
23
+ #
24
+ # @var string
25
+ # RUBY: attr_accessor :value
26
+
27
+ # Sets up the object.
28
+ #
29
+ # It's recommended to use the create:: factory method instead.
30
+ #
31
+ # @param string name
32
+ # @param string value
33
+ def initialize(root, name, value = nil)
34
+ @no_name = false
35
+ @name = (name || '').upcase
36
+ @root = root
37
+
38
+ if name.nil?
39
+ @no_name = true
40
+ @name = self.class.guess_parameter_name_by_value(value)
41
+ else
42
+ @name = name.upcase
43
+ end
44
+
45
+ # If guess_parameter_name_by_value returns an empty string
46
+ # above, we're actually dealing with a parameter that has no value.
47
+ # In that case we have to move the value to the name.
48
+ if @name == ''
49
+ @no_name = false
50
+ @name = value.upcase
51
+ else
52
+ self.value = value
53
+ end
54
+ end
55
+
56
+ # Try to guess property name by value, can be used for vCard 2.1 nameless parameters.
57
+ #
58
+ # Figuring out what the name should have been. Note that a ton of
59
+ # these are rather silly in 2014 and would probably rarely be
60
+ # used, but we like to be complete.
61
+ #
62
+ # @param string value
63
+ #
64
+ # @return string
65
+ def self.guess_parameter_name_by_value(value)
66
+ value ||= ''
67
+ case value.upcase
68
+ # Encodings
69
+ when '7-BIT',
70
+ 'QUOTED-PRINTABLE',
71
+ 'BASE64'
72
+ 'ENCODING'
73
+ # Common types
74
+ when 'WORK',
75
+ 'HOME',
76
+ 'PREF',
77
+
78
+ # Delivery Label Type
79
+ 'DOM',
80
+ 'INTL',
81
+ 'POSTAL',
82
+ 'PARCEL',
83
+
84
+ # Telephone types
85
+ 'VOICE',
86
+ 'FAX',
87
+ 'MSG',
88
+ 'CELL',
89
+ 'PAGER',
90
+ 'BBS',
91
+ 'MODEM',
92
+ 'CAR',
93
+ 'ISDN',
94
+ 'VIDEO',
95
+
96
+ # EMAIL types (lol)
97
+ 'AOL',
98
+ 'APPLELINK',
99
+ 'ATTMAIL',
100
+ 'CIS',
101
+ 'EWORLD',
102
+ 'INTERNET',
103
+ 'IBMMAIL',
104
+ 'MCIMAIL',
105
+ 'POWERSHARE',
106
+ 'PRODIGY',
107
+ 'TLX',
108
+ 'X400',
109
+
110
+ # Photo / Logo format types
111
+ 'GIF',
112
+ 'CGM',
113
+ 'WMF',
114
+ 'BMP',
115
+ 'DIB',
116
+ 'PICT',
117
+ 'TIFF',
118
+ 'PDF',
119
+ 'PS',
120
+ 'JPEG',
121
+ 'MPEG',
122
+ 'MPEG2',
123
+ 'AVI',
124
+ 'QTIME',
125
+
126
+ # Sound Digital Audio Type
127
+ 'WAVE',
128
+ 'PCM',
129
+ 'AIFF',
130
+
131
+ # Key types
132
+ 'X509',
133
+ 'PGP'
134
+ 'TYPE'
135
+
136
+ # Value types
137
+ when 'INLINE',
138
+ 'URL',
139
+ 'CONTENT-ID',
140
+ 'CID'
141
+ 'VALUE'
142
+ else
143
+ ''
144
+ end
145
+ end
146
+
147
+ # Updates the current value.
148
+ #
149
+ # This may be either a single, or multiple strings in an array.
150
+ #
151
+ # @param string|array value
152
+ #
153
+ # @return void
154
+ attr_writer :value
155
+
156
+ # Returns the current value.
157
+ #
158
+ # This method will always return a string, or null. If there were multiple
159
+ # values, it will automatically concatenate them (separated by comma).
160
+ #
161
+ # @return string|null
162
+ def value
163
+ if @value.is_a?(Array)
164
+ @value.join(',')
165
+ else
166
+ @value
167
+ end
168
+ end
169
+
170
+ # Sets multiple values for this parameter.
171
+ #
172
+ # @param array value
173
+ #
174
+ # @return void
175
+ def parts=(value)
176
+ @value = value
177
+ end
178
+
179
+ # Returns all values for this parameter.
180
+ #
181
+ # If there were no values, an empty array will be returned.
182
+ #
183
+ # @return array
184
+ def parts
185
+ if @value.is_a?(Array)
186
+ @value
187
+ elsif @value.nil?
188
+ []
189
+ else
190
+ [@value]
191
+ end
192
+ end
193
+
194
+ # Adds a value to this parameter.
195
+ #
196
+ # If the argument is specified as an array, all items will be added to the
197
+ # parameter value list.
198
+ #
199
+ # @param string|array part
200
+ #
201
+ # @return void
202
+ def add_value(part)
203
+ if @value.nil?
204
+ @value = part
205
+ else
206
+ @value = [@value] unless @value.is_a?(Array)
207
+ part = [part] unless part.is_a?(Array)
208
+ @value.concat(part)
209
+ end
210
+ end
211
+
212
+ # Checks if this parameter contains the specified value.
213
+ #
214
+ # This is a case-insensitive match. It makes sense to call this for for
215
+ # instance the TYPE parameter, to see if it contains a keyword such as
216
+ # 'WORK' or 'FAX'.
217
+ #
218
+ # @param string value
219
+ #
220
+ # @return bool
221
+ def has(value)
222
+ value = value.downcase
223
+ results = (@value.is_a?(Array) ? @value : [@value]).select do |entry|
224
+ entry.downcase == value
225
+ end
226
+ results.any?
227
+ end
228
+
229
+ # Turns the object back into a serialized blob.
230
+ #
231
+ # @return string
232
+ def serialize
233
+ value = parts
234
+
235
+ return "#{@name}=" if value.size == 0
236
+
237
+ if @root.document_type == Document::VCARD21 && @no_name
238
+ return value.join(';')
239
+ end
240
+
241
+ result = value.inject('') do |keep, item|
242
+ keep += ',' unless keep == ''
243
+
244
+ # If there's no special characters in the string, we'll use the simple
245
+ # format.
246
+ #
247
+ # The list of special characters is defined as:
248
+ #
249
+ # Any character except CONTROL, DQUOTE, ";", ":", ","
250
+ #
251
+ # by the iCalendar spec:
252
+ # https://tools.ietf.org/html/rfc5545#section-3.1
253
+ #
254
+ # And we add ^ to that because of:
255
+ # https://tools.ietf.org/html/rfc6868
256
+ #
257
+ # But we've found that iCal (7.0, shipped with OSX 10.9)
258
+ # severaly trips on + characters not being quoted, so we
259
+ # added + as well.
260
+ if !(item.to_s =~ /(?: [\n":;\^,\+] )/x)
261
+ keep + item.to_s
262
+ else
263
+ # Enclosing in double-quotes, and using RFC6868 for encoding any
264
+ # special characters
265
+ keep += '"' + item.to_s.gsub(
266
+ /[\^\n"]/,
267
+ '^' => '^^',
268
+ "\n" => '^n',
269
+ '"' => '^\''
270
+ )
271
+ keep + '"'
272
+ end
273
+ end
274
+
275
+ "#{@name}=#{result}"
276
+ end
277
+
278
+ # This method returns an array, with the representation as it should be
279
+ # encoded in JSON. This is used to create jCard or jCal documents.
280
+ #
281
+ # @return array
282
+ def json_serialize
283
+ @value
284
+ end
285
+
286
+ # This method serializes the data into XML. This is used to create xCard or
287
+ # xCal documents.
288
+ #
289
+ # @param Xml\Writer writer XML writer.
290
+ #
291
+ # @return void
292
+ def xml_serialize(writer)
293
+ @value.split(',').each do |value|
294
+ writer.write_element('text', value)
295
+ end
296
+ end
297
+
298
+ # Called when this object is being cast to a string.
299
+ #
300
+ # @return string
301
+ def to_s
302
+ value.to_s
303
+ end
304
+
305
+ def each
306
+ @value.each do |value|
307
+ yield(value)
308
+ end
309
+ end
310
+
311
+ def iterator
312
+ return @iterator if @iterator
313
+
314
+ @iterator = @value || []
315
+ end
316
+
317
+ # TODO: document
318
+ def ==(other)
319
+ if other.is_a?(String)
320
+ to_s == other
321
+ else
322
+ super(other)
323
+ end
324
+ end
325
+ end
326
+ end
327
+ end
@@ -0,0 +1,7 @@
1
+ module Tilia
2
+ module VObject
3
+ # Exception thrown by Reader if an invalid object was attempted to be parsed.
4
+ class ParseException < Exception
5
+ end
6
+ end
7
+ end