-
Notifications
You must be signed in to change notification settings - Fork 0
/
WealthsimpleAsset.swift
69 lines (62 loc) · 1.83 KB
/
WealthsimpleAsset.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
//
// Asset.swift
//
//
// Created by Steffen Kötte on 2020-07-12.
//
import Foundation
/// Errors which can happen when retrieving an Asset
public enum AssetError: Error {
/// When the received JSON does not have all expected values
case missingResultParamenter(json: [String: Any])
/// When the received JSON does have an unexpected value
case invalidResultParamenter(json: [String: Any])
}
/// Type of the asset
public enum AssetType: String {
/// Cash
case currency
/// Equity
case equity
/// Mutal Funds
case mutualFund = "mutual_fund"
/// Bonds
case bond
/// ETFs
case exchangeTradedFund = "exchange_traded_fund"
}
/// An asset, like a stock or a currency
public protocol Asset {
/// Symbol of the asset, e.g. currency or ticker symbol
var symbol: String { get }
/// Full name of the asset
var name: String { get }
/// Currency the asset is held in
var currency: String { get }
/// Type of the asset, e.g. currency or ETF
var type: AssetType { get }
}
struct WealthsimpleAsset: Asset {
let symbol: String
let name: String
let currency: String
let type: AssetType
let id: String
init(json: [String: Any]) throws {
guard let id = json["security_id"] as? String,
let symbol = json["symbol"] as? String,
let currency = json["currency"] as? String,
let name = json["name"] as? String,
let typeString = json["type"] as? String else {
throw AssetError.missingResultParamenter(json: json)
}
guard let type = AssetType(rawValue: typeString) else {
throw AssetError.invalidResultParamenter(json: json)
}
self.id = id
self.symbol = symbol
self.currency = currency
self.name = name
self.type = type
}
}