Class: Vcard::V4_0::Typegrammars
- Inherits:
-
Object
- Object
- Vcard::V4_0::Typegrammars
- Defined in:
- lib/vobject/vcard/v4_0/typegrammars.rb
Class Method Summary collapse
- .address ⇒ Object
- .clientpidmap ⇒ Object
- .date_and_or_time ⇒ Object
- .date_complete ⇒ Object
- .date_noreduc ⇒ Object
- .date_t ⇒ Object
- .date_time ⇒ Object
- .fivepartname ⇒ Object
- .float_t ⇒ Object
- .gender ⇒ Object
- .iana_token ⇒ Object
-
.integer ⇒ Object
property value types, each defining their own parser.
- .kindvalue ⇒ Object
- .lang ⇒ Object
- .org ⇒ Object
- .text_t ⇒ Object
- .textlist ⇒ Object
- .time_complete ⇒ Object
- .time_notrunc ⇒ Object
- .time_t ⇒ Object
- .timestamp ⇒ Object
-
.typematch(strict, key, params, _component, value) ⇒ Object
Enforce type restrictions on values of particular properties.
- .typeparamtel1list ⇒ Object
- .typerelatedlist ⇒ Object
-
.unescape(x) ⇒ Object
text escapes: \ ; , N n.
-
.unescape_component(x) ⇒ Object
also escape semicolon for compound types.
- .uri ⇒ Object
- .utc_offset ⇒ Object
- .versionvalue ⇒ Object
Class Method Details
.address ⇒ Object
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 |
# File 'lib/vobject/vcard/v4_0/typegrammars.rb', line 330 def address component = seq(C::COMPONENT4 << ",".r, lazy { component }) do |a, b| [unescape_component(a), b].flatten end | C::COMPONENT4.map { |t| [unescape_component(t)] } address = seq(component << ";".r, component << ";".r, component << ";".r, component << ";".r, component << ";".r, component << ";".r, component) do |a, b, c, d, e, f, g| a = a[0] if a.length == 1 b = b[0] if b.length == 1 c = c[0] if c.length == 1 d = d[0] if d.length == 1 e = e[0] if e.length == 1 f = f[0] if f.length == 1 g = g[0] if g.length == 1 PropertyValue::Address.new(pobox: a, ext: b, street: c, locality: d, region: e, code: f, country: g) end address.eof end |
.clientpidmap ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/vobject/vcard/v4_0/typegrammars.rb', line 45 def clientpidmap uri = /\S+/.r.map do |s| if s =~ URI::DEFAULT_PARSER.make_regexp s else { error: "Invalid URI" } end end clientpidmap = seq(/[0-9]/.r << ";".r, uri) do |a, b| PropertyValue::Clientpidmap.new(pid: a, uri: b) end clientpidmap.eof end |
.date_and_or_time ⇒ Object
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 |
# File 'lib/vobject/vcard/v4_0/typegrammars.rb', line 223 def date_and_or_time hour = /[0-9]{2}/.r minute = /[0-9]{2}/.r second = /[0-9]{2}/.r time_notrunc = seq(hour, minute, second, C::ZONE._?) do |h, m, s, z| h = { hour: h, min: m, sec: s } h[:zone] = z[0] unless z.empty? h end | seq(hour, minute, C::ZONE._?) do |h, m, z| h = { hour: h, min: m } h[:zone] = z[0] unless z.empty? h end | seq(hour, C::ZONE._?) do h = { hour: h } h[:zone] = z[0] unless z.empty? h end date_noreduc = seq(/[0-9]{4}/.r, /[0-9]{2}/.r, /[0-9]{2}/.r) do |yy, mm, dd| { year: yy, month: mm, day: dd } end | seq("--", /[0-9]{2}/.r, /[0-9]{2}/.r) do |_, mm, dd| { month: mm, day: dd } end | seq("--", "-", /[0-9]{2}/.r) { |_, _, dd| { day: dd } } date_time = seq(date_noreduc << "T".r, time_notrunc) { |d, t| d.merge t } date = seq(/[0-9]{4}/.r, /[0-9]{2}/.r, /[0-9]{2}/.r) do |yy, mm, dd| { year: yy, month: mm, day: dd } end | seq(/[0-9]{4}/.r << "-".r, /[0-9]{2}/.r) do |yy, dd| { year: yy, day: dd } end | /[0-9]{4}/.r do |yy| { year: yy } end | seq("--", /[0-9]{2}/.r, /[0-9]{2}/.r) do |_, mm, dd| { month: mm, day: dd } end | seq("--", /[0-9]{2}/.r) do |_, mm| { month: mm } end | seq("--", "-", /[0-9]{2}/.r) do |_, _, dd| { day: dd } end time = seq(hour, minute, second, C::ZONE._?) do |h, m, s, z| h = { hour: h, min: m, sec: s } h[:zone] = z[0] unless z.empty? h end | seq(hour, minute, C::ZONE._?) do |h, m, z| h = { hour: h, min: m } h[:zone] = z[0] unless z.empty? h end | seq(hour, C::ZONE._?) do |h, z| h = { hour: h } h[:zone] = z[0] unless z.empty? h # } | seq("-", minute, second, C::ZONE._?) { |m, s, z| # errata: remove zones from truncated times end | seq("-".r >> minute, second) do |m, s| { min: m, sec: s } # } | seq("-", minute, C::ZONE._?) { |m, z| # errata: remove zones from truncated times end | seq("-".r >> minute) do |m| { min: m } # } | seq("-", "-", second, C::ZONE._?) { |s, z| # errata: remove zones from truncated times end | seq("-", "-", second) do |_, _, s| { sec: s } end date_and_or_time = date_time.map do |d| ret = PropertyValue::DateTimeLocal.new d ret.type = "date-and-or-time" ret end | date.map do |d| ret = PropertyValue::Date.new d ret.type = "date-and-or-time" ret end | seq("T".r >> time).map do |t| ret = PropertyValue::Time.new t ret.type = "date-and-or-time" ret end date_and_or_time.eof end |
.date_complete ⇒ Object
102 103 104 105 106 107 108 |
# File 'lib/vobject/vcard/v4_0/typegrammars.rb', line 102 def date_complete date_complete1 = seq(/[0-9]{4}/.r, /[0-9]{2}/.r, /[0-9]{2}/.r) do |yy, mm, dd| { year: yy, month: mm, day: dd } end date_complete = date_complete1.map { |d| PropertyValue::Date.new d } date_complete.eof end |
.date_noreduc ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/vobject/vcard/v4_0/typegrammars.rb', line 90 def date_noreduc date_noreduc1 = seq(/[0-9]{4}/.r, /[0-9]{2}/.r, /[0-9]{2}/.r) do |yy, mm, dd| { year: yy, month: mm, day: dd } end | seq("--".r >> /[0-9]{2}/.r, /[0-9]{2}/.r) do |mm, dd| { month: mm, day: dd } end | seq("--", "-", /[0-9]{2}/.r) do |_, _, dd| { day: dd } end date_noreduc = date_noreduc1.map { |d| PropertyValue::Date.new d } date_noreduc.eof end |
.date_t ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/vobject/vcard/v4_0/typegrammars.rb', line 72 def date_t date_t1 = seq(/[0-9]{4}/.r, /[0-9]{2}/.r, /[0-9]{2}/.r) do |yy, mm, dd| { year: yy, month: mm, day: dd } end | seq(/[0-9]{4}/.r << "-".r, /[0-9]{2}/.r) do |yy, dd| { year: yy, day: dd } end | /[0-9]{4}/.r do |yy| { year: yy } end | seq("--".r >> /[0-9]{2}/.r, /[0-9]{2}/.r) do |mm, dd| { month: mm, day: dd } end | seq("--".r >> /[0-9]{2}/.r) do |mm| { month: mm } end | seq("--", "-", /[0-9]{2}/.r) do |_, _, dd| { day: dd } end date_t = date_t1.map { |d| PropertyValue::Date.new d } date_t.eof end |
.date_time ⇒ Object
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/vobject/vcard/v4_0/typegrammars.rb', line 172 def date_time hour = /[0-9]{2}/.r minute = /[0-9]{2}/.r second = /[0-9]{2}/.r time_notrunc = seq(hour, minute, second, C::ZONE._?) do |h, m, s, z| h = { hour: h, min: m, sec: s } h[:zone] = z[0] unless z.empty? h end | seq(hour, minute, C::ZONE._?) do |h, m, z| h = { hour: h, min: m } h[:zone] = z[0] unless z.empty? h end | seq(hour, C::ZONE._?) do |h, z| h = { hour: h } h[:zone] = z[0] unless z.empty? h end date_noreduc = seq(/[0-9]{4}/.r, /[0-9]{2}/.r, /[0-9]{2}/.r) do |yy, mm, dd| { year: yy, month: mm, day: dd } end | seq("--".r >> /[0-9]{2}/.r, /[0-9]{2}/.r) do |mm, dd| { month: mm, day: dd } end | seq("--", "-", /[0-9]{2}/.r) do |_, _, dd| { day: dd } end date_time = seq(date_noreduc << "T".r, time_notrunc) do |d, t| d = d.merge t PropertyValue::DateTimeLocal.new d end date_time.eof end |
.fivepartname ⇒ Object
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 |
# File 'lib/vobject/vcard/v4_0/typegrammars.rb', line 312 def fivepartname component = seq(C::COMPONENT4 << ",".r, lazy { component }) do |a, b| [unescape_component(a), b].flatten end | C::COMPONENT4.map { |t| [unescape_component(t)] } fivepartname = seq(component << ";".r, component << ";".r, component << ";".r, component << ";".r, component) do |a, b, c, d, e| a = a[0] if a.length == 1 b = b[0] if b.length == 1 c = c[0] if c.length == 1 d = d[0] if d.length == 1 e = e[0] if e.length == 1 PropertyValue::Fivepartname.new(surname: a, givenname: b, additionalname: c, honprefix: d, honsuffix: e) end fivepartname.eof end |
.float_t ⇒ Object
19 20 21 22 |
# File 'lib/vobject/vcard/v4_0/typegrammars.rb', line 19 def float_t float_t = prim(:double).map { |f| PropertyValue::Float.new f } float_t.eof end |
.gender ⇒ Object
353 354 355 356 357 358 359 360 |
# File 'lib/vobject/vcard/v4_0/typegrammars.rb', line 353 def gender gender1 = seq(/[MFONU]/.r._? << ";", C::TEXT4) do |sex, gender| sex = sex[0] unless sex.empty? { sex: sex, gender: gender } end | /[MFONU]/.r.map { |sex| { sex: sex, gender: "" } } gender = gender1.map { |g| PropertyValue::Gender.new g } gender.eof end |
.iana_token ⇒ Object
24 25 26 27 |
# File 'lib/vobject/vcard/v4_0/typegrammars.rb', line 24 def iana_token iana_token = C::IANATOKEN.map { |x| PropertyValue::Ianatoken.new x } iana_token.eof end |
.integer ⇒ Object
property value types, each defining their own parser
14 15 16 17 |
# File 'lib/vobject/vcard/v4_0/typegrammars.rb', line 14 def integer integer = prim(:int32).map { |i| PropertyValue::Integer.new i } integer.eof end |
.kindvalue ⇒ Object
305 306 307 308 309 310 |
# File 'lib/vobject/vcard/v4_0/typegrammars.rb', line 305 def kindvalue kindvalue = (/individual/i.r | /group/i.r | /org/i.r | /location/i.r | /application/i.r | C::IANATOKEN | C::XNAME_VCARD).map { |v| PropertyValue::Kindvalue.new v } kindvalue.eof end |
.lang ⇒ Object
371 372 373 374 |
# File 'lib/vobject/vcard/v4_0/typegrammars.rb', line 371 def lang lang = C::RFC5646LANGVALUE.map { |l| PropertyValue::Lang.new l } lang.eof end |
.org ⇒ Object
362 363 364 365 366 367 368 369 |
# File 'lib/vobject/vcard/v4_0/typegrammars.rb', line 362 def org text = C::COMPONENT4 org1 = seq(text, ";", lazy { org1 }) { |a, _, b| [unescape_component(a), b].flatten } | text.map { |t| [unescape_component(t)] } org = org1.map { |g| PropertyValue::Org.new g } org.eof end |
.text_t ⇒ Object
59 60 61 62 |
# File 'lib/vobject/vcard/v4_0/typegrammars.rb', line 59 def text_t text_t = C::TEXT4.map { |t| PropertyValue::Text.new(unescape(t)) } text_t.eof end |
.textlist ⇒ Object
64 65 66 67 68 69 70 |
# File 'lib/vobject/vcard/v4_0/typegrammars.rb', line 64 def textlist textlist1 = seq(C::TEXT4 << ",".r, lazy { textlist1 }) { |a, b| [unescape(a), b].flatten } | C::TEXT4.map { |t| [unescape(t)] } textlist = textlist1.map { |m| PropertyValue::Textlist.new m } textlist.eof end |
.time_complete ⇒ Object
162 163 164 165 166 167 168 169 170 |
# File 'lib/vobject/vcard/v4_0/typegrammars.rb', line 162 def time_complete time_complete1 = seq(hour, minute, second, C::ZONE._?) do |h, m, s, z| h = { hour: h, min: m, sec: s } h[:zone] = z[0] unless z.empty? h end time_complete = time_complete1.map { |d| PropertyValue::Time.new d } time_complete.eof end |
.time_notrunc ⇒ Object
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/vobject/vcard/v4_0/typegrammars.rb', line 144 def time_notrunc time_notrunc1 = seq(hour, minute, second, C::ZONE._?) do |h, m, s, z| h = { hour: h, min: m, sec: s } h[:zone] = z[0] unless z.empty? h end | seq(hour, minute, C::ZONE._?) do |h, m, z| h = { hour: h, min: m } h[:zone] = z[0] unless z.empty? h end | seq(hour, C::ZONE._?) do |h, z| h = { hour: h } h[:zone] = z[0] unless z.empty? h end time_notrunc = time_notrunc1.map { |d| PropertyValue::Time.new d } time_notrunc.eof end |
.time_t ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/vobject/vcard/v4_0/typegrammars.rb', line 110 def time_t hour = /[0-9]{2}/.r minute = /[0-9]{2}/.r second = /[0-9]{2}/.r time1 = seq(hour, minute, second, C::ZONE._?) do |h, m, s, z| h = { hour: h, min: m, sec: s } h[:zone] = z[0] unless z.empty? h end | seq(hour, minute, C::ZONE._?) do |h, m, z| h = { hour: h, min: m } h[:zone] = z[0] unless z.empty? h end | seq(hour, C::ZONE._?) do |h, z| h = { hour: h } h[:zone] = z[0] unless z.empty? h # } | seq("-", minute, second, C::ZONE._?) { |m, s, z| # errata: remove zones from truncated times end | seq("-".r >> minute, second) do |m, s| { min: m, sec: s } # } | seq("-", minute, C::ZONE._?) { |m, z| # errata: remove zones from truncated times end | seq("-".r >> minute) do |m| { min: m } # } | seq("-", "-", second, C::ZONE._?) do |s, z| # errata: remove zones from truncated times end | seq("-", "-", second) do |_, _, s| h = { sec: s } h end time = time1.map { |d| PropertyValue::Time.new d } time.eof end |
.timestamp ⇒ Object
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
# File 'lib/vobject/vcard/v4_0/typegrammars.rb', line 203 def date_complete = seq(/[0-9]{4}/.r, /[0-9]{2}/.r, /[0-9]{2}/.r) do |yy, mm, dd| { year: yy, month: mm, day: dd } end hour = /[0-9]{2}/.r minute = /[0-9]{2}/.r second = /[0-9]{2}/.r time_complete = seq(hour, minute, second, C::ZONE._?) do |h, m, s, z| h = { hour: h, min: m, sec: s } h[:zone] = z[0] unless z.empty? h end = seq(date_complete << "T".r, time_complete) do |d, t| ret = PropertyValue::DateTimeLocal.new(d.merge(t)) ret.type = 'timestamp' ret end .eof end |
.typematch(strict, key, params, _component, value) ⇒ Object
Enforce type restrictions on values of particular properties. If successful, return typed interpretation of string
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 |
# File 'lib/vobject/vcard/v4_0/typegrammars.rb', line 415 def typematch(strict, key, params, _component, value) errors = [] ctx1 = Rsec::ParseContext.new value, "source" case key when :VERSION ret = versionvalue._parse ctx1 when :SOURCE, :PHOTO, :IMPP, :GEO, :LOGO, :MEMBER, :SOUND, :URL, :FBURL, :CALADRURI, :CALURI, :ORG_DIRECTORY ret = uri._parse ctx1 when :KIND ret = kindvalue._parse ctx1 when :XML, :FN, :EMAIL, :TITLE, :ROLE, :NOTE, :EXPERTISE, :HOBBY, :INTEREST ret = text_t._parse ctx1 when :NICKNAME, :CATEGORIES ret = textlist._parse ctx1 when :ORG ret = org._parse ctx1 when :N ret = fivepartname._parse ctx1 when :ADR ret = address._parse ctx1 when :BDAY, :ANNIVERSARY if params && params[:VALUE] == "text" if params[:CALSCALE] parse_err(strict, errors, "Specified CALSCALE within property #{key} as text", ctx1) end ret = text_t._parse ctx1 else if params && params[:CALSCALE] && /^T/ =~ value parse_err(strict, errors, "Specified CALSCALE within property #{key} as time", ctx1) end ret = date_and_or_time._parse ctx1 end when :DEATHDATE ret = if params && params[:VALUE] == "text" text_t._parse ctx1 else date_and_or_time._parse ctx1 end when :TEL if params && params[:TYPE] typestr = params[:TYPE].is_a?(Array) ? params[:TYPE].join(",") : params[:TYPE] ret1 = typeparamtel1list.parse typestr if !ret1 || Rsec::INVALID[ret1] parse_err(strict, errors, "Specified illegal TYPE parameter #{typestr} within property #{key}", ctx1) end end ret = if params && params[:VALUE] == "uri" uri._parse ctx1 else text_t._parse ctx1 end when :BIRTHPLACE, :DEATHPLACE ret = if params && params[:VALUE] == "uri" uri._parse ctx1 else text_t._parse ctx1 end when :RELATED if params && params[:TYPE] typestr = params[:TYPE].is_a?(Array) ? params[:TYPE].join(";") : params[:TYPE] ret1 = .parse typestr if !ret1 || Rsec::INVALID[ret1] parse_err(strict, errors, "Specified illegal TYPE parameter #{typestr} within property #{key}", ctx1) end end ret = if params && params[:VALUE] == "uri" uri._parse ctx1 else text_t._parse ctx1 end when :UID, :KEY ret = if params && params[:VALUE] == "text" text_t._parse ctx1 else uri._parse ctx1 end when :GENDER ret = gender._parse ctx1 when :LANG ret = lang._parse ctx1 when :TZ ret = if params && params[:VALUE] == "uri" uri._parse ctx1 elsif params && params[:VALUE] == "utc_offset" utc_offset._parse ctx1 else text_t._parse ctx1 end when :REV ret = ._parse ctx1 when :CLIENTPIDMAP if params && params[:PID] parse_err(strict, errors, "Specified PID parameter in CLIENTPIDMAP property", @ctx) end ret = clientpidmap._parse ctx1 else # left completely open in spec ret = Vobject::PropertyValue.new value end if ret.is_a?(Hash) && ret[:error] parse_err(strict, errors, "#{ret[:error]} for property #{key}, value #{value}", ctx1) end if Rsec::INVALID[ret] parse_err(strict, errors, "Type mismatch for property #{key}, value #{value}", ctx1) end ret end |
.typeparamtel1list ⇒ Object
376 377 378 379 380 381 382 383 384 |
# File 'lib/vobject/vcard/v4_0/typegrammars.rb', line 376 def typeparamtel1list typeparamtel1 = /TEXT/i.r | /VOICE/i.r | /FAX/i.r | /CELL/i.r | /VIDEO/i.r | /PAGER/i.r | /TEXTPHONE/i.r | C::IANATOKEN | C::XNAME_VCARD typeparamtel1list = seq(typeparamtel1 << ",".r, lazy { typeparamtel1list }) do |a, b| [a, b].flatten end | typeparamtel1.map { |t| [t] } typeparamtel1list.eof end |
.typerelatedlist ⇒ Object
386 387 388 389 390 391 392 393 394 395 396 397 |
# File 'lib/vobject/vcard/v4_0/typegrammars.rb', line 386 def = /CONTACT/i.r | /ACQUAINTANCE/i.r | /FRIEND/i.r | /MET/i.r | /CO-WORKER/i.r | /COLLEAGUE/i.r | /CO-RESIDENT/i.r | /NEIGHBOR/i.r | /CHILD/i.r | /PARENT/i.r | /SIBLING/i.r | /SPOUSE/i.r | /KIN/i.r | /MUSE/i.r | /CRUSH/i.r | /DATE/i.r | /SWEETHEART/i.r | /ME/i.r | /AGENT/i.r | /EMERGENCY/i.r = seq( << ";".r, lazy { }) { |a, b| [a, b].flatten } | .map { |t| [t] } .eof end |
.unescape(x) ⇒ Object
text escapes: \ ; , N n
400 401 402 403 404 |
# File 'lib/vobject/vcard/v4_0/typegrammars.rb', line 400 def unescape(x) # temporarily escape \\ as \007f, which is disallowed in any text x.gsub(/\\\\/, "\u007f").gsub(/\\,/, ",").gsub(/\\[Nn]/, "\n"). tr("\u007f", "\\") end |
.unescape_component(x) ⇒ Object
also escape semicolon for compound types
407 408 409 410 411 |
# File 'lib/vobject/vcard/v4_0/typegrammars.rb', line 407 def unescape_component(x) # temporarily escape \\ as \007f, which is disallowed in any text x.gsub(/\\\\/, "\u007f").gsub(/\\;/, ";").gsub(/\\,/, ","). gsub(/\\[Nn]/, "\n").tr("\u007f", "\\") end |
.uri ⇒ Object
34 35 36 37 38 39 40 41 42 43 |
# File 'lib/vobject/vcard/v4_0/typegrammars.rb', line 34 def uri uri = /\S+/.r.map do |s| if s =~ URI::DEFAULT_PARSER.make_regexp PropertyValue::Uri.new(s) else { error: "Invalid URI" } end end uri.eof end |
.utc_offset ⇒ Object
300 301 302 303 |
# File 'lib/vobject/vcard/v4_0/typegrammars.rb', line 300 def utc_offset utc_offset = C::UTC_OFFSET.map { |u| PropertyValue::Utcoffset.new u } utc_offset.eof end |
.versionvalue ⇒ Object
29 30 31 32 |
# File 'lib/vobject/vcard/v4_0/typegrammars.rb', line 29 def versionvalue versionvalue = "4.0".r.map { |v| PropertyValue::Version.new v } versionvalue.eof end |