Class: JIJI::Converter

Inherits:
Object
  • Object
show all
Defined in:
lib/jiji/util/rate_data_importer.rb

Constant Summary collapse

SPREAD =
{
  :AUDJPY => 0.02, :GBPUSD => 0.0003, :NZDJPY => 0.024,
  :AUDUSD => 0.0003, :CADJPY => 0.03, :EURCHF => 0.0003,
  :USDJPY => 0.008, :CHFJPY => 0.03,:GBPCHF => 0.0004, 
  :EURJPY => 0.014,:ZARJPY => 0.04,:USDCHF => 0.0004,
  :GBPJPY => 0.024,:EURUSD => 0.00016
}

Instance Method Summary collapse

Constructor Details

#initializeConverter

Returns a new instance of Converter.



109
110
111
# File 'lib/jiji/util/rate_data_importer.rb', line 109

def initialize( )
  @registry = JIJI::Registry.new( "./" )
end

Instance Method Details

#convert(dir, to) ⇒ Object

展開したCSVデータをjijiの形式にフォーマットする。

csv_dir

csvデータ置き場

to

CSVデータ



116
117
118
119
120
121
122
123
124
# File 'lib/jiji/util/rate_data_importer.rb', line 116

def convert( dir, to )
  FileUtils.mkdir_p to
  dao = @registry.rate_dao
  dao.instance_variable_set(:@data_dir, to)
  # CSVを読みつつデータを作成
  each_rate(dir) {|rates|
    dao.next_rates( rates )
  }
end

#each_rate(dir, &block) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/jiji/util/rate_data_importer.rb', line 125

def each_rate(dir, &block)
  yyyymm = Dir.entries( dir ).reject{|d| !(d=~ /\d{6}/)  }.sort
  yyyymm.each {|ym|
    1.upto(31).each {|d|
      readers = {}
      begin 
        JIJI::Download::Session::C_MAP.each {|p|
          file = "#{dir}/#{ym}/#{p[0]}_#{ym}#{sprintf("%02d", d)}.csv"
          next unless File.exist? file
          readers[p[0]] = PushBackReader.new(CSV.open( file, 'r' ))
          readers[p[0]].shift # 最初のデータはヘッダーなので除外
        }
        next if readers.empty?
        read( readers, &block )
      ensure
        readers.each {|p| 
          begin
            p[0].close
          rescue; end
        }
      end
    }
  }
end

#read(readers) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/jiji/util/rate_data_importer.rb', line 149

def read( readers )
  while( true )
    first = nil
    readers.each {|p|
      line = p[1].shift
      p[1].unshift line
      next if !line || line.length < 5
      next unless line[0] =~ /(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/
      first = !first || first.to_i > line[0].to_i ? line[0] : first
    }
    return unless first
    buff = readers.inject([]) {|r,p|
      line = p[1].shift
      next r if !line || line.length < 5
      if ( line[0] != first)
        p[1].unshift line
        next r
      end
      0.upto(3) {|i|
        time = Time.local( $1.to_i, $2.to_i, $3.to_i, $4.to_i, $5.to_i, i*10 )
        map = r[i] || r[i] = {}
        bid = line[i+1].to_f
        map[p[0].to_sym] = Rate.new( bid, bid+SPREAD[p[0].to_sym], 0, 0, time )
      }
      r
    }
    return if !buff || buff.empty?
    buff.each {|e|
      yield Rates.new( {}, e, e[:USDJPY].time )
    }
  end
end