virginity 0.3.31

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.
Files changed (42) hide show
  1. checksums.yaml +7 -0
  2. data/lib/virginity.rb +6 -0
  3. data/lib/virginity/api_extensions.rb +87 -0
  4. data/lib/virginity/api_extensions/fields_to_json.rb +82 -0
  5. data/lib/virginity/api_extensions/fields_to_xml.rb +151 -0
  6. data/lib/virginity/bnf.rb +84 -0
  7. data/lib/virginity/dir_info.rb +93 -0
  8. data/lib/virginity/dir_info/content_line.rb +146 -0
  9. data/lib/virginity/dir_info/line_folding.rb +60 -0
  10. data/lib/virginity/dir_info/param.rb +208 -0
  11. data/lib/virginity/dir_info/query.rb +144 -0
  12. data/lib/virginity/encoding_decoding.rb +177 -0
  13. data/lib/virginity/encodings.rb +36 -0
  14. data/lib/virginity/fixes.rb +230 -0
  15. data/lib/virginity/vcard.rb +244 -0
  16. data/lib/virginity/vcard/base_field.rb +126 -0
  17. data/lib/virginity/vcard/categories.rb +57 -0
  18. data/lib/virginity/vcard/cleaning.rb +364 -0
  19. data/lib/virginity/vcard/field.rb +22 -0
  20. data/lib/virginity/vcard/field/params.rb +93 -0
  21. data/lib/virginity/vcard/field_values.rb +10 -0
  22. data/lib/virginity/vcard/field_values/binary.rb +22 -0
  23. data/lib/virginity/vcard/field_values/boolean.rb +14 -0
  24. data/lib/virginity/vcard/field_values/case_insensitive_value.rb +13 -0
  25. data/lib/virginity/vcard/field_values/date.rb +16 -0
  26. data/lib/virginity/vcard/field_values/integer.rb +15 -0
  27. data/lib/virginity/vcard/field_values/optional_structured_text.rb +35 -0
  28. data/lib/virginity/vcard/field_values/separated_text.rb +59 -0
  29. data/lib/virginity/vcard/field_values/structured_text.rb +71 -0
  30. data/lib/virginity/vcard/field_values/text.rb +23 -0
  31. data/lib/virginity/vcard/field_values/uri.rb +15 -0
  32. data/lib/virginity/vcard/fields.rb +284 -0
  33. data/lib/virginity/vcard/fields_osx.rb +95 -0
  34. data/lib/virginity/vcard/fields_soocial.rb +45 -0
  35. data/lib/virginity/vcard/name_handler.rb +151 -0
  36. data/lib/virginity/vcard/patching.rb +262 -0
  37. data/lib/virginity/vcard21.rb +2 -0
  38. data/lib/virginity/vcard21/base.rb +30 -0
  39. data/lib/virginity/vcard21/parser.rb +359 -0
  40. data/lib/virginity/vcard21/reader.rb +103 -0
  41. data/lib/virginity/vcard21/writer.rb +139 -0
  42. metadata +111 -0
@@ -0,0 +1,139 @@
1
+ require 'virginity/vcard21/base'
2
+
3
+ module Virginity
4
+ module Vcard21
5
+ module Writer # for Field
6
+
7
+
8
+ class Vcard21Line
9
+
10
+ class LineTooWide < Error; end
11
+
12
+ # this is probably the correct way to see if we have non-ascii in ruby1.9
13
+ # we duplicate every part of a content-line so that we safely can change values
14
+ def initialize(group, name, params, value)
15
+ @group = group.nil? ? nil : group.dup
16
+ @name = name.dup
17
+ @params = Param::deep_copy(params)
18
+ if @params.any? {|p| p.key =~ ENCODING and p.value =~ /^b$/i }
19
+ @params.delete_if {|p| p.key =~ ENCODING and p.value =~ /^b$/i }
20
+ @params << Param.new("ENCODING","BASE64")
21
+ end
22
+ if qp?
23
+ @value = EncodingDecoding::decode_quoted_printable(@value)
24
+ @params.delete_if {|p| p.key =~ ENCODING and p.value =~ QUOTED_PRINTABLE }
25
+ else
26
+ @value = value.dup
27
+ end
28
+ @value = "2.1" if @name == "VERSION"
29
+ end
30
+
31
+ def inspect
32
+ {:group => @group, :name => @name, :params => @params.join(', '), :value => @value}.inspect
33
+ end
34
+
35
+ LINE_TOO_LONG = /[^\n\r]{76,}/
36
+ def line_too_wide_for21?(line)
37
+ !(line =~ LINE_TOO_LONG).nil?
38
+ end
39
+
40
+ def qp?
41
+ @params.any? { |p| Vcard21::qp_param?(p) }
42
+ end
43
+
44
+ def base64?
45
+ @params.any? { |p| Vcard21::base64_param?(p) }
46
+ end
47
+
48
+ # FIXME there must be a better way to find non-ascii chars
49
+ def non_ascii?
50
+ @value.each_byte do |b|
51
+ return true if b > 127
52
+ end
53
+ false
54
+ end
55
+
56
+ # NON_ASCII_CHAR = /^\u{0000}-\u{007F}/
57
+ # def non_ascii?
58
+ # if @value =~ NON_ASCII_CHAR
59
+ # @params << Param.new("CHARSET","UTF-8")
60
+ # @params << Param.new("ENCODING","QUOTED-PRINTABLE")
61
+ # return
62
+ # end
63
+ # end
64
+
65
+ # param = param-name "=" param-value *("," param-value)
66
+ def params_to_s(options = {})
67
+ return "" if @params.empty?
68
+ if options[:vcard21_omit_type_if_knowntype]
69
+ pv = @params.uniq.sort.map do |p|
70
+ (p.key == "TYPE" and KNOWNTYPES.include? p.value) ? p.value : p.to_s
71
+ end
72
+ ";" + pv.join(";")
73
+ else
74
+ ";" + @params.uniq.sort.join(";")
75
+ end
76
+ end
77
+
78
+
79
+
80
+ def pre_process_value!(options = {})
81
+ encoding = @params.select {|p| p.key =~ ENCODING }
82
+ raise "vCard author is confused, #{encoding.inspect}" if encoding.size > 1
83
+ if encoding.empty?
84
+ if non_ascii?
85
+ @params << Param.new("CHARSET","UTF-8")
86
+ @params << Param.new("ENCODING","QUOTED-PRINTABLE")
87
+ elsif @value =~ /\r|\n/
88
+ @params << Param.new("ENCODING","QUOTED-PRINTABLE")
89
+ elsif line_too_wide_for21?(@value) # if the value part alone already is too wide
90
+ @params << Param.new("ENCODING","QUOTED-PRINTABLE")
91
+ end
92
+ else
93
+ case encoding.first.value
94
+ when QUOTED_PRINTABLE, EIGHT_BIT, SEVEN_BIT, BASE64
95
+ nil
96
+ else
97
+ raise "unexpected encoding #{encoding.first.inspect}"
98
+ end
99
+ end
100
+ end
101
+
102
+ def to_s(options = {})
103
+ pre_process_value!(options)
104
+ @params.uniq!
105
+ line = [@group, @name].compact.join(".")
106
+ line << params_to_s(options.merge({:vcard21_omit_type_if_knowntype => true}))
107
+ line << ":"
108
+ if qp?
109
+ line << EncodingDecoding::encode_quoted_printable(@value, :initial_position => line.size) if qp?
110
+ elsif base64?
111
+ #Fixes::photo_folding_like_apple(@value, options.merge({:width => 70})) + "\r\n"
112
+ # "\\0" is the matched string
113
+ line << "\r\n" << @value.gsub(/.{70}/u, "\\0\r\n") << "\r\n"
114
+ else
115
+ line << @value
116
+ end
117
+ raise LineTooWide, "line_too_wide #{line.inspect}" if line_too_wide_for21?(line)
118
+ line
119
+ rescue LineTooWide => e
120
+ return line if qp? # we did all we can, let's hope the phone can read this vcard
121
+ @params << Param.new("ENCODING","QUOTED-PRINTABLE")
122
+ retry
123
+ end
124
+ end
125
+
126
+ def vcard21line
127
+ if self.respond_to?(:text)
128
+ Vcard21Line.new(@group, @name, @params, text) # we possibly want to reencode text as QP. With newlines and such
129
+ else
130
+ Vcard21Line.new(@group, @name, @params, @value)
131
+ end
132
+ end
133
+
134
+ def encode21(options = {})
135
+ vcard21line.to_s(options)
136
+ end
137
+ end
138
+ end
139
+ end
metadata ADDED
@@ -0,0 +1,111 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: virginity
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.31
5
+ platform: ruby
6
+ authors:
7
+ - Tijn Schuurmans
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2012-09-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: reactive_array
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: fast_xs
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: Virginity reads and writes vcards and provides a nice api to modify them.
42
+ email: tijn@soocial.com
43
+ executables: []
44
+ extensions: []
45
+ extra_rdoc_files: []
46
+ files:
47
+ - lib/virginity.rb
48
+ - lib/virginity/api_extensions.rb
49
+ - lib/virginity/api_extensions/fields_to_json.rb
50
+ - lib/virginity/api_extensions/fields_to_xml.rb
51
+ - lib/virginity/bnf.rb
52
+ - lib/virginity/dir_info.rb
53
+ - lib/virginity/dir_info/content_line.rb
54
+ - lib/virginity/dir_info/line_folding.rb
55
+ - lib/virginity/dir_info/param.rb
56
+ - lib/virginity/dir_info/query.rb
57
+ - lib/virginity/encoding_decoding.rb
58
+ - lib/virginity/encodings.rb
59
+ - lib/virginity/fixes.rb
60
+ - lib/virginity/vcard.rb
61
+ - lib/virginity/vcard/base_field.rb
62
+ - lib/virginity/vcard/categories.rb
63
+ - lib/virginity/vcard/cleaning.rb
64
+ - lib/virginity/vcard/field.rb
65
+ - lib/virginity/vcard/field/params.rb
66
+ - lib/virginity/vcard/field_values.rb
67
+ - lib/virginity/vcard/field_values/binary.rb
68
+ - lib/virginity/vcard/field_values/boolean.rb
69
+ - lib/virginity/vcard/field_values/case_insensitive_value.rb
70
+ - lib/virginity/vcard/field_values/date.rb
71
+ - lib/virginity/vcard/field_values/integer.rb
72
+ - lib/virginity/vcard/field_values/optional_structured_text.rb
73
+ - lib/virginity/vcard/field_values/separated_text.rb
74
+ - lib/virginity/vcard/field_values/structured_text.rb
75
+ - lib/virginity/vcard/field_values/text.rb
76
+ - lib/virginity/vcard/field_values/uri.rb
77
+ - lib/virginity/vcard/fields.rb
78
+ - lib/virginity/vcard/fields_osx.rb
79
+ - lib/virginity/vcard/fields_soocial.rb
80
+ - lib/virginity/vcard/name_handler.rb
81
+ - lib/virginity/vcard/patching.rb
82
+ - lib/virginity/vcard21.rb
83
+ - lib/virginity/vcard21/base.rb
84
+ - lib/virginity/vcard21/parser.rb
85
+ - lib/virginity/vcard21/reader.rb
86
+ - lib/virginity/vcard21/writer.rb
87
+ homepage: https://github.com/tijn/virginity
88
+ licenses:
89
+ - MIT
90
+ metadata: {}
91
+ post_install_message:
92
+ rdoc_options: []
93
+ require_paths:
94
+ - lib
95
+ required_ruby_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ required_rubygems_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ requirements: []
106
+ rubyforge_project:
107
+ rubygems_version: 2.2.2
108
+ signing_key:
109
+ specification_version: 4
110
+ summary: Virginity vCard writer/parser.
111
+ test_files: []