tyrauber / stock_quote

A ruby gem that retrieves stock quotes from IEX
MIT License
210 stars 57 forks source link

quote throws JSON::ParserError for certain symbols #47

Closed clay-reed-a closed 6 years ago

clay-reed-a commented 6 years ago

It seems like StockQuote::Stock.quote can't parse the JSON from a number of the response it's getting back. I tried this on Linux and then again on a Mac, a couple different Ruby versions too. Currently on 2.1.4, but tried latest stable as well.

For examples of symbols that seem to provoke this issue, please see:

require 'stock_quote' 

examples = [
  'AON', 'JCI', 'PH', 'A', 'MYL', 'INFO', 'SNPS', 'ALLY', 'MSCI',
  'FANG', 'XL', 'FOX', 'BR', 'BG', 'NI', 'ST', 'Y', 'KEYS',
  'AYI', 'MIDD', 'BLUE', 'SCI', 'SERV', 'COMM', 'SNAP', 'RS',
  'SON', 'TECH', 'Z'
]

examples.each do |symbol|
  begin
    quote = StockQuote::Stock.quote symbol 
  rescue JSON::ParserError => msg 
    puts "Either Google's API is dispensing invalid JSON for #{symbol} or..."
    puts "Something is wrong with stock_quote..."
    puts msg 
    next  
  end

  puts "Or maybe something is wrong with me."
end 

Can't quite figure out what exactly it's objecting to in the response, the output is rather large but also incomplete.

JSON::ParserError: 757: unexpected token at 'tart" : "","num" : "","num_company_results" : "571","num_mf_results" : "1066","num_all_results" : "","original_query" : "Z","query_for_display" : "Z","results_type" : "DEFAULT","searchresults" :[{"title" : "La-Z-Boy Incorporated","id" : "21057","is_active" : "","ticker" : "LZB","exchange" : "NYSE","is_supported_exchange" : "","local_currency_symbol" : "$","columns" : []}, {"title" : "Royal Bank of Canada","id" : "427488555729150","is_active" : "","ticker" : "RY-Z","exchange" : "TSE","is_supported_exchange" : "","local_currency_symbol" : "CA$","columns" : []}, {"title" : "CA, Inc.","id" : "8500","is_active" : "","ticker" : "CA","exchange" : "NASDAQ","is_supported_exchange" : "","local_currency_symbol" : "$","columns" : []}, {"title" : "Bank of America Corporation BAC Capital Trust VIII 6% Capital Securities","id" : "5096791","is_active" : "","ticker" : "BAC-Z.CL","exchange" : "NYSE","is_supported_exchange" : "","local_currency_symbol" : "$","columns" : []}, {"title" : "J P Morgan Chase \u0026 Co Cap Secs Var Ser Z 12\u002F31\u002F2048","id" : "10634449","is_active" : "","ticker" : "JPM-Z.CL","exchange" : "NYSE","is_supported_exchange" : "","local_currency_symbol" : "$","columns" : []}, {"title" : "HSBC USA, Inc. CL \u0027CL\u0027 PRFD \u0027Z.CL\u0027","id" : "9958998","is_active" : "","ticker" : "HUSI-Z.CL","exchange" : "NYSE","is_supported_exchange" : "","local_currency_symbol" : "$","columns" : []}, {"title" : "Regions Fing Tr III Tr Pfd Secs 06\u002F15\u002F2078","id" : "11523241","is_active" : "","ticker" : "RF-Z","exchange" : "NYSE","is_supported_exchange" : "","local_currency_symbol" : "$","columns" : []}, {"title" : "Z-Obee Holdings Ltd","id" : "14893800","is_active" : "","ticker" : "0948","exchange" : "HKG","is_supported_exchange" : "","local_currency_symbol" : "HK$","columns" : []}, {"title" : "SunTrust Banks, Inc. Suntrust Capital IX 7.875% Trust Preferred Securities","id" : "5007980","is_active" : "","ticker" : "STI-Z","exchange" : "NYSE","is_supported_exchange" : "","local_currency_symbol" : "$","columns" : []}, {"title" : "Apartment Investment and Management Co","id" : "5313417","is_active" : "","ticker" : "AIV-Z.CL","exchange" : "NYSE","is_supported_exchange" : "","local_currency_symbol" : "$","columns" : []}, {"title" : "Zinc One Resources Inc","id" : "716140","is_active" : "","ticker" : "Z","exchange" : "CVE","is_supported_exchange" : "","local_currency_symbol" : "CA$","columns" : []}, {"title" : "Autoliv, Inc. Corp Unit","id" : "8101780","is_active" : "","ticker" : "ALV-Z","exchange" : "NYSE","is_supported_exchange" : "","local_currency_symbol" : "$","columns" : []}, {"title" : "Johnson Controls, Inc. Corp Unit","id" : "8700113","is_active" : "","ticker" : "JCI-Z.CL","exchange" : "NYSE","is_supported_exchange" : "","local_currency_symbol" : "$","columns" : []}, {"title" : "Public Storage Publlic Storage, Inc. Depositary Share Series Z","id" : "13899203","is_active" : "","ticker" : "","exchange" : "","is_supported_exchange" : "","local_currency_symbol" : "$","columns" : []}, {"title" : "Ci:z Holdings Co Ltd","id" : "685229","is_active" : "","ticker" : "4924","exchange" : "TYO","is_supported_exchange" : "","local_currency_symbol" : "¥","columns" : []}, {"title" : "Z Gallerie, LLC","id" : "3616665","is_active" : "","ticker" : "","exchange" : "","is_supported_exchange" : "","local_currency_symbol" : "-","columns" : []}, {"title" : "Z Corporation","id" : "2554659","is_active" : "","ticker" : "","exchange" : "","is_supported_exchange" : "","local_currency_symbol" : "-","columns" : []}, {"title" : "Z Energy Ltd","id" : "439717741744784","is_active" : "","ticker" : "ZEL","exchange" : "NZE","is_supported_exchange" : "","local_currency_symbol" : "NZ$","columns" : []}, {"title" : "Capital Z Management, LLC","id" : "3906016","is_active" : "","ticker" : "","exchange" : "","is_supported_exchange" : "","local_currency_symbol" : "-","columns" : []}, {"title" : "AviChina Industry \u0026 Technology Co Ltd","id" : "688860","is_active" : "","ticker" : "2357","exchange" : "HKG","is_supported_exchange" : "","local_currency_symbol" : "HK$","columns" : []}],"mf_searchresults" :[{"title" : "Columbia Value and Restructuring Fund Class Z","id" : "43941755427292","is_active" : "","ticker" : "UMBIX","exchange" : "MUTF","is_supported_exchange" : "","local_currency_symbol" : "$","columns" : []}, {"title" : "Touchstone Sands Capital Select Growth Fund Class Z","id" : "7759470454357","is_active" : "","ticker" : "PTSGX","exchange" : "MUTF","is_supported_exchange" : "","local_currency_symbol" : "$","columns" : []}, {"title" : "Prudential Jennison Growth Fund Class Z","id" : "769291539680209","is_active" : "","ticker" : "PJFZX","exchange" : "MUTF","is_supported_exchange" : "","local_currency_symbol" : "$","columns" : []}, {"title" : "Franklin Mutual Global Discovery Fund Class Z","id" : "218019676354140","is_active" : "","ticker" : "MDISX","exchange" : "MUTF","is_supported_exchange" : "","local_currency_symbol" : "$","columns" : []}, {"title" : "Columbia Large Cap Growth Fund II Class Z","id" : "613166786496134","is_active" : "","ticker" : "NMYAX","exchange" : "MUTF","is_supported_exchange" : "","local_currency_symbol" : "$","columns" : []}, {"title" : "AMG TimesSquare Mid Cap Growth Fund Class Z","id" : "379819052250780","is_active" : "","ticker" : "TMDIX","exchange" : "MUTF","is_supported_exchange" : "","local_currency_symbol" : "$","columns" : []}, {"title" : "Franklin Mutual Shares Fund Class Z","id" : "932077600196270","is_active" : "","ticker" : "MUTHX","exchange" : "MUTF","is_supported_exchange" : "","local_currency_symbol" : "$","columns" : []}, {"title" : "Franklin Mutual Quest Fund Class Z","id" : "271587861591046","is_active" : "","ticker" : "MQIFX","exchange" : "MUTF","is_supported_exchange" : "","local_currency_symbol" : "$","columns" : []}, {"title" : "Prudential Jennison Natural Resources Fund Class Z","id" : "33348043363522","is_active" : "","ticker" : "PNRZX","exchange" : "MUTF","is_supported_exchange" : "","local_currency_symbol" : "$","columns" : []}, {"title" : "Prudential Jennison Mid-Cap Growth Fund, Inc Class Z","id" : "28046060491903","is_active" : "","ticker" : "PEGZX","exchange" : "MUTF","is_supported_exchange" : "","local_currency_symbol" : "$","columns" : []}, {"title" : "Franklin Mutual Beacon Fund Class Z","id" : "235663387518811","is_active" : "","ticker" : "BEGRX","exchange" : "MUTF","is_supported_exchange" : "","local_currency_symbol" : "$","columns" : []}, {"title" : "Target International Equity Portfolio Fund Class Z","id" : "1073966859120379","is_active" : "","ticker" : "TAIEX","exchange" : "MUTF","is_supported_exchange" : "","local_currency_symbol" : "$","columns" : []}, {"title" : "Prudential Jennison Small Company Fund Class Z","id" : "828277264034160","is_active" : "","ticker" : "PSCZX","exchange" : "MUTF","is_supported_exchange" : "","local_currency_symbol" : "$","columns" : []}, {"title" : "Prudential Jennison Health Sciences Fund Class Z","id" : "266075624098149","is_active" : "","ticker" : "PHSZX","exchange" : "MUTF","is_supported_exchange" : "","local_currency_symbol" : "$","columns" : []}, {"title" : "Franklin Mutual European Fund Class Z","id" : "465001737452351","is_active" : "","ticker" : "MEURX","exchange" : "MUTF","is_supported_exchange" : "","local_currency_symbol" : "$","columns" : []}, {"title" : "Prudential Short-Term Corporate Bond Fund Class Z","id" : "651889985205405","is_active" : "","ticker" : "PIFZX","exchange" : "MUTF","is_supported_exchange" : "","local_currency_symbol" : "$","columns" : []}, {"title" : "Prudential High-Yield Fund Class Z","id" : "490452772294254","is_active" : "","ticker" : "PHYZX","exchange" : "MUTF","is_supported_exchange" : "","local_currency_symbol" : "$","columns" : []}, {"title" : "Prudential Corporate Bond Fund Class Z","id" : "926627073221877","is_active" : "","ticker" : "TGMBX","exchange" : "MUTF","is_supported_exchange" : "","local_currency_symbol" : "$","columns" : []}, {"title" : "Prudential Total Return Bond Fund Class Z","id" : "475285720482124","is_active" : "","ticker" : "PDBZX","exchange" : "MUTF","is_supported_exchange" : "","local_currency_symbol" : "$","columns" : []}, {"title" : "Columbia Large Cap Growth Fund V Class Z","id" : "994437973422510","is_active" : "","ticker" : "NGIPX","exchange" : "MUTF","is_supported_exchange" : "","local_currency_symbol" : "$","columns" : []}]}'

All of them seem to start: 'tart" : "",.

tyrauber commented 6 years ago

@clay-reed-a Looks like Google doesn't recognize the symbol "Z", and instead of returning a normal formatted response is returning a response containing suggestions. I am going to update the gem to accommodate the additional response instead of crashing, but I wanted to let you know what the issue was.

clay-reed-a commented 6 years ago

@tyrauber I have found the issue and am submitting a pull request. It actually isn't about the symbol Z but about the leading "// " we see when we get more than one result back.

tyrauber commented 6 years ago

Whoops, already worked up a solution, 827d3b7f6897c2628a49105045040cc51af751ce. Want to let me know if that resolves your issue before I push our a 1.5.3?

clay-reed-a commented 6 years ago

Hey @tyrauber, Left a comment on why that might not be quite what you're looking for.

I'm looking more deeply into it and I don't think it's an unknown symbol but an ambiguous symbol. Take the response for "NI" as an example. That returns one of these searchresults lists, but if I take the exchange from the first "NI" in that searchresults list and go StockQuote.quote "NI:NYSE" then I get what I'm after.

tyrauber commented 6 years ago

Hmm, updating the documentation to suggest that format, "Symbol:Exchange" would suffice?

tyrauber commented 6 years ago

Closing this issue due to an API change to IEX (see #55). Not sure what the outcome of this change is for this specific issue is. Feel free to re-open is necessary in regards to version 2.0.0.