InterestingNumbersLibrary is a Swift Package that fetches interesting number facts from the Numbers API.
The main class, NumbersManager
, is responsible for fetching facts about numbers. The fetched facts can either be a single fact about a number or multiple facts about different numbers. These facts are returned as a dictionary where the keys are the numbers and the values are the facts.
Import the package to your project:
import InterestingNumbersLibrary
Instantiate the NumbersManager
class:
let numbersManager = NumbersManager()
Set the delegate to receive the fetched facts or errors:
numbersManager.delegate = self
Your class needs to conform to the NumbersManagerDelegate
protocol:
extension YourClass: NumbersManagerDelegate {
func didUpdateNumberFacts(_ manager: NumbersManager, facts: [String: String]) {
// Handle the fetched facts
}
func didFailWithError(error: Error) {
// Handle the error
}
}
To fetch a single fact about a number:
numbersManager.isParseOneFact = true
numbersManager.userInputNumber = "10"
numbersManager.fetchFacts(numbers: "10")
To fetch multiple facts:
numbersManager.isParseOneFact = false
numbersManager.fetchFacts(numbers: "1..3")
The library also includes a dependency injection feature for testing purposes. You can initialize the NumbersManager
with a custom URLSession
:
let mockSession = URLSession(configuration: .ephemeral)
let numbersManager = NumbersManager(session: mockSession)
Contributions are welcome. Please open a pull request with your changes.
This project is open-source and available under the MIT license.
Please note that the Numbers API might have rate limits. Check the Numbers API documentation for more information.