Skip to content

Commit

Permalink
Merge pull request #13 from bilaalrashid/bilaal/no-widths-sizing-method
Browse files Browse the repository at this point in the history
Implement NO_WIDTH image sizing type
  • Loading branch information
bilaalrashid authored Jun 9, 2024
2 parents 00d4740 + 9c7c7db commit 96f3fd3
Show file tree
Hide file tree
Showing 9 changed files with 1,709 additions and 30 deletions.
16 changes: 10 additions & 6 deletions Sources/BbcNews/Models/Story/Image/FDImage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,21 @@ public struct FDImage: Codable, Equatable, Hashable {
/// - Returns: A URL that returns a version of the image, or `nil` if no image URLs can be found.
public func largestImageUrl(upTo maxWidth: Double) -> URL? {
if self.source.sizingMethod.type == .specificWidths {
let allowedWidths = self.source.sizingMethod.widths.filter { Double($0) <= maxWidth }
guard let widths = self.source.sizingMethod.widths else {
return nil
}

let allowedWidths = widths.filter { Double($0) <= maxWidth }

if let maxSize = allowedWidths.last {
let url = self.source.url.replacingOccurrences(of: self.source.sizingMethod.widthToken, with: String(maxSize))
return URL(string: url)
guard let maxSize = allowedWidths.last, let widthToken = self.source.sizingMethod.widthToken else {
return nil
}

return nil
let url = self.source.url.replacingOccurrences(of: widthToken, with: String(maxSize))
return URL(string: url)
}

// If we don't recognise the sizing method, assume the URL is not templated.
// Non-templated URLs.
return URL(string: self.source.url)
}
}
10 changes: 7 additions & 3 deletions Sources/BbcNews/Models/Story/Image/FDImageSizingMethod.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,21 @@ public struct FDImageSizingMethod: Codable, Equatable, Hashable {
public var type: FDImageSizingMethodType

/// The token in the URL where the chosen width should be defined.
public var widthToken: String
///
/// This is used when `type` is `.specificWidths`.
public var widthToken: String?

/// The fixed options of widths that can we used to substitute in place of the width token.
public var widths: [Int]
///
/// This is used when `type` is `.specificWidths`.
public var widths: [Int]?

/// Creates a new definition of how to fetch the correct size of the image.
/// - Parameters:
/// - type: The type of sizing method used.
/// - widthToken: The token in the URL where the chosen width should be defined.
/// - widths: The fixed options of widths that can we used to substitute in place of the width token.
public init(type: FDImageSizingMethodType, widthToken: String, widths: [Int]) {
public init(type: FDImageSizingMethodType, widthToken: String? = nil, widths: [Int]? = nil) {
self.type = type
self.widthToken = widthToken
self.widths = widths
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,8 @@ public enum FDImageSizingMethodType: String, Codable, Equatable, Hashable, Unkno
/// A sizing method where a token in a URL is substituted with a selection from a pre-defined list of sizes.
case specificWidths = "SPECIFIC_WIDTHS"

/// A sizing method where only a single width is supported and is hardcoded into a URL.
case noWidth = "NO_WIDTH"

case unknown
}
25 changes: 20 additions & 5 deletions Tests/BbcNewsTests/FDImageTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import XCTest
@testable import BbcNews

final class FDImageTests: XCTestCase {
func testImageUrls() throws {
let image = self.getFDImage(sizingMethod: .specificWidths)
func testSpecificWidthUrls() throws {
let image = self.getFDImage(url: "https://example.invalid/img/{width}/example.jpg", sizingMethod: .specificWidths)

XCTAssertEqual(
image.largestImageUrl,
Expand All @@ -29,8 +29,23 @@ final class FDImageTests: XCTestCase {
)
}

func testNoWidthUrl() throws {
let image = self.getFDImage(url: "https://example.invalid/img/example.jpg", sizingMethod: .noWidth)

XCTAssertEqual(
image.largestImageUrl,
URL(string: "https://example.invalid/img/example.jpg"),
"Fails to return the fixed URL when requesting the largest width"
)
XCTAssertEqual(
image.largestImageUrl(upTo: 250),
URL(string: "https://example.invalid/img/example.jpg"),
"Fails to return the fixed URL when requesting a custom width"
)
}

func testInvalidSizingMethods() throws {
let image = self.getFDImage(sizingMethod: .unknown)
let image = self.getFDImage(url: "https://example.invalid/img/{width}/example.jpg", sizingMethod: .unknown)

XCTAssertEqual(
image.largestImageUrl,
Expand All @@ -44,10 +59,10 @@ final class FDImageTests: XCTestCase {
)
}

private func getFDImage(sizingMethod: FDImageSizingMethodType) -> FDImage {
private func getFDImage(url: String, sizingMethod: FDImageSizingMethodType) -> FDImage {
return FDImage(
source: FDImageSource(
url: "https://example.invalid/img/{width}/example.jpg",
url: url,
sizingMethod: FDImageSizingMethod(
type: sizingMethod,
widthToken: "{width}",
Expand Down
Loading

0 comments on commit 96f3fd3

Please sign in to comment.