# $Id: sum_price.rb,v 1.2 2003-05-01 02:34:44+09 waka Exp waka $ =begin sum_price.rb nDiary Plug-in amazon.rb、amazondvd.rb で取得した商品の合計を算出し表示する option: dir=str str にはキャッシュディレクトリのあるディレクトリを指定 example: or =end class Numeric def c_sep digit = Array.new if self.zero? then return self end if self < 0 then target = self.abs minus = "-" else target = self minus = "" end mod = target.to_s.length.modulo(3) if mod.nonzero? then digit << target.to_s[0..mod-1] << target.to_s[mod..-1].scan(/\d{3}/) else digit = target.to_s.scan(/\d{3}/) end digit = minus + digit.join(",") return digit end end def plug(opt) dirname = ['amzcache','amzcacheDVD'] i = 0 prices = Array.new dirname.each do |f| cache_dir = opt['dir'] + f price_data = Struct.new("Price", :media, :price, :id) dir = Dir.open(cache_dir) media = price = id ="" dir.each do |x| cache = File.join(cache_dir,x) id = File.basename(x) if !File.directory?(cache) then cacheF = File.open(cache) while line = cacheF.gets price = line.split("\t")[5] media = line.split("\t")[11] price = price.gsub(/[^0-9]/,"").to_i end prices[i] = price_data.new(media, price, id) i += 1 end end dir.close end total = dvd = cd = book = game = 0 prices.each {|p| if /ゲーム機本体|Xbox|GAMECUBE|PlayStation/i =~ p.media then game = p.price.to_i + game elsif /DVD/ =~ p.media then dvd = p.price.to_i + dvd elsif /CD/ =~ p.media then cd = p.price.to_i + cd else book = p.price.to_i + book end } total = dvd + cd + book + game str0 = Array.new str = "" str0 << "[DVD \\" + dvd.c_sep.to_s + "]" unless dvd.zero? str0 << "[CD \\" + cd.c_sep.to_s + "]" unless cd.zero? str0 << "[Game \\" + game.c_sep.to_s + "]" unless game.zero? str0 << "[Book \\" + book.c_sep.to_s + "]" unless book.zero? str << "