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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3979c8c12db4aaa03312129567f9836f09fd4ef4c1288b4f6737e3b030e4aae7
4
- data.tar.gz: 265f5cf58138de71a8a4cd977bc7222a78c20c6cecd5346744c5561ecca41a62
3
+ metadata.gz: fe5948267cd436af9d1cd8639040bee740b642b57f3d79cdb9cf26537508f08e
4
+ data.tar.gz: a7989a59ed50492a700bd258d63dedbf4bd8a3e4cb38caea59c1300d3e76453e
5
5
  SHA512:
6
- metadata.gz: 32832c0c4d326ca5bb0ec5fbee10b05cacc01712f178cb1c8aacb3e9148571e0b6a3a7389c744572c46c25ba1a783bf6852b8c1a672708995885ded6ecbfe5d4
7
- data.tar.gz: 6fe7d3c06a98f18dc921f9dadc9a272e0389cb341f567df6f1d2bf6222555fac02fe44e4d60a83ffca8e56f5dfb08b6fd06f547d6210772d4ffa20c3b27bb04f
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Zarby
4
- VERSION = "0.1.0"
4
+ VERSION = "0.1.1"
5
5
  end
data/lib/zarby.rb CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  require_relative 'zarby/version'
4
4
  require_relative 'zarby/csv'
5
+ require_relative 'zarby/normalize'
5
6
 
6
7
  module Zarby
7
8
  class ZarbyError < StandardError; end
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.0
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.7
53
+ rubygems_version: 3.2.3
53
54
  signing_key:
54
55
  specification_version: 4
55
56
  summary: String encoding and decoding with Ruby.