1 require 'security_identifiers'
4 desc "Run quote update cycle. Useful for scheduling, ie with cron."
5 task :update_quotes => :environment do
6 require 'fetch_quote_job'
8 Security.where(:fetch_quote=>true).each do |s|
9 next if s.has_trading_ceased? or s.is_expired?
10 Smr::FetchQuoteJob.perform_later(s)
14 desc "Create Securities from ISIN symbols in :file."
15 task :import_symbols, [:file] => :environment do |t, args|
16 symbols, item, found, imported, known, expired, invalid, no_metadata = 0, 0, 0, 0, 0, 0, 0, 0
20 puts 'Usage: rake smr:import_symbols[/path/to/file]'
24 matches = File.open(args[:file], 'r').read.scan(/[A-Z]{2}[A-Z0-9]{9}\d{1}?/)
25 symbols = matches.count
28 puts '%i/%i => %s' % [item, symbols, m]
31 unless SecurityIdentifiers::ISIN.new(m).valid?
32 puts ' ISIN code invalid, skipping.'; invalid+=1
35 # use original string until https://github.com/invisiblelines/security_identifiers/issues/5 is solved
38 puts "--- sleep #{sleep rand(300) } --- " if item % stop_each == 0
40 if Security.where(:symbol=>isin).count == 0
41 s = Security.new :symbol=>isin
43 puts ' found %s' % s; found+=1
45 puts ' is expired, dropping.'; expired+=1
48 s.description = 'created by smr:import_symbols'
53 puts ' no metadata found, dropping.'; no_metadata+=1
57 else puts ' is known, skipping.'; known+=1 end
58 #Security.where(:symbol=>isin).destroy_all
62 puts 'stats: symbols=%i invalid_symbols=%i found=%i not_found=%i skipped_known=%i skipped_expired=%i imported=%i' % [
63 symbols, invalid, found, no_metadata, known, expired, imported
67 desc 'Create fixtures for testing from data in an existing database. Defaults to development database. Set RAILS_ENV to override.'
68 task :extract_fixtures => :environment do
69 sql = 'SELECT * FROM `%s`'
70 skip_tables = ['schema_info']
71 ActiveRecord::Base.establish_connection
72 (ActiveRecord::Base.connection.tables - skip_tables).each do |table_name|
74 File.open("#{Rails.root}/test/fixtures/#{table_name}.yml", 'w') do |file|
75 data = ActiveRecord::Base.connection.select_all(sql % table_name)
76 file.write data.inject({}) { |hash, record|
77 hash["#{table_name}_#{i.succ!}"] = record