e4u 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,30 @@
1
+ $LOAD_PATH.unshift File.dirname(__FILE__)
2
+
3
+ require 'e4u/base'
4
+ require 'e4u/docomo'
5
+ require 'e4u/kddi'
6
+ require 'e4u/softbank'
7
+ require 'e4u/google'
8
+ require 'e4u/util'
9
+
10
+ module E4U
11
+ def self.docomo
12
+ @docomo ||= DoCoMo.new
13
+ end
14
+
15
+ def self.kddi
16
+ @kddi ||= KDDI.new
17
+ end
18
+
19
+ def self.kddiweb
20
+ self.kddi
21
+ end
22
+
23
+ def self.softbank
24
+ @softbank ||= Softbank.new
25
+ end
26
+
27
+ def self.google
28
+ @google ||= Google.new
29
+ end
30
+ end
@@ -0,0 +1,77 @@
1
+ require 'rexml/document'
2
+
3
+ module E4U
4
+ class Base
5
+ include Enumerable
6
+
7
+ def each
8
+ data.each do |element|
9
+ yield element
10
+ end
11
+ self
12
+ end
13
+
14
+ private
15
+
16
+ def data
17
+ return @data if @data
18
+ @data = []
19
+ xml.root.each_element('//e') do |element|
20
+ hash = {}
21
+ element.attributes.each{ |k,v| hash[k.to_sym] = v.to_s }
22
+ if element.has_elements?
23
+ hash[:desc] = element.get_text('desc').to_s if element.get_text('desc')
24
+ hash[:ann] = element.get_text('ann').to_s if element.get_text('ann')
25
+ end
26
+ @data << emojinize(hash)
27
+ end
28
+ @data.recursive_freeze
29
+ end
30
+
31
+ def xml
32
+ opt = 'rb'
33
+ opt+= ':utf-8' unless RUBY_VERSION < '1.9'
34
+ @xml ||= REXML::Document.new(File.open(path, opt){ |f| f.read })
35
+ end
36
+
37
+ def emojinize object
38
+ object.extend Base::Emojinize
39
+ end
40
+ end
41
+
42
+ module Base::Emojinize
43
+ def emojiize
44
+ Base::Emoji.new self
45
+ end
46
+ end
47
+
48
+ class Base::Emoji
49
+ def initialize attributes
50
+ @attributes = attributes
51
+ @attributes.each do |key, value|
52
+ next if key =~ /\A(id|object_id|__(id|send)__)\z/
53
+ self.class.class_eval("def #{key}; @attributes[:#{key}]; end", __FILE__, __LINE__)
54
+ end
55
+ end
56
+
57
+ def alternate?
58
+ !!(unicode =~ /\A>/)
59
+ end
60
+
61
+ def utf8
62
+ hex = unicode.sub(/\A[\>\*\+]/, '')
63
+ raise if hex.size == 0
64
+ hex.split(/\+/, -1).map{ |ch| ch.hex }.pack('U')
65
+ end
66
+
67
+ def cp932
68
+ hex = unicode.sub(/\A[\>\*\+]/, '')
69
+ raise if hex.size == 0
70
+ chr = hex.split(/\+/, -1).map{ |ch| unicode_to_cp932(ch.hex) }.pack('n')
71
+ chr.force_encoding('CP932') if chr.respond_to? :force_encoding
72
+ chr
73
+ end
74
+ alias :sjis :cp932
75
+
76
+ end
77
+ end
@@ -0,0 +1,37 @@
1
+ require 'e4u/base'
2
+
3
+ module E4U
4
+ class DoCoMo < Base
5
+ private
6
+
7
+ def path
8
+ File.expand_path(File.join('..', '..', 'data', 'docomo', 'carrier_data.xml'), File.dirname(__FILE__))
9
+ end
10
+
11
+ def emojinize object
12
+ object.extend DoCoMo::Emojinize
13
+ end
14
+ end
15
+
16
+ module DoCoMo::Emojinize
17
+ def docomo_emoji
18
+ DoCoMo::Emoji.new self
19
+ end
20
+ end
21
+
22
+ class DoCoMo::Emoji < Base::Emoji
23
+ private
24
+
25
+ def unicode_to_cp932 octet
26
+ return if octet < 0xE63E or octet > 0xE757
27
+
28
+ if octet <= 0xE69B
29
+ octet + 4705
30
+ elsif octet <= 0xE6DA
31
+ octet + 4772
32
+ else
33
+ octet + 4773
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,60 @@
1
+ require 'e4u/base'
2
+
3
+ module E4U
4
+ class Google < Base
5
+ private
6
+
7
+ def path
8
+ File.expand_path(File.join('..', '..', 'data', 'emoji4unicode.xml'), File.dirname(__FILE__))
9
+ end
10
+
11
+ def emojinize object
12
+ object.extend Google::Emojinize
13
+ end
14
+ end
15
+
16
+ module Google::Emojinize
17
+ def docomo_emoji
18
+ DoCoMo::Emoji.new :unicode => self[:docomo]
19
+ end
20
+
21
+ def kddi_emoji
22
+ KDDI::Emoji.new :unicode => self[:kddi]
23
+ end
24
+
25
+ def kddiweb_emoji
26
+ KDDIWeb::Emoji.new :unicode => self[:kddi]
27
+ end
28
+
29
+ def softbank_emoji
30
+ Softbank::Emoji.new :unicode => self[:softbank]
31
+ end
32
+
33
+ def unicode_emoji
34
+ Google::Emoji.new :unicode => self[:unicode]
35
+ end
36
+
37
+ def google_emoji
38
+ Google::Emoji.new self
39
+ end
40
+ end
41
+
42
+ class Google::Emoji < Base::Emoji
43
+ def proposal?
44
+ @attributes[:in_proposal] == 'yes'
45
+ end
46
+
47
+ def translate carrier
48
+ case carrier.to_s.downcase
49
+ when 'docomo'
50
+ E4U.docomo.find{ |e| e[:unicode] == docomo }.docomo_emoji
51
+ when 'kddi'
52
+ E4U.kddi.find{ |e| e[:unicode] == kddi }.kddi_emoji
53
+ when 'softbank'
54
+ E4U.softbank.find{ |e| e[:unicode] == softbank }.softbank_emoji
55
+ else
56
+ raise ArgumentError
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,69 @@
1
+ require 'e4u/base'
2
+
3
+ module E4U
4
+ class KDDI < Base
5
+ private
6
+
7
+ def path
8
+ File.expand_path(File.join('..', '..', 'data', 'kddi', 'carrier_data.xml'), File.dirname(__FILE__))
9
+ end
10
+
11
+ def emojinize object
12
+ object.extend KDDI::Emojinize
13
+ end
14
+ end
15
+
16
+ module KDDI::Emojinize
17
+ def kddi_emoji
18
+ KDDI::Emoji.new self
19
+ end
20
+ def kddiweb_emoji
21
+ KDDIWeb::Emoji.new self
22
+ end
23
+ end
24
+
25
+ class KDDI::Emoji < Base::Emoji
26
+ private
27
+
28
+ def unicode_to_cp932 octet
29
+ if octet >= 0xE468 and octet <= 0xE5DF
30
+ sjis = if octet <= 0xE4A6
31
+ octet + 4568
32
+ elsif octet <= 0xE523
33
+ octet + 4569
34
+ elsif octet <= 0xE562
35
+ octet + 4636
36
+ elsif octet <= 0xE5B4
37
+ octet + 4637
38
+ elsif octet <= 0xE5CC
39
+ octet + 4656
40
+ else
41
+ octet + 3443
42
+ end
43
+ elsif octet >= 0xEA80 and octet <= 0xEB8E
44
+ sjis = if octet <= 0xEAAB
45
+ octet + 2259
46
+ elsif octet <= 0xEAFA
47
+ octet + 2260
48
+ elsif octet <= 0xEB0D
49
+ octet + 3287
50
+ elsif octet <= 0xEB3B
51
+ octet + 2241
52
+ elsif octet <= 0xEB7A
53
+ octet + 2308
54
+ else
55
+ octet + 2309
56
+ end
57
+ end
58
+ end
59
+ end
60
+
61
+ class KDDIWeb < KDDI
62
+ end
63
+
64
+ class KDDIWeb::Emoji < KDDI::Emoji
65
+ def utf8
66
+ sjis.unpack('n*').map{ |char| char - 1792 }.pack('U')
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,35 @@
1
+ require 'e4u/base'
2
+
3
+ module E4U
4
+ class Softbank < Base
5
+ private
6
+
7
+ def path
8
+ File.expand_path(File.join('..', '..', 'data', 'softbank', 'carrier_data.xml'), File.dirname(__FILE__))
9
+ end
10
+
11
+ def emojinize object
12
+ object.extend Softbank::Emojinize
13
+ end
14
+ end
15
+
16
+ module Softbank::Emojinize
17
+ def softbank_emoji
18
+ Softbank::Emoji.new self
19
+ end
20
+ end
21
+
22
+ class Softbank::Emoji < Base::Emoji
23
+ private
24
+
25
+ def unicode_to_cp932 octet
26
+ return if octet < 0xE001 or octet > 0xE55A
27
+
28
+ page = (octet >> 8) & 7
29
+ sjisHi = [0xF9, 0xF7, 0xF7, 0xF9, 0xFB, 0xFB][page]
30
+ sjisLo = [0x40, 0x40, 0xA0, 0xA0, 0x40, 0xA0][page] + (octet & 0x7F)
31
+ sjisLo += 1 if sjisLo > 0x7E and sjisLo < 0xA1
32
+ sjisHi << 8 | sjisLo
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,25 @@
1
+ # -*- coding: utf-8 -*-
2
+ class Object
3
+ def recursive_freeze
4
+ case self
5
+ when Hash
6
+ self.each_value{ |e| e.recursive_freeze }
7
+ when Array
8
+ self.each{ |e| e.recursive_freeze }
9
+ end
10
+ self.freeze
11
+ end
12
+ end
13
+
14
+ if __FILE__ == $PROGRAM_NAME
15
+ a = Array.new(10){|i| {:c => "#{i}"} }
16
+
17
+ # 本当は Object.freeze! のがよかったなぁ
18
+ a.recursive_freeze
19
+
20
+ puts a.frozen?
21
+ a.each do |b|
22
+ puts b.frozen?
23
+ puts b[:c].frozen?
24
+ end
25
+ end
@@ -0,0 +1,54 @@
1
+ # -*- coding: utf-8 -*-
2
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
3
+
4
+ describe E4U::DoCoMo do
5
+ before :all do
6
+ @docomo = E4U.docomo
7
+ end
8
+
9
+ it "should include Enumerable" do
10
+ @docomo.should be_kind_of Enumerable
11
+ end
12
+
13
+ it "should include E4U::DoCoMo::Emojinize" do
14
+ @docomo.first.should be_kind_of E4U::DoCoMo::Emojinize
15
+ end
16
+
17
+ it "すべての要素に:unicodeをキーとしたデータはあること" do
18
+ @docomo.each do |emj|
19
+ emj.should have_key(:unicode)
20
+ end
21
+ end
22
+
23
+ it "正しくデータを取得できること" do
24
+ # <e jis="7541" name_en="Fine" name_ja="晴れ" unicode="E63E"/>
25
+ emj = @docomo.find{ |e| e[:name_en] == 'Fine' }
26
+ emj[:unicode].should == 'E63E'
27
+ emj[:name_en].should == 'Fine'
28
+ emj[:name_ja].should == '晴れ'
29
+ emj[:jis].should == '7541'
30
+ end
31
+
32
+ it "docomo_emojiでE4U::DoCoMo::Emojiが返ってくること" do
33
+ emj = @docomo.find{ |e| e[:name_en] == 'Fine' }
34
+ emj.docomo_emoji.should be_instance_of E4U::DoCoMo::Emoji
35
+ end
36
+
37
+ describe E4U::DoCoMo::Emoji do
38
+ before :all do
39
+ @emj = @docomo.find{ |e| e[:unicode] == 'E63E' }.docomo_emoji
40
+ end
41
+
42
+ it "utf8が返ってくること" do
43
+ @emj.utf8.should == [0xE63E].pack('U')
44
+ end
45
+
46
+ it "sjisが返ってくること" do
47
+ @emj.sjis.dump.should == "\xF8\x9F".dump
48
+ end
49
+
50
+ it "alternate?が返ってくること" do
51
+ @emj.alternate?.should be_false
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,14 @@
1
+ # -*- coding: utf-8 -*-
2
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
3
+
4
+ describe "E4U" do
5
+ it "インスタンスが返ってくること" do
6
+ { :google => E4U::Google,
7
+ :docomo => E4U::DoCoMo,
8
+ :kddi => E4U::KDDI,
9
+ :kddiweb => E4U::KDDI,
10
+ :softbank => E4U::Softbank }.each{ |method, klass|
11
+ E4U.method(method).call.should be_an_instance_of klass
12
+ }
13
+ end
14
+ end
@@ -0,0 +1,116 @@
1
+ # -*- coding: utf-8 -*-
2
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
3
+
4
+ describe E4U::Google do
5
+ before :all do
6
+ @google = E4U.google
7
+ end
8
+
9
+ it "should include Enumerable" do
10
+ @google.should be_kind_of Enumerable
11
+ end
12
+
13
+ it "should E4U::Google::Emojinize" do
14
+ @google.first.should be_kind_of E4U::Google::Emojinize
15
+ end
16
+
17
+ it "すべての要素に:idをキーとしたデータはあること" do
18
+ @google.each do |emj|
19
+ emj.should have_key(:id)
20
+ end
21
+ end
22
+
23
+ it "正しくデータを取得できること" do
24
+ # つーかハッシュだし
25
+ # <e docomo="E63E" google="FE000" id="000" kddi="E488" name="BLACK SUN WITH RAYS" softbank="E04A" unicode="2600">
26
+ emj = @google.find{ |e| e[:id] == '000' }
27
+ emj[:docomo].should == 'E63E'
28
+ emj[:google].should == 'FE000'
29
+ emj[:kddi].should == 'E488'
30
+ emj[:name].should == 'BLACK SUN WITH RAYS'
31
+ emj[:softbank].should == 'E04A'
32
+ emj[:unicode].should == '2600'
33
+ end
34
+
35
+ it "docomo_emojiでE4U::DoCoMo::Emojiが返ってくること" do
36
+ de = @google.find{ |e| e[:id] == '000' }.docomo_emoji
37
+ de.should be_instance_of E4U::DoCoMo::Emoji
38
+ de.utf8.should == [0xE63E].pack('U')
39
+ de.sjis.dump.should == "\xF8\x9F".dump
40
+ end
41
+
42
+ it "kddi_emojiでE4U::KDDI::Emojiが返ってくること" do
43
+ ke = @google.find{ |e| e[:id] == '000' }.kddi_emoji
44
+ ke.should be_instance_of E4U::KDDI::Emoji
45
+ ke.utf8.should == [0xE488].pack('U')
46
+ ke.sjis.dump.should == "\xF6\x60".dump
47
+ end
48
+
49
+ it "softbank_emojiでE4U::Softbank::Emojiが返ってくること" do
50
+ se = @google.find{ |e| e[:id] == '000' }.softbank_emoji
51
+ se.should be_instance_of E4U::Softbank::Emoji
52
+ se.utf8.should == [0xE04A].pack('U')
53
+ se.sjis.dump.should == "\xF9\x8B".dump
54
+ end
55
+
56
+ describe E4U::Google::Emoji do
57
+ before :all do
58
+ @emj = @google.find{ |e| e[:id] == '000' }.google_emoji
59
+ end
60
+
61
+ it "docomoが返ってくること" do
62
+ @emj.docomo.should == 'E63E'
63
+ @emj.docomo.hex.should == 0xE63E
64
+ end
65
+
66
+ it "kddiが返ってくること" do
67
+ @emj.kddi.should == 'E488'
68
+ @emj.kddi.hex.should == 0xE488
69
+ end
70
+
71
+ it "softbankが返ってくること" do
72
+ @emj.softbank.should == 'E04A'
73
+ @emj.softbank.hex.should == 0xE04A
74
+ end
75
+
76
+ it "descが返ってくること" do
77
+ @emj.desc.should be_an_instance_of String
78
+ @emj.desc.should match(/clear weather/i)
79
+ end
80
+
81
+ it "translate(:docomo)でDoCoMo絵文字に変換できること" do
82
+ de = @emj.translate :docomo
83
+ de.utf8.should == [0xE63E].pack('U')
84
+ end
85
+
86
+ it "translate(:kddi)でKDDI絵文字に変換できること" do
87
+ de = @emj.translate :kddi
88
+ de.utf8.should == [0xE488].pack('U')
89
+ end
90
+
91
+ it "translate(:softbank)でSoftbank絵文字に変換できること" do
92
+ de = @emj.translate :softbank
93
+ de.utf8.should == [0xE04A].pack('U')
94
+ end
95
+
96
+ it "translateでArgumentErrorが起こること" do
97
+ lambda{ @emj.translate :emobile }.should raise_error ArgumentError
98
+ end
99
+
100
+ it "proposal?が返ってくること" do
101
+ @emj.proposal?.should be_false
102
+ end
103
+
104
+ it "text_fallbackが返ってくること" do
105
+ @google.find{ |e| e[:google] == 'FE006' }.google_emoji.text_fallback.should == '[霧]'
106
+ end
107
+
108
+ it "utf8が返ってくること" do
109
+ @emj.utf8.should == [0x2600].pack('U')
110
+ end
111
+
112
+ it "alternate?が返ってくること" do
113
+ @emj.alternate?.should be_false
114
+ end
115
+ end
116
+ end