Created
November 17, 2020 17:55
-
-
Save bartjacobs/4dff823594836d60e4c98653fe08569c to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import UIKit | |
final class CloudinaryURLBuilder { | |
// MARK: - Properties | |
private let source: URL | |
// MARK: - | |
private var width: Int? | |
private var height: Int? | |
// MARK: - Initialization | |
init(source: URL) { | |
self.source = source | |
} | |
// MARK: - Public API | |
func width(_ width: Int) -> CloudinaryURLBuilder { | |
self.width = width | |
return self | |
} | |
func height(_ height: Int) -> CloudinaryURLBuilder { | |
self.height = height | |
return self | |
} | |
func build() -> URL { | |
var parameters: [String] = [] | |
var url = Environment.cloudinaryBaseUrl | |
if let width = width { | |
parameters.append("w_\(width)") | |
} | |
if let height = height { | |
parameters.append("h_\(height)") | |
} | |
// Define Format | |
parameters.append("f_png") | |
// Define Device Pixel Ratio | |
let dpr = String(format: "%1.1f", UIScreen.main.scale) | |
parameters.append("dpr_\(dpr)") | |
// Append Parameters | |
if !parameters.isEmpty { | |
let parametersAsString = parameters.joined(separator: ",") | |
url = url.appendingPathComponent(parametersAsString) | |
} | |
return url.appendingPathComponent(source.absoluteString) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is how you would use the
CloudinaryURLBuilder
class.