grit_ext 0.6.2 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5c2db9ffa33d3eaa39a3e640b1c879e52e6c570b
4
+ data.tar.gz: 736042a74a29d727cbf26755843a7ac0fac2ba07
5
+ SHA512:
6
+ metadata.gz: 11a4e1e8c379994ad2fe9794c092710984cad0bd87b10ee12fa0c798a665cd533fc69fde81851a3c05d0d8af0314c75a15e6fe4c9333e4931a8b14bea735fb29
7
+ data.tar.gz: 3a59a6118d64ebf8dba01a3a6f5f168b7d6b9716a9cc290e76e03cfcffc9d1eda8792496132214ae5c5f3d0a1f145cf1ef08b7f5c57972533a4aeb541b868662
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
@@ -4,9 +4,9 @@ require File.expand_path('../lib/grit_ext/version', __FILE__)
4
4
  Gem::Specification.new do |gem|
5
5
  gem.authors = ["Saito"]
6
6
  gem.email = ["saitowu@gmail.com"]
7
- gem.description = %q{the grit's extension}
8
- gem.summary = %q{gives grit utf-8 support and some misc enhancement}
9
- gem.homepage = "http://saito.im"
7
+ gem.description = %q{the grit's utf-8 support}
8
+ gem.summary = %q{gives grit utf-8 support}
9
+ gem.homepage = "https://github.com/saitowu/grit_ext"
10
10
 
11
11
  gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
12
12
  gem.files = `git ls-files`.split("\n")
@@ -16,4 +16,9 @@ Gem::Specification.new do |gem|
16
16
  gem.version = GritExt.version
17
17
 
18
18
  gem.add_dependency("charlock_holmes", "~> 0.6.9")
19
+
20
+ gem.add_development_dependency("rspec", "~> 2.11")
21
+ gem.add_development_dependency("grit", "~> 2.5")
22
+ gem.add_development_dependency("rake")
23
+ gem.add_development_dependency("pry")
19
24
  end
@@ -18,20 +18,24 @@ module GritExt
18
18
 
19
19
  # return message if message type is binary
20
20
  detect = CharlockHolmes::EncodingDetector.detect(message)
21
- return message if detect[:type] == :binary
21
+ return message.force_encoding("BINARY") if detect && detect[:type] == :binary
22
22
 
23
- # if message is not utf-8 encoding, convert it
24
- if detect[:encoding]
23
+ # encoding message to detect encoding
24
+ if detect && detect[:encoding]
25
25
  message.force_encoding(detect[:encoding])
26
- message.encode!("UTF-8", detect[:encoding], undef: :replace, replace: "", invalid: :replace)
27
26
  end
28
27
 
29
- # ensure message encoding is utf8
30
- message.valid_encoding? ? message : raise
31
-
32
- # Prevent app from crash cause of encoding errors
28
+ # encode and clean the bad chars
29
+ message.replace clean(message)
33
30
  rescue
34
31
  encoding = detect ? detect[:encoding] : "unknown"
35
32
  "--broken encoding: #{encoding}"
36
33
  end
34
+
35
+ private
36
+ def clean(message)
37
+ message.encode("UTF-16BE", :undef => :replace, :invalid => :replace, :replace => "")
38
+ .encode("UTF-8")
39
+ .gsub("\0".encode("UTF-8"), "")
40
+ end
37
41
  end
@@ -2,6 +2,6 @@ module GritExt
2
2
  extend self
3
3
 
4
4
  def version
5
- "0.6.2"
5
+ "0.8.0"
6
6
  end
7
7
  end
@@ -0,0 +1,37 @@
1
+ # encoding: UTF-8
2
+
3
+ require "spec_helper"
4
+
5
+ describe GritExt do
6
+ describe :encode! do
7
+ it "should return nil if the message is nil" do
8
+ message = GritExt.encode! nil
9
+ message.should be_nil
10
+ end
11
+
12
+ it "should return binary it self when message is binary" do
13
+ message = "\xFF\xD8\xFF\xE0"
14
+ encoded_message = GritExt.encode!(message)
15
+ encoded_message.bytes.to_a.should eql(message.bytes.to_a)
16
+ message.encoding.name.should eql("ASCII-8BIT")
17
+ end
18
+
19
+ it "cleans up UTF-8 strings with invalid encoding" do
20
+ message = GritExt.encode!("yummy\xE2 \xF0\x9F\x8D\x94 \x9F\x8D\x94")
21
+ message.should eql("yummy 🍔 ")
22
+ message.encoding.name.should eql("UTF-8")
23
+ end
24
+
25
+ it "encode string to UTF-8" do
26
+ message = GritExt.encode!("{foo \xC3 'bar'}")
27
+ message.should eql("{foo à 'bar'}")
28
+ message.encoding.name.should eql("UTF-8")
29
+
30
+ message = "我爱你".encode("GBK")
31
+ message.encoding.name.should eql("GBK")
32
+ GritExt.encode!(message)
33
+ message.should eql("我爱你")
34
+ message.encoding.name.should eql("UTF-8")
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,3 @@
1
+ require 'pry'
2
+ require 'grit'
3
+ require 'grit_ext'
metadata CHANGED
@@ -1,20 +1,18 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: grit_ext
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.2
5
- prerelease:
4
+ version: 0.8.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Saito
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-03-01 00:00:00.000000000 Z
11
+ date: 2013-03-20 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: charlock_holmes
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
17
  - - ~>
20
18
  - !ruby/object:Gem::Version
@@ -22,12 +20,67 @@ dependencies:
22
20
  type: :runtime
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
24
  - - ~>
28
25
  - !ruby/object:Gem::Version
29
26
  version: 0.6.9
30
- description: the grit's extension
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '2.11'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '2.11'
41
+ - !ruby/object:Gem::Dependency
42
+ name: grit
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '2.5'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '2.5'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: the grit's utf-8 support
31
84
  email:
32
85
  - saitowu@gmail.com
33
86
  executables: []
@@ -35,6 +88,7 @@ extensions: []
35
88
  extra_rdoc_files: []
36
89
  files:
37
90
  - .gitignore
91
+ - .rspec
38
92
  - Gemfile
39
93
  - LICENSE
40
94
  - README.md
@@ -48,28 +102,31 @@ files:
48
102
  - lib/grit_ext/tag.rb
49
103
  - lib/grit_ext/tree.rb
50
104
  - lib/grit_ext/version.rb
51
- homepage: http://saito.im
105
+ - spec/grit_ext_spec.rb
106
+ - spec/spec_helper.rb
107
+ homepage: https://github.com/saitowu/grit_ext
52
108
  licenses: []
109
+ metadata: {}
53
110
  post_install_message:
54
111
  rdoc_options: []
55
112
  require_paths:
56
113
  - lib
57
114
  required_ruby_version: !ruby/object:Gem::Requirement
58
- none: false
59
115
  requirements:
60
116
  - - '>='
61
117
  - !ruby/object:Gem::Version
62
118
  version: '0'
63
119
  required_rubygems_version: !ruby/object:Gem::Requirement
64
- none: false
65
120
  requirements:
66
121
  - - '>='
67
122
  - !ruby/object:Gem::Version
68
123
  version: '0'
69
124
  requirements: []
70
125
  rubyforge_project:
71
- rubygems_version: 1.8.25
126
+ rubygems_version: 2.0.2
72
127
  signing_key:
73
- specification_version: 3
74
- summary: gives grit utf-8 support and some misc enhancement
75
- test_files: []
128
+ specification_version: 4
129
+ summary: gives grit utf-8 support
130
+ test_files:
131
+ - spec/grit_ext_spec.rb
132
+ - spec/spec_helper.rb