Skip to content

Commit

Permalink
Merge pull request #141 from Automattic/etoledom/is-gravatar-url
Browse files Browse the repository at this point in the history
Adding `isGravatarURL` extension.
  • Loading branch information
etoledom authored Mar 27, 2024
2 parents c34bf6a + d8c849d commit 4eaa1d0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
10 changes: 1 addition & 9 deletions Sources/Gravatar/AvatarURL.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,7 @@ public struct AvatarURL {
}

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/")
url.isGravatarURL && url.path.hasPrefix("/avatar/")
}

public func replacing(options: AvatarQueryOptions) -> AvatarURL? {
Expand Down
13 changes: 13 additions & 0 deletions Sources/Gravatar/Extensions/URL.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,17 @@ extension URL {
self.appendingPathComponent(path)
}
}

/// Whether this URL instance corresponds to a valid Gravatar URL.
var isGravatarURL: Bool {
guard
let components = URLComponents(url: self, resolvingAgainstBaseURL: false),
let host = components.host
else {
return false
}

return (host.hasSuffix(".gravatar.com") || host == "gravatar.com")
&& components.scheme == "https"
}
}

0 comments on commit 4eaa1d0

Please sign in to comment.