comparison Simoleon/Jobs/CurrenciesController.swift @ 154:8afba86ab8dd

Refactor code
author Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
date Wed, 25 Aug 2021 10:43:12 +0100
parents
children
comparison
equal deleted inserted replaced
153:2590ee472aa9 154:8afba86ab8dd
1 //
2 // GetCompatibleCurrencies.swift
3 // Simoleon
4 //
5 // Created by Dennis Concepción Martín on 24/8/21.
6 //
7
8 import Foundation
9
10 class CurrenciesController {
11 let fileController = FileController()
12
13 func get(currenciesCompatibleWith currencySymbol: String?, currencies: Bool?) -> [String] {
14 // If currencies not false -> return all currencies
15 guard currencies == false else { return allCurrencies() }
16
17 // This block won't be executed if the previous check fails
18 return compatibleCurrencies(with: currencySymbol!)
19 }
20
21 /*
22 * Input all currencies supported by vendor
23 * Return individual currency symbols without duplicates
24 */
25 private func allCurrencies() -> [String] {
26 let currencyPairsSupported: [String] = try! fileController.read(json: "CurrencyPairsSupported.json")
27
28 var currencies = Set<String>()
29 for currencyPairSupported in currencyPairsSupported {
30 let currency = currencyPairSupported.components(separatedBy: "/")[0]
31 currencies.insert(currency)
32 }
33
34 return Array(currencies)
35 }
36
37 /*
38 * Given the first symbol of the currency pair
39 * Return all compatible symbols
40 */
41 private func compatibleCurrencies(with currencySymbol: String) -> [String] {
42 let currencyPairsSupported: [String] = try! fileController.read(json: "CurrencyPairsSupported.json")
43
44 var currencies = [String]()
45 for currencyPairSupported in currencyPairsSupported {
46 if currencyPairSupported.hasPrefix(currencySymbol) {
47 let compatibleCurrency = currencyPairSupported.components(separatedBy: "/")[1]
48 currencies.append(compatibleCurrency)
49 }
50 }
51
52 return currencies
53 }
54 }