class Syachicky::Syachicky

Public Class Methods

new(bland, date=Date.today-36.hour, maxCacheDays=365*3) click to toggle source
# File lib/syachicky.rb, line 9
    def initialize(bland, date=Date.today-36.hour, maxCacheDays=365*3)
    # 銘柄コード
    @bland = bland.to_i
    
    # 取得データ日
    @day = date
    @dayString = @day.strftime("%Y/%m/%d")
    
    # データの保持期間
    @maxCacheDays = maxCacheDays
    
    # 最終日データ
    @lastDayData = nil
    
    # dataディレクトリ
    @dataDir = File.expand_path(File.dirname(__FILE__)) + "/../data"
end

Public Instance Methods

dataUpdate() click to toggle source
# File lib/syachicky.rb, line 27
def dataUpdate
    cleanupIfDataOverThreshold()
    writeBlandData() unless isGotToday()
end
getData(format=nil) click to toggle source
# File lib/syachicky.rb, line 45
def getData(format=nil)
    return @lastDayData
end
printData(format=nil) click to toggle source

foramt => '社名:#{syachiku}, 終値:#{syachiku}'

# File lib/syachicky.rb, line 33
def printData(format=nil)
    return unless @lastDayData
    
    if format
            syachiku = @lastDayData
            puts eval('"' + format + '"')
    else
        pp @lastDayData
    end
    
end

Private Instance Methods

cleanupIfDataOverThreshold() click to toggle source
# File lib/syachicky.rb, line 61
def cleanupIfDataOverThreshold
    return unless File.exist?("#{@dataDir}/#{@bland}")
    
    open("|wc -l < #{@dataDir}/#{@bland}"){|file|
            lines = file.gets().to_i
            File.unlink("#{@dataDir}/#{@bland}") if lines > @maxCacheDays
    }
end
isGotToday() click to toggle source
# File lib/syachicky.rb, line 50
def isGotToday
    return false unless File.exist?("#{@dataDir}/#{@bland}")
    
    open("|tail -n 1 < #{@dataDir}/#{@bland}"){|file|
            data = file.gets()
            @lastDayData = JSON.parse(data) if data
            result = @lastDayData["date"] if @lastDayData
            return (@dayString == result)
    }
end
writeBlandData() click to toggle source
# File lib/syachicky.rb, line 70
def writeBlandData
    info = SorryYahooFinance::GET(@bland, @day).values.stringify_keys!
    info["date"] = @dayString
    @lastDayData = info
    
    Dir.mkdir("#{@dataDir}") unless Dir.exist?("#{@dataDir}")
    File.open("#{@dataDir}/#{@bland}", "a"){|file|
            file.puts(@lastDayData.to_json)
    }
end