update.
[chise/ruby.git] / chise / kageserver.rb
index 26a956d..5b0224f 100755 (executable)
@@ -3,32 +3,38 @@
 require "singleton"
 require "net/http"
 require "uri"
-$LOAD_PATH << "../../lib"
 require "chise"
-require "chise/network" #漢字間接続のネットワークを計算する
-#require "chise/kanjilist"
-include CHISE
+require "chise/network"
+require "chise/kanjilist"
 
 class KageServer
   include Singleton
-  TYPES = "skeleton mincho gothic".split
+
+  TYPES = %w(skeleton mincho gothic)
   SKELETON = 0
   MINCHO = 1
   GOTHIC = 2
   URL  = "http://192.168.2.60:5100/"
+
   def initialize(url=URL)
     @url = url
     @glyphs = []
-    @use_cache = true #デフォルト: cacheに存在する場合はcacheから引き出す。
-    @offline = false #テスト用
-    @offline = true #テスト用
+    @use_cache = true # retriev from cache.  default.
+    # @offline = false
+    @offline = true # for test
     c = Config.instance
     @cache_dir = c.base_dir+"/cache_kage"
     Dir.mkdir(@cache_dir) unless FileTest.directory?(@cache_dir)
   end
   attr_accessor :url, :use_cache, :offline
-  def filename(num, type=SKELETON) sprintf("u%04x.%s", num, TYPES[type]) end
-  def cache_file(num, type=SKELETON) @cache_dir+"/"+filename(num, type)+".svg" end
+
+  def filename(num, type=SKELETON)
+    sprintf("u%04x.%s", num, TYPES[type])
+  end
+
+  def cache_file(num, type=SKELETON)
+    @cache_dir+"/"+filename(num, type)+".svg"
+  end
 
   def list_cache
     ar = []
@@ -43,15 +49,19 @@ class KageServer
   end
 
   def get(num, type=SKELETON)
-    return open(cache_file(num, type)).read if FileTest.exist?(cache_file(num, type))
+    if FileTest.exist?(cache_file(num, type))
+      return open(cache_file(num, type)) {|f| f.read }
+    end
     svg = get_http(num, type)
-    return svg if svg
-    return nil
+    svg
   end
+
   def get_http(num, type=SKELETON)
     return nil if @offline
+
     uri = URI.parse(URL + filename(num, type))
     p ["uri", uri.to_s]
+
     Net::HTTP.version_1_1   # declear to use 1.1 features.
     Net::HTTP.start( uri.host, uri.port ) {|http|
       response, body = http.get("/"+filename(num, type)+".svg")
@@ -65,17 +75,20 @@ class KageServer
        end
       end
     }
-    return nil
+    nil
   end
+
   def store_cache(num, type, svg)
     #p ["store", num]
     open(cache_file(num, type), "w") {|f|
       f.print svg
     }
   end
+
   def error?(svg)
     (svg =~ /<!-- error -->/)
   end
+
   def read_list
     h = {}
     open("kage-list.txt"){|f|
@@ -91,6 +104,7 @@ class KageServer
     }
     return h
   end
+
   def get_all
     #error_h = read_list
     STDOUT.binmode
@@ -118,6 +132,7 @@ class KageServer
     }
     get_ar(ar)
   end
+
   def get_ar(ar)
     ar.each {|num| #intの数列
       char = Character.get(num)
@@ -133,6 +148,7 @@ class KageServer
       }
     }
   end
+
   def test_kanji
     char = "&CDP-8BA5;".de_er
     #p char.inspect_all