0

I am trying to display the results of an external API when the user types in the ticker on the new stock form. What I am trying to accomplish is kind of stock search engine using the stock quote gem, so when the user types in the ticker it'll display some data points on that page and if the user hits submit it'll store that ticker and the price in the DB.

Here is my controller

def new
  @assignment = Assignment.new(portfolio_id: params[:portfolio_id])
  @stock = Assignment.new_from_lookup(params[:ticker])
end

And my model

def self.new_from_lookup(symbol)
  begin
    stock = StockQuote::Stock.quote(symbol)
    new(company_name: stock.company_name,
        symbol: stock.symbol,
        latest_price: stock.latest_price,
        latest_time: stock.latest_time)
  rescue Exception => e
    # Rescues all errors, an puts the exception object in `e
    return nil
  end
end

I hope this isn't too broad of a question but how would I display the data points on the form when the ticker is entered and store the data points if the submit button is clicked?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Joseph Parker
  • 35
  • 1
  • 5
  • 1
    This depends entirely on what the data returned by the API looks like. Also doing `rescue Exception => e` is a really bad idea since when you rescue Exception you rescue from everything, including subclasses such as SyntaxError, LoadError, and Interrupt. `rescue StandardError` is somewhat better but you really should only rescue exceptions that you actually know what to do with. https://stackoverflow.com/questions/10048173/why-is-it-bad-style-to-rescue-exception-e-in-ruby – max Jan 23 '19 at 23:35
  • It would be in this format when the request is returned { "symbol": "AAPL", "companyName": "Apple Inc.", "primaryExchange": "Nasdaq Global Select", "sector": "Technology" }. What I'm trying to do is display the company name and sector and other things on the form page when the ticker param is filled in – Joseph Parker Jan 24 '19 at 15:49

0 Answers0