Module: CPEE::EvalRuby::Translation
- Defined in:
- lib/cpee-eval-ruby/translation.rb
Defined Under Namespace
Classes: ParamArray
Class Method Summary collapse
- .convert_to_base64(text) ⇒ Object
- .detect_encoding(text) ⇒ Object
- .extract_base64(text) ⇒ Object
- .simplify_result(result) ⇒ Object
- .simplify_structurized_result(result) ⇒ Object
- .structurize_result(result) ⇒ Object
Class Method Details
.convert_to_base64(text) ⇒ Object
136 137 138 |
# File 'lib/cpee-eval-ruby/translation.rb', line 136 def self::convert_to_base64(text) ('data:' + MimeMagic.by_magic(text).type + ';base64,' + Base64::encode64(text)) rescue ('data:application/octet-stream;base64,' + Base64::encode64(text)) end |
.detect_encoding(text) ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/cpee-eval-ruby/translation.rb', line 117 def self::detect_encoding(text) if text.is_a? String if text.valid_encoding? && text.encoding.name == 'UTF-8' 'UTF-8' else res = CharlockHolmes::EncodingDetector.detect(text) if res.is_a?(Hash) && res[:type] == :text && res[:ruby_encoding] != "binary" res[:encoding] elsif res.is_a?(Hash) && res[:type] == :binary 'BINARY' else 'ISO-8859-1' end end else 'OTHER' end end |
.extract_base64(text) ⇒ Object
139 140 141 142 143 144 145 |
# File 'lib/cpee-eval-ruby/translation.rb', line 139 def self::extract_base64(text) if text.is_a?(String) && text.start_with?(/(data:[\w_\/-]+;base64,)/) Base64::decode64(text.delete_prefix $1) else text end end |
.simplify_result(result) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/cpee-eval-ruby/translation.rb', line 77 def self::simplify_result(result) if result.length == 1 if result[0].is_a? Riddl::Parameter::Simple result = result[0].value elsif result[0].is_a? Riddl::Parameter::Complex if result[0].mimetype == 'application/json' result = JSON::parse(result[0].value.read) rescue nil elsif result[0].mimetype == 'text/csv' result = result[0].value.read elsif result[0].mimetype == 'text/yaml' result = YAML::load(result[0].value.read) rescue nil elsif result[0].mimetype == 'application/xml' || result[0].mimetype == 'text/xml' result = XML::Smart::string(result[0].value.read) rescue nil elsif result[0].mimetype == 'text/plain' result = result[0].value.read if result.start_with?("<?xml version=") result = XML::Smart::string(result) else result = result.to_f if result == result.to_f.to_s result = result.to_i if result == result.to_i.to_s end elsif result[0].mimetype == 'text/html' result = result[0].value.read result = result.to_f if result == result.to_f.to_s result = result.to_i if result == result.to_i.to_s else result = result[0] end end else result = Riddl::Parameter::Array[*result] end if result.is_a? String enc = CPEE::EvalRuby::Translation::detect_encoding(result) enc == 'OTHER' ? result : (result.encode('UTF-8',enc) rescue CPEE::EvalRuby::Translation::convert_to_base64(result)) else result end end |
.simplify_structurized_result(result) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/cpee-eval-ruby/translation.rb', line 37 def self::simplify_structurized_result(result) if result && result.length == 1 if result[0].has_key? 'mimetype' if result[0]['mimetype'] == 'application/json' result = JSON::parse(CPEE::EvalRuby::Translation::extract_base64(result[0]['data'])) rescue nil elsif result[0]['mimetype'] == 'text/csv' result = CPEE::EvalRuby::Translation::extract_base64(result[0]['data']) elsif result[0]['mimetype'] == 'text/yaml' result = YAML::load(CPEE::EvalRuby::Translation::extract_base64(result[0]['data'])) rescue nil elsif result[0]['mimetype'] == 'application/xml' || result[0]['mimetype'] == 'text/xml' result = XML::Smart::string(CPEE::EvalRuby::Translation::extract_base64(result[0]['data'])) rescue nil elsif result[0]['mimetype'] == 'text/plain' result = CPEE::EvalRuby::Translation::extract_base64(result[0]['data']) if result.start_with?("<?xml version=") result = XML::Smart::string(result) else result = result.to_f if result == result.to_f.to_s result = result.to_i if result == result.to_i.to_s end elsif result[0]['mimetype'] == 'text/html' result = CPEE::EvalRuby::Translation::extract_base64(result[0]['data']) result = result.to_f if result == result.to_f.to_s result = result.to_i if result == result.to_i.to_s else result = result[0] end else result = result[0]['data'] end else result = ParamArray[*result] end if result.is_a? String enc = CPEE::EvalRuby::Translation::detect_encoding(result) enc == 'OTHER' ? result : (result.encode('UTF-8',enc) rescue CPEE::EvalRuby::Translation::convert_to_base64(result)) else result end end |
.structurize_result(result) ⇒ Object
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/cpee-eval-ruby/translation.rb', line 147 def self::structurize_result(result) result.map do |r| if r.is_a? Riddl::Parameter::Simple { 'name' => r.name, 'data' => r.value } elsif r.is_a? Riddl::Parameter::Complex res = if r.mimetype == 'application/json' ttt = r.value.read enc = CPEE::EvalRuby::Translation::detect_encoding(ttt) enc == 'OTHER' ? ttt.inspect : (ttt.encode('UTF-8',enc) rescue CPEE::EvalRuby::Translation::convert_to_base64(ttt)) elsif r.mimetype == 'text/csv' ttt = r.value.read enc = CPEE::EvalRuby::Translation::detect_encoding(ttt) enc == 'OTHER' ? ttt.inspect : (ttt.encode('UTF-8',enc) rescue CPEE::EvalRuby::Translation::convert_to_base64(ttt)) elsif r.mimetype == 'text/plain' || r.mimetype == 'text/html' ttt = r.value.read ttt = ttt.to_f if ttt == ttt.to_f.to_s ttt = ttt.to_i if ttt == ttt.to_i.to_s enc = CPEE::EvalRuby::Translation::detect_encoding(ttt) enc == 'OTHER' ? ttt.inspect : (ttt.encode('UTF-8',enc) rescue CPEE::EvalRuby::Translation::convert_to_base64(ttt)) else CPEE::EvalRuby::Translation::convert_to_base64(r.value.read) end tmp = { 'name' => r.name == '' ? 'result' : r.name, 'mimetype' => r.mimetype, 'data' => res.to_s } r.value.rewind tmp end end end |