zarby 0.1.0 → 0.1.1
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/lib/zarby/normalize.rb +43 -0
- data/lib/zarby/version.rb +1 -1
- data/lib/zarby.rb +1 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fe5948267cd436af9d1cd8639040bee740b642b57f3d79cdb9cf26537508f08e
|
4
|
+
data.tar.gz: a7989a59ed50492a700bd258d63dedbf4bd8a3e4cb38caea59c1300d3e76453e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: adaab75a5d8988eb5074068a4746d02fde6267bf2abad0e842eaec851771f9b135fe942001a77467806c911b34fc77ee4b39c3022eb7fe43e3a45a8cfeaaa44d
|
7
|
+
data.tar.gz: da24b0a4c02b6ca7296cec3caf79292faebb2803eaae4aa3de9ce3545eac4b7f0f4247139be8d264e43b722808050cbb6e8fea29b013d1d823257b43d7b7c705
|
@@ -0,0 +1,43 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Zarby
|
4
|
+
class Normalize
|
5
|
+
# utf-8 converting from the string's given encoding
|
6
|
+
COMMON_ENCODINGS = %w[UTF-8 Windows-1252 ASCII-8BIT US-ASCII].freeze
|
7
|
+
|
8
|
+
def initialize(input:)
|
9
|
+
@input = input
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.utf8(input)
|
13
|
+
new(input: input).utf8
|
14
|
+
end
|
15
|
+
|
16
|
+
def utf8
|
17
|
+
output = @input if valid?
|
18
|
+
|
19
|
+
COMMON_ENCODINGS.each do |encoding|
|
20
|
+
output ||= convert { @input.encode(encoding) }
|
21
|
+
output ||= convert { @input.force_encoding('UTF-8') } if encoding == 'UTF-8'
|
22
|
+
end
|
23
|
+
|
24
|
+
# replace any unknown characters with a placeholder: �
|
25
|
+
output ||= convert { @input.encode('UTF-8', invalid: :replace, undef: :replace) }
|
26
|
+
|
27
|
+
output
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
def convert
|
33
|
+
string = yield
|
34
|
+
string if string.valid_encoding?
|
35
|
+
rescue Encoding::UndefinedConversionError, Encoding::InvalidByteSequenceError
|
36
|
+
nil
|
37
|
+
end
|
38
|
+
|
39
|
+
def valid?
|
40
|
+
@input.encoding.name == 'UTF-8' && @input.valid_encoding?
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
data/lib/zarby/version.rb
CHANGED
data/lib/zarby.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: zarby
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- vianney.sonneville
|
@@ -22,6 +22,7 @@ files:
|
|
22
22
|
- Rakefile
|
23
23
|
- lib/zarby.rb
|
24
24
|
- lib/zarby/csv.rb
|
25
|
+
- lib/zarby/normalize.rb
|
25
26
|
- lib/zarby/version.rb
|
26
27
|
- sig/zarby.rbs
|
27
28
|
- zarby.gemspec
|
@@ -49,7 +50,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
49
50
|
- !ruby/object:Gem::Version
|
50
51
|
version: '0'
|
51
52
|
requirements: []
|
52
|
-
rubygems_version: 3.3
|
53
|
+
rubygems_version: 3.2.3
|
53
54
|
signing_key:
|
54
55
|
specification_version: 4
|
55
56
|
summary: String encoding and decoding with Ruby.
|