File tree Expand file tree Collapse file tree 5 files changed +86
-1
lines changed Expand file tree Collapse file tree 5 files changed +86
-1
lines changed Original file line number Diff line number Diff line change @@ -61,6 +61,7 @@ module Utils
61
61
autoload :Buffer , 'puppet_forge_server/utils/buffer'
62
62
autoload :Http , 'puppet_forge_server/utils/http'
63
63
autoload :MarkdownRenderer , 'puppet_forge_server/utils/md_renderer'
64
+ autoload :Encoding , 'puppet_forge_server/utils/encoding'
64
65
end
65
66
66
67
module Http
Original file line number Diff line number Diff line change 16
16
17
17
module PuppetForgeServer ::Api ::V3
18
18
module Releases
19
+ include PuppetForgeServer ::Utils ::Encoding
19
20
def get_releases ( metadata )
20
21
metadata . map do |element |
21
22
author , name = element . metadata . name . split ( '-' )
@@ -36,7 +37,7 @@ def get_releases(metadata)
36
37
:file_uri => "/v3/files#{ element . path } " ,
37
38
:file_md5 => element . checksum ,
38
39
:deleted_at => element . deleted_at ,
39
- :readme => element . readme
40
+ :readme => to_utf8 ( element . readme )
40
41
}
41
42
end . uniq { |r | r [ :version ] } . sort_by { |r | Gem ::Version . new ( r [ :version ] ) }
42
43
end
Original file line number Diff line number Diff line change
1
+ # -*- encoding: utf-8 -*-
2
+ #
3
+ # Copyright 2015 Centralny Osroder Informatyki (gov.pl)
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+
17
+ require 'iconv'
18
+
19
+ module PuppetForgeServer ::Utils
20
+ module Encoding
21
+
22
+ @@ic = Iconv . new ( 'UTF-8//IGNORE' , 'UTF-8' )
23
+
24
+ # Converts give text to valid UTF-8
25
+ # @param [string] text given string, can be null
26
+ # @return [string] output string in utf-8
27
+ def to_utf8 ( text )
28
+ replaced = text
29
+ unless replaced . nil?
30
+ replaced = replaced . force_encoding ( "UTF-8" ) if is_ascii_8bit? ( replaced )
31
+ replaced = cleanup_utf8 ( replaced )
32
+ end
33
+ replaced
34
+ end
35
+
36
+ private
37
+
38
+ def is_ascii_8bit? ( text )
39
+ text . encoding . name == 'ASCII-8BIT'
40
+ end
41
+
42
+ def cleanup_utf8 ( text )
43
+ @@ic . iconv ( text )
44
+ end
45
+ end
46
+ end
Original file line number Diff line number Diff line change @@ -46,12 +46,15 @@ Gem::Specification.new do |spec|
46
46
spec . add_dependency 'multipart-post' , '~> 2.0.0'
47
47
spec . add_dependency 'redcarpet' , '~> 3.3.0'
48
48
spec . add_dependency 'lrucache' , '~> 0.1.4'
49
+ spec . add_dependency 'iconv' , '~> 1.0.4'
49
50
50
51
spec . add_development_dependency 'rake' , '~> 10.3'
51
52
spec . add_development_dependency 'rspec' , '~> 3.1'
52
53
spec . add_development_dependency 'rspec-core' , '~> 3.1'
53
54
spec . add_development_dependency 'simplecov' , '~> 0.11.0'
54
55
spec . add_development_dependency 'rspec-stopwatch' , '~> 0.1.3'
56
+ spec . add_development_dependency 'pry'
57
+ spec . add_development_dependency 'pry-byebug'
55
58
56
59
spec . required_ruby_version = '>= 1.9.3'
57
60
end
Original file line number Diff line number Diff line change
1
+ require 'spec_helper'
2
+
3
+ describe 'PuppetForgeServer::Utils::Encoding' do
4
+ let ( :dummy_class ) { Class . new { include PuppetForgeServer ::Utils ::Encoding } }
5
+ let ( :dummy ) { dummy_class . new }
6
+ describe '#to_utf8' do
7
+ subject { dummy . to_utf8 ( input_text ) }
8
+ context 'for nil' do
9
+ let ( :input_text ) { nil }
10
+ it { expect ( subject ) . to be_nil }
11
+ end
12
+
13
+ context 'for valid ascii text' do
14
+ let ( :input_text ) { 'a simple ascii text!' }
15
+ it { expect ( subject ) . to eq ( 'a simple ascii text!' ) }
16
+ end
17
+
18
+ context 'for valid utf-8 text' do
19
+ let ( :input_text ) { 'Zażółć gęślą jażń' }
20
+ it { expect ( subject ) . to eq ( 'Zażółć gęślą jażń' ) }
21
+ end
22
+
23
+ context 'for text with ISO-8859-1 encoded as UTF-8' do
24
+ let ( :input_text ) { "Co\xE2 te d'Azur" }
25
+ it { expect ( subject ) . to eq ( 'Cote d\'Azur' ) }
26
+ end
27
+
28
+ context 'for text with UTF-8 not replaced characters' do
29
+ let ( :multidot ) { [ 0xE2 , 0x80 , 0xA6 ] . pack ( "c*" ) }
30
+ let ( :input_text ) { "Read more#{ multidot } " }
31
+ it { expect ( subject ) . to eq ( 'Read more…' ) }
32
+ end
33
+ end
34
+ end
You can’t perform that action at this time.
0 commit comments