Module: Jpmobile::Util

Defined in:
lib/jpmobile/util.rb

Constant Summary

SJIS =

SJIS = “Shift_JIS”

"Windows-31J"
UTF8 =
"UTF-8"
JIS =
"ISO-2022-JP"
JIS_WIN =
"CP50220"
BINARY =
"ASCII-8BIT"
WAVE_DASH =
[0x301c].pack("U")
FULLWIDTH_TILDE =
[0xff5e].pack("U")
OVERLINE =
[0x203e].pack("U")
FULLWIDTH_MACRON =
[0xffe3].pack("U")
EM_DASH =
[0x2014].pack("U")
HORIZONTAL_BAR =
[0x2015].pack("U")
MINUS_SIGN =
[0x2212].pack("U")
FULLWIDTH_HYPHEN_MINUS =
[0xFF0D].pack("U")

Class Method Summary collapse

Class Method Details

.ascii_8bit(str) ⇒ Object



97
98
99
100
101
102
# File 'lib/jpmobile/util.rb', line 97

def ascii_8bit(str)
  if str.respond_to?(:force_encoding) and !ascii_8bit?(str)
    str.force_encoding(BINARY)
  end
  str
end

.ascii_8bit?(str) ⇒ Boolean



291
292
293
# File 'lib/jpmobile/util.rb', line 291

def ascii_8bit?(str)
  detect_encoding(str) == BINARY
end

.ascii_compatible!(str) ⇒ Object



104
105
106
107
108
109
# File 'lib/jpmobile/util.rb', line 104

def ascii_compatible!(str)
  if str.respond_to?(:encoding) and !str.encoding.ascii_compatible?
    str.force_encoding(BINARY)
  end
  str
end

.check_charset(str, charset) ⇒ Object



350
351
352
353
# File 'lib/jpmobile/util.rb', line 350

def check_charset(str, charset)
  # use NKF.guess
  ::Encoding.compatible?(NKF.guess(str), ::Encoding.find(charset))
end

.correct_encoding(str) ⇒ Object



355
356
357
358
359
360
361
# File 'lib/jpmobile/util.rb', line 355

def correct_encoding(str)
  if str.encoding != ::Encoding::ASCII_8BIT and NKF.guess(str) != str.encoding
    str.force_encoding(NKF.guess(str))
  end

  str
end

.decode(str, encoding, charset) ⇒ Object



335
336
337
338
339
340
341
342
343
344
345
346
347
348
# File 'lib/jpmobile/util.rb', line 335

def decode(str, encoding, charset)
  _str = case encoding
         when /quoted-printable/i
           str.unpack('M').first.strip
         when /base64/i
           str.unpack('m').first.strip
         else
           str
         end

  _extract_charset = Jpmobile::Util.extract_charset(_str)
  charset = _extract_charset unless _extract_charset.blank? or _extract_charset == charset
  Jpmobile::Util.set_encoding(_str, charset)
end

.deep_apply(obj, &proc) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/jpmobile/util.rb', line 27

def deep_apply(obj, &proc)
  case obj
  when Hash
    obj.each_pair do |key, value|
      obj[key] = deep_apply(value, &proc)
    end
  when Array
    obj.collect!{|value| deep_apply(value, &proc)}
  when String
    obj = obj.to_param if obj.respond_to?(:to_param)
    proc.call(obj)
  else
    # NilClass, TrueClass, FalseClass, Tempfile, StringIO, etc...
    return obj
  end
end

.deep_convert(obj, &proc) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/jpmobile/util.rb', line 44

def deep_convert(obj, &proc)
  case obj
  when Hash
    new_obj = {}
    obj.each_pair do |key, value|
      new_obj[deep_convert(key.dup, &proc)] = deep_convert(value, &proc)
    end
    new_obj
  when Array
    new_obj = obj.map do |value|
      deep_convert(value, &proc)
    end
  when Symbol
    new_obj = proc.call(obj.to_s).to_sym
  when String
    obj = obj.to_param if obj.respond_to?(:to_param)
    new_obj = proc.call(obj)
  else
    # NilClass, TrueClass, FalseClass, Tempfile, StringIO, etc...
    new_obj = obj
  end

  new_obj
end

.detect_encoding(str) ⇒ Object



276
277
278
279
280
281
282
283
284
285
286
287
288
289
# File 'lib/jpmobile/util.rb', line 276

def detect_encoding(str)
  case str.encoding
  when ::Encoding::ISO2022_JP
    JIS
  when ::Encoding::Shift_JIS, ::Encoding::Windows_31J, ::Encoding::CP932
    SJIS
  when ::Encoding::UTF_8
    UTF8
  when ::Encoding::ASCII_8BIT
    BINARY
  else
    BINARY
  end
end

.emdash_to_horizontal_bar(utf8_str) ⇒ Object



227
228
229
# File 'lib/jpmobile/util.rb', line 227

def emdash_to_horizontal_bar(utf8_str)
  utf8_str.gsub(EM_DASH, HORIZONTAL_BAR)
end

.encode(str, charset) ⇒ Object



197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/jpmobile/util.rb', line 197

def encode(str, charset)
  if (charset.nil? or charset == "" or str.nil? or str == "")
    str
  elsif utf8?(str) and charset.match(/iso-2022-jp/i)
    utf8_to_jis(str)
  elsif utf8?(str) and charset.match(/shift_jis/i)
    utf8_to_sjis(str)
  elsif utf8?(str) and charset.match(/utf-8/i)
    str
  else
    str.encode(charset)
  end
end

.extract_charset(str) ⇒ Object



263
264
265
266
267
268
269
270
271
272
273
274
# File 'lib/jpmobile/util.rb', line 263

def extract_charset(str)
  case str
  when /iso-2022-jp/i
    "ISO-2022-JP"
  when /shift_jis/i
    "Shift_JIS"
  when /utf-8/i
    "UTF-8"
  else
    ""
  end
end

.fold_text(str, size = 15) ⇒ Object



304
305
306
307
308
309
310
311
312
313
# File 'lib/jpmobile/util.rb', line 304

def fold_text(str, size = 15)
  folded_texts = []

  while texts = split_text(str, size) and texts.first.size != 0
    folded_texts << texts.first
    str = texts.last
  end

  folded_texts
end

.force_encode(str, from, to) ⇒ Object



239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/jpmobile/util.rb', line 239

def force_encode(str, from, to)
  s = str.dup
  return str if detect_encoding(str) == to

  to = SJIS if to =~ /shift_jis/i

  to_enc = ::Encoding.find(to)
  return str if s.encoding == to_enc

  if from
    from_enc = ::Encoding.find(from)
    s.force_encoding(from) unless s.encoding == from_enc
  end

  s.encode(to)
end

.fullwidth_hyphen_minus_to_minus_sign(utf8_str) ⇒ Object



235
236
237
# File 'lib/jpmobile/util.rb', line 235

def fullwidth_hyphen_minus_to_minus_sign(utf8_str)
  utf8_str.gsub(FULLWIDTH_HYPHEN_MINUS, MINUS_SIGN)
end

.fullwidth_macron_to_overline(utf8_str) ⇒ Object



223
224
225
# File 'lib/jpmobile/util.rb', line 223

def fullwidth_macron_to_overline(utf8_str)
  utf8_str.gsub(FULLWIDTH_MACRON, OVERLINE)
end

.fullwidth_tilde_to_wavedash(utf8_str) ⇒ Object



215
216
217
# File 'lib/jpmobile/util.rb', line 215

def fullwidth_tilde_to_wavedash(utf8_str)
  utf8_str.gsub(FULLWIDTH_TILDE, WAVE_DASH)
end

.hash_to_utf8(hash) ⇒ Object



170
171
172
173
174
175
# File 'lib/jpmobile/util.rb', line 170

def hash_to_utf8(hash)
  new_hash = {}
  hash.each do |keu, value|
    new_hash[utf8(key)] = utf8(value)
  end
end

.invert_table(hash) ⇒ Object



321
322
323
324
325
326
327
328
329
330
331
332
333
# File 'lib/jpmobile/util.rb', line 321

def invert_table(hash)
  result = {}
  hash.keys.each do |key|
    if result[hash[key]]
      if !key.kind_of?(Array) and !result[hash[key]].kind_of?(Array) and result[hash[key]] > key
        result[hash[key]] = key
      end
    else
      result[hash[key]] = key
    end
  end
  result
end

.jis(str) ⇒ Object



83
84
85
86
87
88
# File 'lib/jpmobile/util.rb', line 83

def jis(str)
  if str.respond_to?(:force_encoding) and !jis?(str)
    str.force_encoding(JIS)
  end
  str
end

.jis?(str) ⇒ Boolean



300
301
302
# File 'lib/jpmobile/util.rb', line 300

def jis?(str)
  detect_encoding(str) == JIS
end

.jis_regexp(jis) ⇒ Object



187
188
189
190
191
# File 'lib/jpmobile/util.rb', line 187

def jis_regexp(jis)
  jis_str = jis.kind_of?(Numeric) ? [jis].pack('n') : jis

  Regexp.compile(Regexp.escape(jis_str.force_encoding(BINARY)))
end

.jis_string_regexpObject



193
194
195
# File 'lib/jpmobile/util.rb', line 193

def jis_string_regexp
  Regexp.compile(Regexp.escape(ascii_8bit("\x1b\x24\x42")) + "(.+?)" + Regexp.escape(ascii_8bit("\x1b\x28\x42")))
end

.jis_to_utf8(jis_str) ⇒ Object



154
155
156
157
158
159
160
# File 'lib/jpmobile/util.rb', line 154

def jis_to_utf8(jis_str)
  if jis_str.respond_to?(:encode)
    jis_str.encode(UTF8, :universal_newline => true)
  else
    NKF.nkf("-m0 -x -Jw", jis_str).gsub(/\r\n?/, "\n")
  end
end

.jis_win(str) ⇒ Object



90
91
92
93
94
95
# File 'lib/jpmobile/util.rb', line 90

def jis_win(str)
  if str.respond_to?(:force_encoding) and !jis?(str)
    str.force_encoding(JIS_WIN)
  end
  str
end

.minus_sign_to_fullwidth_hyphen_minus(utf8_str) ⇒ Object



231
232
233
# File 'lib/jpmobile/util.rb', line 231

def minus_sign_to_fullwidth_hyphen_minus(utf8_str)
  utf8_str.gsub(MINUS_SIGN, FULLWIDTH_HYPHEN_MINUS)
end

.overline_to_fullwidth_macron(utf8_str) ⇒ Object



219
220
221
# File 'lib/jpmobile/util.rb', line 219

def overline_to_fullwidth_macron(utf8_str)
  utf8_str.gsub(OVERLINE, FULLWIDTH_MACRON)
end

.regexp_to_sjis(sjis_str) ⇒ Object



166
167
168
# File 'lib/jpmobile/util.rb', line 166

def regexp_to_sjis(sjis_str)
  Regexp.compile(Regexp.escape(sjis(sjis_str)))
end

.regexp_utf8_to_sjis(utf8_str) ⇒ Object



162
163
164
# File 'lib/jpmobile/util.rb', line 162

def regexp_utf8_to_sjis(utf8_str)
  Regexp.compile(Regexp.escape(utf8_to_sjis(utf8_str)))
end

.set_encoding(str, encoding) ⇒ Object



256
257
258
259
260
261
# File 'lib/jpmobile/util.rb', line 256

def set_encoding(str, encoding)
  encoding = SJIS if encoding =~ /shift_jis/i
  str.force_encoding(encoding)

  str
end

.shift_jis?(str) ⇒ Boolean



297
298
299
# File 'lib/jpmobile/util.rb', line 297

def shift_jis?(str)
  detect_encoding(str) == SJIS
end

.sjis(str) ⇒ Object



69
70
71
72
73
74
# File 'lib/jpmobile/util.rb', line 69

def sjis(str)
  if str.respond_to?(:force_encoding) and !shift_jis?(str)
    str.force_encoding(SJIS)
  end
  str
end

.sjis_regexp(sjis) ⇒ Object



177
178
179
180
181
182
183
184
185
# File 'lib/jpmobile/util.rb', line 177

def sjis_regexp(sjis)
  sjis_str = if sjis.kind_of?(Numeric)
               [sjis].pack('n')
             else
               sjis
             end

  Regexp.compile(Regexp.escape(sjis_str.force_encoding(SJIS)))
end

.sjis_to_utf8(sjis_str) ⇒ Object



130
131
132
133
134
135
136
137
138
139
# File 'lib/jpmobile/util.rb', line 130

def sjis_to_utf8(sjis_str)
  utf8_str = if sjis_str.respond_to?(:encode)
               sjis_str.encode("UTF-8", :universal_newline => true)
             else
               NKF.nkf("-m0 -x -w --ic=cp932", sjis_str).gsub(/\r\n?/, "\n")
             end

  # 波ダッシュ対策
  fullwidth_tilde_to_wavedash(utf8_str)
end

.split_text(str, size = 15) ⇒ Object



315
316
317
318
319
# File 'lib/jpmobile/util.rb', line 315

def split_text(str, size = 15)
  return nil if str.nil? or str == ''

  [str[0..(size-1)], str[size..-1]]
end

.utf8(str) ⇒ Object



76
77
78
79
80
81
# File 'lib/jpmobile/util.rb', line 76

def utf8(str)
  if str.respond_to?(:force_encoding) and !utf8?(str)
    str.force_encoding(UTF8)
  end
  str
end

.utf8?(str) ⇒ Boolean



294
295
296
# File 'lib/jpmobile/util.rb', line 294

def utf8?(str)
  detect_encoding(str) == UTF8
end

.utf8_to_jis(utf8_str) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/jpmobile/util.rb', line 141

def utf8_to_jis(utf8_str)
  # 波ダッシュ対策
  utf8_str = fullwidth_tilde_to_wavedash(utf8_str)

  if utf8_str.respond_to?(:encode)
    utf8_str.
    gsub(/(\r\n|\r|\n)/, "\r\n").
    encode(JIS, :undef => :replace, :replace => '?')
  else
    NKF.nkf("-m0 -x -Wj --fb-subchar=63", utf8_str).gsub(/(\r\n|\r|\n)/, "\r\n")
  end
end

.utf8_to_sjis(utf8_str) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/jpmobile/util.rb', line 111

def utf8_to_sjis(utf8_str)
  # 波ダッシュ対策
  utf8_str = wavedash_to_fullwidth_tilde(utf8_str)
  # オーバーライン対策(不可逆的)
  utf8_str = overline_to_fullwidth_macron(utf8_str)
  # ダッシュ対策(不可逆的)
  utf8_str = emdash_to_horizontal_bar(utf8_str)
  # マイナス対策(不可逆的)
  utf8_str = minus_sign_to_fullwidth_hyphen_minus(utf8_str)

  if utf8_str.respond_to?(:encode)
    utf8_str.
    gsub(/(\r\n|\r|\n)/, "\r\n").
    encode(SJIS, :undef => :replace, :replace => '?')
  else
    NKF.nkf("-m0 -x -W --oc=cp932 --fb-subchar=63", utf8_str).gsub(/(\r\n|\r|\n)/, "\r\n")
  end
end

.wavedash_to_fullwidth_tilde(utf8_str) ⇒ Object



211
212
213
# File 'lib/jpmobile/util.rb', line 211

def wavedash_to_fullwidth_tilde(utf8_str)
  utf8_str.gsub(WAVE_DASH, FULLWIDTH_TILDE)
end