From 917c0fcfaebb0aac74d6508a8402a099bc5d46b6 Mon Sep 17 00:00:00 2001 From: Roberto Machorro <7190436+RobertoMachorro@users.noreply.github.com> Date: Sat, 22 Jul 2023 15:26:08 -0400 Subject: [PATCH] Completed How to Use in README. --- README.md | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 0fdb34f..8bac16b 100644 --- a/README.md +++ b/README.md @@ -6,10 +6,43 @@ # SwiftGravatar A Swift library for Gravatar Profile data access. -# Setup +# How to Use Add the SwiftGravatar package to your project or via Package.swift using the address: https://github.com/RobertoMachorro/SwiftGravatar image + +Fetching the profile from the server can be done many ways, the easiest is to use the convenience function *GravatarProfile.getProfile*: + +```swift +import SwiftGravatar + +if let profile = try await GravatarProfile.getProfile(using: "myemailaddress@example.com"), let entry = profile.entry.first { + print(entry.preferredUsername) + print(entry.profileUrl) + + print(entry.thumbnailUrl) + print(entry.photos?.first?.value) +} else { + print("We have a problem...") +} +``` + +Code can also be retrieved by other methods and then decoded: + +```swift +guard let url = URL(string: "https://en.gravatar.com/632d2f3abe7be4db174da5cb2760f0ae.json") else { + return XCTFail("Unable to parse Gravatar URL") +} +let (data, _) = try await URLSession.shared.data(from: url) +let profile = try JSONDecoder().decode(GravatarProfile.self, from: data) +``` + +An easy converter from e-mail to Gravatar URL can be accessed as follows: + +```swift +let myemailaddress = GravatarProfile.getProfileAddress(using: "myemailaddress@example.com") +// "https://en.gravatar.com/0bc83cb571cd1c50ba6f3e8a78ef1346.json" +```