Class: JIJI::Download::Session

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

Constant Summary collapse

C_MAP =
{
  :USDJPY=>"01", :EURJPY=>"02", :GBPJPY=>"03",
  :AUDJPY=>"04", :NZDJPY=>"05", :CADJPY=>"06",
  :CHFJPY=>"07", :ZARJPY=>"08", :EURUSD=>"09",
  :GBPUSD=>"10", :AUDUSD=>"11",:EURCHF=>"12",
  :GBPCHF=>"13", :USDCHF=>"14"
}

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Session

Returns a new instance of Session.



51
52
53
# File 'lib/jiji/util/rate_data_importer.rb', line 51

def initialize( client )
  @client = client
end

Instance Method Details

#download(year, month, pair, to = "./") ⇒ Object

CSVデータをダウンロードする

yesr

month

pair

通貨ペア

to

ダウンロード先ディレクトリ



59
60
61
62
63
64
65
66
67
# File 'lib/jiji/util/rate_data_importer.rb', line 59

def download( year, month, pair, to="./" )
  FileUtils.makedirs(to)
  file = "#{to}/#{pair}_#{year}_#{month}.zip"
  result = @client.get("https://tb.click-sec.com/fx/historical/historicalDataDownload.do?" +
    "y=#{year}&m=#{sprintf("%02d", month)}&c=#{C_MAP[pair]}&n=#{pair}" )
  open( file, "w" ) {|w| w << result.body }
  extract( file, "#{to}" )
  FileUtils.rm(file)
end

#extract(zip, dest) ⇒ Object

zipファイルを展開する。

zip

zipファイル

dest

展開先ディレクトリ



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/jiji/util/rate_data_importer.rb', line 75

def extract( zip, dest )
  FileUtils.makedirs(dest)
  Zip::ZipFile.foreach(zip) {|entry|
    if entry.file?
      FileUtils.makedirs("#{dest}/#{File.dirname(entry.name)}")
      entry.get_input_stream {|io|
        open( "#{dest}/#{entry.name}", "w" ) {|w|
          while ( bytes = io.read(1024))
            w.write bytes
          end
        }
      }
    else
      FileUtils.makedirs("#{dest}/#{entry.name}")
    end
  }
end

#logoutObject

ログアウトする



69
70
71
# File 'lib/jiji/util/rate_data_importer.rb', line 69

def logout
  @client.get("https://sec-sso.click-sec.com/loginweb/sso-logout")
end