Class: Emoticon::Transcoder

Inherits:
Object
  • Object
show all
Includes:
ConversionTable, Singleton
Defined in:
lib/emoticon/transcoder.rb

Direct Known Subclasses

Au, Docomo, Null, Softbank

Defined Under Namespace

Classes: Au, Docomo, Jphone, Null, Softbank

Constant Summary collapse

Vodafone =
Emoticon::Transcoder::Softbank

Constants included from ConversionTable

ConversionTable::AU_EMAILJIS_TO_UNICODE, ConversionTable::AU_SJIS_REGEXP, ConversionTable::AU_SJIS_TO_UNICODE, ConversionTable::AU_UNICODE_TO_SJIS, ConversionTable::CONVERSION_TABLE_TO_AU, ConversionTable::CONVERSION_TABLE_TO_DOCOMO, ConversionTable::CONVERSION_TABLE_TO_SOFTBANK, ConversionTable::DOCOMO_SJIS_REGEXP, ConversionTable::DOCOMO_SJIS_TO_UNICODE, ConversionTable::DOCOMO_UNICODE_TO_SJIS, ConversionTable::EMOTICON_UNICODES, ConversionTable::SJIS_REGEXP, ConversionTable::SJIS_TO_UNICODE, ConversionTable::SOFTBANK_UNICODE_REGEXP, ConversionTable::SOFTBANK_UNICODE_TO_WEBCODE, ConversionTable::SOFTBANK_WEBCODE_REGEXP, ConversionTable::SOFTBANK_WEBCODE_TO_UNICODE, ConversionTable::UNICODE_TO_SJIS, ConversionTable::UTF8_REGEXP

Instance Method Summary collapse

Instance Method Details

#internal_to_external(s) ⇒ Object



60
61
62
# File 'lib/emoticon/transcoder.rb', line 60

def internal_to_external(s)
  unicodecr_to_external(utf8_to_unicodecr(s))
end

#unicodecr_to_external(str) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/emoticon/transcoder.rb', line 11

def unicodecr_to_external(str)
  str.gsub(/&#x([0-9a-f]{4});/i) do |match|
    unicode = $1.scanf("%x").first
    if conversion_table
      converted = conversion_table[unicode] # キャリア間変換
    else
      converted = unicode # 変換しない
    end

    # 携帯側エンコーディングに変換する
    case converted
    when Integer
      # 変換先がUnicodeで指定されている。つまり対応する絵文字がある。
      if sjis = UNICODE_TO_SJIS[converted]
        [sjis].pack('n')
      elsif webcode = SOFTBANK_UNICODE_TO_WEBCODE[converted-0x1000]
        "\x1b\x24#{webcode}\x0f"
      else
        # キャリア変換テーブルに指定されていたUnicodeに対応する
        # 携帯側エンコーディングが見つからない(変換テーブルの不備の可能性あり)。
        match
      end
    when String
      # 変換先がUnicodeで指定されている。
      to_sjis ? Kconv::kconv(converted, Kconv::SJIS, Kconv::UTF8) : converted
    when nil
      # 変換先が定義されていない。
      match
    end
  end
end

#unicodecr_to_utf8(str) ⇒ Object



49
50
51
52
53
54
55
56
57
58
# File 'lib/emoticon/transcoder.rb', line 49

def unicodecr_to_utf8(str)
  str.gsub(/&#x([0-9a-f]{4});/i) do |match|
    unicode = $1.scanf("%x").first
    if UNICODE_TO_SJIS[unicode] || SOFTBANK_UNICODE_TO_WEBCODE[unicode-0x1000]
      [unicode].pack('U')
    else
      match
    end
  end
end

#utf8_to_unicodecr(str) ⇒ Object



43
44
45
46
47
# File 'lib/emoticon/transcoder.rb', line 43

def utf8_to_unicodecr(str)
  str.gsub(UTF8_REGEXP) do |match|
    "&#x%04x;" % match.unpack('U').first
  end
end