Skip to content

Instantly share code, notes, and snippets.

@nolochemical
Created March 19, 2020 02:14
Show Gist options
  • Save nolochemical/e54df6dcd676479f403dc8234a5f6fc4 to your computer and use it in GitHub Desktop.
Save nolochemical/e54df6dcd676479f403dc8234a5f6fc4 to your computer and use it in GitHub Desktop.
Alpha vantage api response decoder, for symbol search
import Foundation
struct MultiQuotes: Decodable{
var symbol: String
var name: String
var type: String
var region: String
var marketOpen: String
var marketClose: String
var timeZone: String
var currency: String
var matchScore: String
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: MultiQuotesDecodeKeys.self)
symbol = try container.decode(String.self, forKey: .symbol)
name = try container.decode(String.self, forKey: .name)
type = try container.decode(String.self, forKey: .type)
region = try container.decode(String.self, forKey: .region)
marketOpen = try container.decode(String.self, forKey: .marketOpen)
marketClose = try container.decode(String.self, forKey: .marketClose)
timeZone = try container.decode(String.self, forKey: .timeZone)
currency = try container.decode(String.self, forKey: .currency)
matchScore = try container.decode(String.self, forKey: .matchScore)
}
enum MultiQuotesDecodeKeys: String, CodingKey {
case symbol = "1. symbol"
case name = "2. name"
case type = "3. type"
case region = "4. region"
case marketOpen = "5. marketOpen"
case marketClose = "6. marketClose"
case timeZone = "7. timezone"
case currency = "8. currency"
case matchScore = "9. matchScore"
}
}
struct MultiQuote: Decodable {
// Search ticker
var multiQuote: [MultiQuotes]
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: DecodingKeysMulti.self)
multiQuote = try container.decode([MultiQuotes].self, forKey: .bestMatches)
}
enum DecodingKeysMulti: String, CodingKey {
case bestMatches = "bestMatches"
}
}
let bb = """
{
"bestMatches": [
{
"1. symbol": "RY",
"2. name": "Royal Bank of Canada",
"3. type": "Equity",
"4. region": "United States",
"5. marketOpen": "09:30",
"6. marketClose": "16:00",
"7. timezone": "UTC-05",
"8. currency": "USD",
"9. matchScore": "1.0000"
},
{
"1. symbol": "R",
"2. name": "Ryder System Inc.",
"3. type": "Equity",
"4. region": "United States",
"5. marketOpen": "09:30",
"6. marketClose": "16:00",
"7. timezone": "UTC-05",
"8. currency": "USD",
"9. matchScore": "0.8000"
},
{
"1. symbol": "RYAAY",
"2. name": "Ryanair Holdings plc",
"3. type": "Equity",
"4. region": "United States",
"5. marketOpen": "09:30",
"6. marketClose": "16:00",
"7. timezone": "UTC-05",
"8. currency": "USD",
"9. matchScore": "0.8000"
},
{
"1. symbol": "RHP",
"2. name": "Ryman Hospitality Properties Inc.",
"3. type": "Equity",
"4. region": "United States",
"5. marketOpen": "09:30",
"6. marketClose": "16:00",
"7. timezone": "UTC-05",
"8. currency": "USD",
"9. matchScore": "0.8000"
},
{
"1. symbol": "RYAM",
"2. name": "Rayonier Advanced Materials Inc.",
"3. type": "Equity",
"4. region": "United States",
"5. marketOpen": "09:30",
"6. marketClose": "16:00",
"7. timezone": "UTC-05",
"8. currency": "USD",
"9. matchScore": "0.8000"
},
{
"1. symbol": "RYN",
"2. name": "Rayonier Inc.",
"3. type": "Equity",
"4. region": "United States",
"5. marketOpen": "09:30",
"6. marketClose": "16:00",
"7. timezone": "UTC-05",
"8. currency": "USD",
"9. matchScore": "0.6667"
},
{
"1. symbol": "RYH",
"2. name": "Invesco S&P 500 Equal Weight Health Care ETF",
"3. type": "ETF",
"4. region": "United States",
"5. marketOpen": "09:30",
"6. marketClose": "16:00",
"7. timezone": "UTC-05",
"8. currency": "USD",
"9. matchScore": "0.6667"
},
{
"1. symbol": "RYI",
"2. name": "Ryerson Holding Corporation",
"3. type": "Equity",
"4. region": "United States",
"5. marketOpen": "09:30",
"6. marketClose": "16:00",
"7. timezone": "UTC-05",
"8. currency": "USD",
"9. matchScore": "0.5714"
},
{
"1. symbol": "RYT",
"2. name": "Invesco S&P 500 Equal Weight Technology ETF",
"3. type": "ETF",
"4. region": "United States",
"5. marketOpen": "09:30",
"6. marketClose": "16:00",
"7. timezone": "UTC-05",
"8. currency": "USD",
"9. matchScore": "0.4000"
}
]
}
""".data(using: .utf8)
let decoder = JSONDecoder()
do {
// MultiQuote or Alphaquote
let info = try decoder.decode(MultiQuote.self, from: bb!)
print(" \(info.multiQuote[0].symbol)")
} catch let e{
print("Decode result failed with error: \(e)")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment