summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>2013-01-31 16:10:02 +0200
committerDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>2013-01-31 16:10:02 +0200
commit16326c4170b82787f66cc146fa7a71439068588c (patch)
treef75c157407085261fcb7ccd12c2ebb86de229203 /lib
parent01088eb84cfad17505d8431da582acdb55b885df (diff)
downloadgitlab-ci-16326c4170b82787f66cc146fa7a71439068588c.tar.gz
GitlabCi::Encode lib
Diffstat (limited to 'lib')
-rw-r--r--lib/gitlab_ci/encode.rb31
1 files changed, 31 insertions, 0 deletions
diff --git a/lib/gitlab_ci/encode.rb b/lib/gitlab_ci/encode.rb
new file mode 100644
index 0000000..6cf84fd
--- /dev/null
+++ b/lib/gitlab_ci/encode.rb
@@ -0,0 +1,31 @@
+module GitlabCi
+ module Encode
+ extend self
+
+ def encode!(message)
+ return nil unless message.respond_to? :force_encoding
+
+ # if message is utf-8 encoding, just return it
+ message.force_encoding("UTF-8")
+ return message if message.valid_encoding?
+
+ # return message if message type is binary
+ detect = CharlockHolmes::EncodingDetector.detect(message)
+ return message if detect[:type] == :binary
+
+ # if message is not utf-8 encoding, convert it
+ if detect[:encoding]
+ message.force_encoding(detect[:encoding])
+ message.encode!("UTF-8", detect[:encoding], undef: :replace, replace: "", invalid: :replace)
+ end
+
+ # ensure message encoding is utf8
+ message.valid_encoding? ? message : raise
+
+ # Prevent app from crash cause of encoding errors
+ rescue
+ encoding = detect ? detect[:encoding] : "unknown"
+ "--broken encoding: #{encoding}"
+ end
+ end
+end