force_utf8 1.0.0

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.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in force_utf8.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Gitorious AS
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,41 @@
1
+ # ForceUtf8
2
+
3
+ ForceUtf8 is a library that allows you to convert any Ruby string into a string encoded in UTF-8. It replaces any unknown characters with question marks.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'force_utf8'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install force_utf8
18
+
19
+ ## Usage
20
+
21
+ ### Monkey-patched version
22
+
23
+ require 'force_utf8'
24
+
25
+ "foo\xCFbar".force_utf8 # returns "foo?bar"
26
+ "foo\xCFbar".force_utf8! # mutates string into "foo?bar"
27
+
28
+ ### Global function
29
+
30
+ require 'force_utf8/encode'
31
+
32
+ ForceUtf8::Encode.encode("foo\xCFbar") # returns "foo?bar"
33
+ ForceUtf8::Encode.encode!("foo\xCFbar") # mutates string into "foo?bar"
34
+
35
+ ## Contributing
36
+
37
+ 1. Fork it
38
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
39
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
40
+ 4. Push to the branch (`git push origin my-new-feature`)
41
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'force_utf8/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "force_utf8"
8
+ spec.version = ForceUtf8::VERSION
9
+ spec.authors = ["Adam Pohorecki"]
10
+ spec.email = ["adam@pohorecki.pl"]
11
+ spec.description = %q{Convert any Ruby string to UTF8.}
12
+ spec.summary = %q{Convert any Ruby string to UTF8.}
13
+ spec.homepage = "https://gitorious.org/gitorious/force_utf8"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ end
@@ -0,0 +1,3 @@
1
+ require "force_utf8/version"
2
+ require "force_utf8/encode"
3
+ require "force_utf8/core_ext"
@@ -0,0 +1,9 @@
1
+ class String
2
+ def force_utf8
3
+ ForceUtf8::Encode.encode(self)
4
+ end
5
+
6
+ def force_utf8!
7
+ ForceUtf8::Encode.encode!(self)
8
+ end
9
+ end
@@ -0,0 +1,23 @@
1
+ module ForceUtf8
2
+ module Encode
3
+ def encode(string)
4
+ return unless string
5
+ string = string.dup
6
+ encode!(string)
7
+ string
8
+ end
9
+
10
+ def encode!(string)
11
+ return unless string
12
+
13
+ string.force_encoding("UTF-8")
14
+
15
+ unless string.valid_encoding?
16
+ new_chars = string.chars.map { |c| c.valid_encoding? ? c : '?' }.join
17
+ string.replace(new_chars)
18
+ end
19
+ end
20
+
21
+ extend self
22
+ end
23
+ end
@@ -0,0 +1,3 @@
1
+ module ForceUtf8
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,79 @@
1
+ # encoding: utf-8
2
+
3
+ require "force_utf8"
4
+
5
+ describe ForceUtf8::Encode do
6
+ context "monkey patches" do
7
+ it "includes the mutating extension" do
8
+ str = "żółć\xCF"
9
+ str.force_utf8!
10
+
11
+ str.should == "żółć?"
12
+ end
13
+
14
+ it "includes the immutable extension" do
15
+ str = "żółć\xCF"
16
+
17
+ str.force_utf8.should == "żółć?"
18
+ str.should == "żółć\xCF"
19
+ end
20
+ end
21
+
22
+ context "encode!" do
23
+ it "does not replace valid UTF-8 chars" do
24
+ str = "żółć"
25
+
26
+ ForceUtf8::Encode.encode!(str)
27
+
28
+ str.should == "żółć"
29
+ str.valid_encoding?.should be_true
30
+ end
31
+
32
+ it "replaces invalid UTF-8 chars with question marks" do
33
+ str = "żółć\xCF"
34
+
35
+ ForceUtf8::Encode.encode!(str)
36
+
37
+ str.should == "żółć?"
38
+ str.valid_encoding?.should be_true
39
+ end
40
+
41
+ it "does not attempt to mutate a nil value" do
42
+ expect {
43
+ ForceUtf8::Encode.encode!(nil)
44
+ }.not_to raise_error
45
+ end
46
+ end
47
+
48
+ context "encode" do
49
+ it "does not replace valid UTF-8 chars" do
50
+ str = "żółć"
51
+
52
+ encoded = ForceUtf8::Encode.encode(str)
53
+
54
+ encoded.should == "żółć"
55
+ encoded.valid_encoding?.should be_true
56
+ end
57
+
58
+ it "replaces invalid UTF-8 chars with question marks" do
59
+ str = "żółć\xCF"
60
+
61
+ encoded = ForceUtf8::Encode.encode(str)
62
+
63
+ encoded.should == "żółć?"
64
+ encoded.valid_encoding?.should be_true
65
+ end
66
+
67
+ it "does not mutate the given string" do
68
+ str = "żółć\xCF"
69
+
70
+ ForceUtf8::Encode.encode(str)
71
+
72
+ str.should == "żółć\xCF"
73
+ end
74
+
75
+ it "returns nil for nil value" do
76
+ ForceUtf8::Encode.encode(nil).should be_nil
77
+ end
78
+ end
79
+ end
metadata ADDED
@@ -0,0 +1,112 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: force_utf8
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Adam Pohorecki
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-11-04 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.3'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.3'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description: Convert any Ruby string to UTF8.
63
+ email:
64
+ - adam@pohorecki.pl
65
+ executables: []
66
+ extensions: []
67
+ extra_rdoc_files: []
68
+ files:
69
+ - .gitignore
70
+ - Gemfile
71
+ - LICENSE.txt
72
+ - README.md
73
+ - Rakefile
74
+ - force_utf8.gemspec
75
+ - lib/force_utf8.rb
76
+ - lib/force_utf8/core_ext.rb
77
+ - lib/force_utf8/encode.rb
78
+ - lib/force_utf8/version.rb
79
+ - spec/force_utf8/encode_spec.rb
80
+ homepage: https://gitorious.org/gitorious/force_utf8
81
+ licenses:
82
+ - MIT
83
+ post_install_message:
84
+ rdoc_options: []
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ none: false
89
+ requirements:
90
+ - - ! '>='
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ segments:
94
+ - 0
95
+ hash: -2425636791820530650
96
+ required_rubygems_version: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ segments:
103
+ - 0
104
+ hash: -2425636791820530650
105
+ requirements: []
106
+ rubyforge_project:
107
+ rubygems_version: 1.8.25
108
+ signing_key:
109
+ specification_version: 3
110
+ summary: Convert any Ruby string to UTF8.
111
+ test_files:
112
+ - spec/force_utf8/encode_spec.rb