Class: Iro::StocksController

Inherits:
ApplicationController show all
Defined in:
app/controllers/iro/stocks_controller.rb

Overview

Instance Method Summary collapse

Methods inherited from ApplicationController

#home, #schwab_sync, #schwab_sync_exec

Instance Method Details

#createObject



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'app/controllers/iro/stocks_controller.rb', line 8

def create
  @stock = Iro::Stock.new(stock_params)
  authorize! :create, @stock

  if @stock.save
    flash_notice @stock
  else
    @stock = Iro::Stock.unscoped.find_by ticker: stock_params[:ticker]
    flag = @stock.update( deleted_at: nil, options_price_increment: stock_params[:options_price_increment] )
    flash_alert @stock
  end
  redirect_to action: :index
end

#destroyObject



22
23
24
25
26
# File 'app/controllers/iro/stocks_controller.rb', line 22

def destroy
  authorize! :destroy, @stock
  @stock.destroy
  redirect_to stocks_url, notice: 'Stock was destroyed.'
end

#editObject



28
29
30
# File 'app/controllers/iro/stocks_controller.rb', line 28

def edit
  authorize! :edit, @stock
end

#get_historic_dataObject



32
33
34
35
36
37
# File 'app/controllers/iro/stocks_controller.rb', line 32

def get_historic_data
  authorize! :show, Iro::Stock
  @stock = Iro::Stock.find params[:id]
  @stock.get_historic_data( params[:date_from].to_date )
  redirect_to action: :show, id: params[:id]
end

#indexObject



46
47
48
49
50
51
52
53
54
55
56
# File 'app/controllers/iro/stocks_controller.rb', line 46

def index
  @all_stocks = Iro::Stock.all
  authorize! :index, Iro::Stock

  respond_to do |format|
    format.html
    format.json do
      render layout: false
    end
  end
end

#newObject



58
59
60
61
# File 'app/controllers/iro/stocks_controller.rb', line 58

def new
  @stock = Iro::Stock.new
  authorize! :new, @stock
end

#recompute_volatilityObject



39
40
41
42
43
44
# File 'app/controllers/iro/stocks_controller.rb', line 39

def recompute_volatility
  authorize! :show, Iro::Stock
  @stock = Iro::Stock.find params[:id]
  @stock.volatility( recompute: true )
  redirect_to action: :show, id: params[:id]
end

#showObject



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'app/controllers/iro/stocks_controller.rb', line 78

def show
  authorize! :show, @stock

  @priceitems = ::Iro::Priceitem.where({
    ticker: @stock.ticker,
  })
  @datapoints = Iro::Datapoint.where({
    symbol: @stock.ticker,
  }).order_by({ date: :desc })
  if @datapoints.length == 0
    @stock.get_historic_data
    @datapoints = Iro::Datapoint.where({
      symbol: @stock.ticker,
    }).order_by({ date: :desc })
  end
  @datapoints_h = {}
  @datapoints.each do |dp|
    @datapoints_h[dp.date.to_s] = dp.value
  end

  respond_to do |format|
    format.html
    format.json do
      render layout: false
    end
  end
end

#syncObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'app/controllers/iro/stocks_controller.rb', line 63

def sync
  authorize! :refresh, Iro::Stock
  tickers = Iro::Stock.all.map { |s| s.ticker }.join(',')
  outs = Tda::Stock.get_quotes tickers
  puts! outs, 'got all tickers'

  outs.map do |out|
    puts! out, 'a ticker'

    Iro::Stock.where( ticker: out[:symbol] ).update_all( last: out[:last] )
  end
  flash_notice 'refreshed stocks'
  redirect_to request.referrer
end

#updateObject



106
107
108
109
110
111
112
113
114
115
# File 'app/controllers/iro/stocks_controller.rb', line 106

def update
  @stock = Iro::Stock.find params[:id]
  authorize! :update, @stock
  if @stock.update(stock_params)
    flash_notice @stock
  else
    flash_alert @stock
  end
  redirect_to request.referrer
end