Skip to content

Instantly share code, notes, and snippets.

@junpluse
Created December 23, 2020 04:17
Show Gist options
  • Save junpluse/8b48dfe2a929d0de2dd852509e7f55c7 to your computer and use it in GitHub Desktop.
Save junpluse/8b48dfe2a929d0de2dd852509e7f55c7 to your computer and use it in GitHub Desktop.
import Foundation
/// https://source.unsplash.com
public struct Unsplash {
public struct Source: RawRepresentable {
public let rawValue: String
public init(rawValue: String) {
self.rawValue = rawValue
}
public static let random = Self(rawValue: "random")
public static func user(name: String) -> Self {
Self(rawValue: "user/\(name)")
}
public static func userLikes(name: String) -> Self {
Self(rawValue: "user/\(name)/likes")
}
public static func collection(id: Int) -> Self {
Self(rawValue: "collection/\(id)")
}
}
public static func url(
source: Source = .random,
width: Int,
height: Int,
keywords: [String]
) -> URL {
var components = URLComponents(
string: "https://source.unsplash.com"
)!
components.path = "/\(source.rawValue)/\(width)x\(height)"
if keywords.isEmpty == false {
components.query = keywords.joined(separator: ",")
}
return components.url!
}
public static func url(
source: Source = .random,
width: Int,
height: Int,
keywords: String...
) -> URL {
self.url(
source: source,
width: width,
height: height,
keywords: keywords
)
}
public static func url(
source: Source = .random,
size: Int,
keywords: String...
) -> URL {
self.url(
source: source,
width: size,
height: size,
keywords: keywords
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment