Class: Iro::Api::StocksController

Inherits:
Iro::ApiController
  • Object
show all
Defined in:
app/controllers/iro/api/stocks_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject



5
6
7
8
9
10
11
12
13
14
15
# File 'app/controllers/iro/api/stocks_controller.rb', line 5

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



17
18
19
20
# File 'app/controllers/iro/api/stocks_controller.rb', line 17

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

#indexObject



22
23
24
25
26
27
28
29
30
# File 'app/controllers/iro/api/stocks_controller.rb', line 22

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

  respond_to do |format|
    format.html
    format.json
  end
end

#max_painObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/controllers/iro/api/stocks_controller.rb', line 32

def max_pain
  authorize! :max_pain, @stock
  Iro::Iro.schwab_sync

  hash = Tda::Option.get_chains({ ticker: @stock.ticker, force: false })
  # hash = JSON.parse File.read './trash.json'
  @max_pain = Iro::Option.max_pain hash

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

#newObject



48
49
50
51
# File 'app/controllers/iro/api/stocks_controller.rb', line 48

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

#showObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'app/controllers/iro/api/stocks_controller.rb', line 54

def show
  authorize! :show, @stock
  end_on = Time.now.to_date.in_time_zone('UTC')

  begin_on = ( Time.now - 1.year ).to_date.in_time_zone('UTC')
  begin_on = params[:begin_on].to_date.in_time_zone('UTC') if params[:begin_on]
  end_on   = params[:end_on].to_date.in_time_zone('UTC')   if params[:end_on]

  case params[:period]
  when '1-mo'
    begin_on = ( Time.now - 30.days ).to_date.in_time_zone('UTC')
    # end_on   = Time.now.to_date.in_time_zone('UTC')
  when '3-mo'
    begin_on = ( Time.now - 90.days ).to_date.in_time_zone('UTC')
    # end_on   = Time.now.to_date.in_time_zone('UTC')
  when '1-yr'
    begin_on = ( Time.now - 1.year ).to_date.in_time_zone('UTC')
    # end_on   = Time.now.to_date.in_time_zone('UTC')
  when '5-yr'
    begin_on = ( Time.now - 5.years ).to_date.in_time_zone('UTC')
  end

  @datapoints = Iro::Datapoint.where({
    :quote_at.gte => begin_on,
    :quote_at.lte => end_on,
    symbol:          params[:ticker],
  }).order_by({ quote_at: :asc })
end

#updateObject



83
84
85
86
87
88
89
90
91
92
# File 'app/controllers/iro/api/stocks_controller.rb', line 83

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