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

Instance Method Details

#createObject



8
9
10
11
12
13
14
15
16
17
18
# 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
    flash_alert @stock
  end
  redirect_to action: :index
end

#destroyObject



20
21
22
23
# File 'app/controllers/iro/stocks_controller.rb', line 20

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

#editObject



25
26
27
# File 'app/controllers/iro/stocks_controller.rb', line 25

def edit
  authorize! :edit, @stock
end

#indexObject



29
30
31
32
33
34
35
36
37
38
39
# File 'app/controllers/iro/stocks_controller.rb', line 29

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

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

#newObject



43
44
45
46
# File 'app/controllers/iro/stocks_controller.rb', line 43

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

#showObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'app/controllers/iro/stocks_controller.rb', line 59

def show
  authorize! :show, @stock

  @priceitems = ::Iro::Priceitem.where({
    ticker: @stock.ticker,
  })
  @datapoints = Iro::Datapoint.where({
    symbol: @stock.ticker,
  }).order_by({ date: :desc }).limit(100)

  filename = "./data/schwab/#{Time.now.to_date.to_s}-#{@stock.ticker}-chains.json"
  if File.exists? filename
    hash = JSON.parse File.read filename
  else
    hash = Tda::Option.get_chains({ ticker: @stock.ticker })
    File.write filename, hash.to_json
  end
  @max_pain = Iro::Option.max_pain hash
  @max_pain_summary = {}
  @max_pain.each do |date, types|
    all = types['all']
    @max_pain_summary[date] = all.keys[0]
    all.each do |strike, amount|
      if amount < all[@max_pain_summary[date]]
        @max_pain_summary[date] = strike
      end
    end
  end

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

#syncObject



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

def sync
  authorize! :refresh, Iro::Stock
  tickers = Iro::Stock.all.map { |s| s.ticker }.join(',')
  outs = Tda::Stock.get_quotes tickers
  outs.map do |out|
    Iro::Stock.where( ticker: out[:symbol] ).update( last: out[:last] )
  end
  flash_notice 'refreshed stocks'
  redirect_to request.referrer
end

#updateObject



96
97
98
99
100
101
102
103
104
105
# File 'app/controllers/iro/stocks_controller.rb', line 96

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