Sha256: 8b37248355b2a0e326e583265609c1644b01364902fac76801f0448303bfed30

Contents?: true

Size: 1.46 KB

Versions: 105

Compression:

Stored size: 1.46 KB

Contents

def puts! a, b=''
  puts "+++ +++ #{b}"
  puts a.inspect
end

namespace :ish_manager do

  desc "every user needs a user_profile"
  task :generate_user_profiles => :environment do
    User.all.map do |u|
      unless u.profile
        p = ::IshModels::UserProfile.new :email => u.email, :user => u, :role_name => :guy
        u.profile = p
        u.save && p.save && print('.')
      end
    end
    puts 'OK'
  end

  desc 'watch the stocks, and trigger actions'
  task :watch_stocks => :environment do
    while true
      stocks = Ish::StockWatch.where( :notification_type => :EMAIL )
      puts! stocks.map(&:ticker), "Watching these stocks on #{Time.now}"
      stocks.each do |stock|
        begin
          Timeout::timeout( 10 ) do
            r = HTTParty.get "https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol=#{stock.ticker}&interval=1min&apikey=X1C5GGH5MZSXMF3O"
          end
        rescue Exception => e
          puts! e, 'e in :watch_stocks'
        end
        r = JSON.parse( r.body )['Time Series (1min)']
        r = r[r.keys.first]['4. close'].to_f
        if stock.direction == :ABOVE &&  r >= stock.price ||
           stock.direction == :BELOW && r <= stock.price
          IshManager::ApplicationMailer.stock_alert( stock ).deliver

=begin
          # actions
          stock.stock_actions.where( :is_active => true ).each do |action|
            # @TODO: actions
          end
=end
          
        end
      end
      sleep 60
    end
  end

end

Version data entries

105 entries across 105 versions & 1 rubygems

Version Path
ish_manager-0.1.8.145 lib/tasks/ish_manager_tasks.rake
ish_manager-0.1.8.144 lib/tasks/ish_manager_tasks.rake
ish_manager-0.1.8.143 lib/tasks/ish_manager_tasks.rake
ish_manager-0.1.8.142 lib/tasks/ish_manager_tasks.rake
ish_manager-0.1.8.141 lib/tasks/ish_manager_tasks.rake
ish_manager-0.1.8.140 lib/tasks/ish_manager_tasks.rake
ish_manager-0.1.8.139 lib/tasks/ish_manager_tasks.rake
ish_manager-0.1.8.138 lib/tasks/ish_manager_tasks.rake
ish_manager-0.1.8.137 lib/tasks/ish_manager_tasks.rake
ish_manager-0.1.8.136 lib/tasks/ish_manager_tasks.rake
ish_manager-0.1.8.135 lib/tasks/ish_manager_tasks.rake
ish_manager-0.1.8.134 lib/tasks/ish_manager_tasks.rake
ish_manager-0.1.8.133 lib/tasks/ish_manager_tasks.rake
ish_manager-0.1.8.132 lib/tasks/ish_manager_tasks.rake
ish_manager-0.1.8.131 lib/tasks/ish_manager_tasks.rake
ish_manager-0.1.8.129 lib/tasks/ish_manager_tasks.rake
ish_manager-0.1.8.128 lib/tasks/ish_manager_tasks.rake
ish_manager-0.1.8.127 lib/tasks/ish_manager_tasks.rake
ish_manager-0.1.8.126 lib/tasks/ish_manager_tasks.rake
ish_manager-0.1.8.125 lib/tasks/ish_manager_tasks.rake