-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #128 from Automattic/etoledom/split-gravatarurl
Split GravatarURL into AvatarURL and ProfileURL
- Loading branch information
Showing
10 changed files
with
322 additions
and
242 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import Foundation | ||
|
||
public struct AvatarURL { | ||
public let canonicalUrl: URL | ||
public let hash: String | ||
public let url: URL | ||
|
||
let options: AvatarQueryOptions | ||
let components: URLComponents | ||
|
||
public init?(url: URL, options: AvatarQueryOptions = AvatarQueryOptions()) { | ||
guard | ||
Self.isAvatarUrl(url), | ||
let components = URLComponents(url: url, resolvingAgainstBaseURL: false)?.sanitizingComponents(), | ||
let sanitizedURL = components.url, | ||
let url = sanitizedURL.addQueryItems(from: options) | ||
else { | ||
return nil | ||
} | ||
|
||
self.canonicalUrl = sanitizedURL | ||
self.components = components | ||
self.hash = sanitizedURL.lastPathComponent | ||
self.options = options | ||
self.url = url | ||
} | ||
|
||
public init?(email: String, options: AvatarQueryOptions = AvatarQueryOptions()) { | ||
self.init(hash: email.sanitized.sha256(), options: options) | ||
} | ||
|
||
public init?(hash: String, options: AvatarQueryOptions = AvatarQueryOptions()) { | ||
guard let url = URL(string: .baseURL + hash) else { return nil } | ||
self.init(url: url, options: options) | ||
} | ||
|
||
public static func isAvatarUrl(_ url: URL) -> Bool { | ||
guard | ||
let components = URLComponents(url: url, resolvingAgainstBaseURL: false), | ||
let host = components.host | ||
else { | ||
return false | ||
} | ||
|
||
return (host.hasSuffix(".gravatar.com") || host == "gravatar.com") | ||
&& components.path.hasPrefix("/avatar/") | ||
} | ||
|
||
public func replacing(options: AvatarQueryOptions) -> AvatarURL? { | ||
AvatarURL(hash: hash, options: options) | ||
} | ||
} | ||
|
||
extension AvatarURL: Equatable { | ||
public static func == (lhs: AvatarURL, rhs: AvatarURL) -> Bool { | ||
lhs.url.absoluteString == rhs.url.absoluteString | ||
} | ||
} | ||
|
||
extension String { | ||
fileprivate static let scheme = "https" | ||
fileprivate static let baseURL = "https://gravatar.com/avatar/" | ||
} | ||
|
||
extension URL { | ||
fileprivate func addQueryItems(from options: AvatarQueryOptions) -> URL? { | ||
guard var components = URLComponents(url: self, resolvingAgainstBaseURL: false) else { | ||
return nil | ||
} | ||
components.queryItems = options.queryItems | ||
|
||
if components.queryItems?.isEmpty == true { | ||
components.queryItems = nil | ||
} | ||
|
||
return components.url | ||
} | ||
} | ||
|
||
extension URLComponents { | ||
fileprivate func sanitizingComponents() -> URLComponents { | ||
var copy = self | ||
copy.scheme = .scheme | ||
copy.query = nil | ||
return copy | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import Foundation | ||
|
||
extension URL { | ||
@available(swift, deprecated: 16.0, message: "Use URL.appending(path:) instead") | ||
func appending(pathComponent path: String) -> URL { | ||
if #available(iOS 16.0, *) { | ||
self.appending(path: path) | ||
} else { | ||
self.appendingPathComponent(path) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import Foundation | ||
|
||
public struct ProfileURL { | ||
public let url: URL | ||
public let hash: String | ||
public var avatarURL: AvatarURL? { | ||
AvatarURL(hash: hash) | ||
} | ||
|
||
static let baseURL: URL? = { | ||
guard | ||
let baseURL = URL(string: .baseURL), | ||
let components = URLComponents(url: baseURL, resolvingAgainstBaseURL: false) | ||
else { | ||
return nil | ||
} | ||
return components.url | ||
}() | ||
|
||
public init?(email: String) { | ||
let hash = email.sanitized.sha256() | ||
self.init(hash: hash) | ||
} | ||
|
||
public init?(hash: String) { | ||
guard let url = Self.baseURL?.appending(pathComponent: hash) else { | ||
return nil | ||
} | ||
self.url = url | ||
self.hash = hash | ||
} | ||
} | ||
|
||
extension String { | ||
fileprivate static let baseURL = "https://gravatar.com/" | ||
} |
Oops, something went wrong.