Skip to content

Commit

Permalink
FEATURE: protocol for TMDbAPI (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
adamayoung authored Sep 25, 2020
1 parent 299b895 commit d8edc5b
Show file tree
Hide file tree
Showing 7 changed files with 471 additions and 14 deletions.
45 changes: 39 additions & 6 deletions Sources/TMDb/DTOs/PersonDTO.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ public struct PersonDTO: Identifiable, Decodable, Equatable, ProfileURLProviding
public let placeOfBirth: String?
public let profilePath: URL?
public let popularity: Float?
public let imdbId: String?
public let homepage: URL?
public let imdbID: String?

private let homepage: String?

public init(id: Int, name: String, alsoKnownAs: [String]? = nil, knownForDepartment: String? = nil,
biography: String? = nil, birthday: Date? = nil, deathday: Date? = nil, gender: GenderDTO? = nil,
placeOfBirth: String? = nil, profilePath: URL? = nil, popularity: Float? = nil, imdbId: String? = nil,
homepage: URL? = nil) {
placeOfBirth: String? = nil, profilePath: URL? = nil, popularity: Float? = nil, imdbID: String? = nil,
homepageURL: URL? = nil) {
self.id = id
self.name = name
self.alsoKnownAs = alsoKnownAs
Expand All @@ -31,8 +32,40 @@ public struct PersonDTO: Identifiable, Decodable, Equatable, ProfileURLProviding
self.placeOfBirth = placeOfBirth
self.profilePath = profilePath
self.popularity = popularity
self.imdbId = imdbId
self.homepage = homepage
self.imdbID = imdbID
self.homepage = homepageURL?.absoluteString
}

}

extension PersonDTO {

public var homepageURL: URL? {
guard let homepage = homepage else {
return nil
}

return URL(string: homepage)
}

}

extension PersonDTO {

enum CodingKeys: String, CodingKey {
case id
case name
case alsoKnownAs
case knownForDepartment
case biography
case birthday
case deathday
case gender
case placeOfBirth
case profilePath
case popularity
case imdbID = "imdbId"
case homepage
}

}
2 changes: 1 addition & 1 deletion Sources/TMDb/DTOs/StillURLProviding.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public extension StillURLProviding {
}

static var stillAspectRatio: Float {
100 / 150
500 / 281
}

}
411 changes: 411 additions & 0 deletions Sources/TMDb/MovieTVShowAPI.swift

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions Sources/TMDb/TMDbAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import Foundation
/// The Movie Database API.
///
/// - Note: [The Movie Database API Documentation](https://developers.themoviedb.org)
public final class TMDbAPI {
public final class TMDbAPI: MovieTVShowAPI {

/// A shared instance of the TMDbAPI.
/// A shared instance of the TMDb API.
public static let shared = TMDbAPI()

private let certificationService: CertificationService
Expand Down
17 changes: 15 additions & 2 deletions Tests/TMDbTests/DTOs/PersonDTOTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,19 @@ import XCTest

class PersonDTOTests: XCTestCase {

func testHomepageURLWhenNilReturnsNil() {
let somePerson = PersonDTO(id: 1, name: "Name 1", homepageURL: nil)

XCTAssertNil(somePerson.homepageURL)
}

func testHomepageURLWhenHasURLReturnsURL() {
let expectedResult = URL(string: "https://some.domain.com")!
let somePerson = PersonDTO(id: 1, name: "Name 1", homepageURL: expectedResult)

XCTAssertEqual(somePerson.homepageURL, expectedResult)
}

func testDecodeReturnsPerson() throws {
let data = json.data(using: .utf8)!
let result = try JSONDecoder.theMovieDatabase.decode(PersonDTO.self, from: data)
Expand Down Expand Up @@ -52,8 +65,8 @@ class PersonDTOTests: XCTestCase {
placeOfBirth: "Shawnee, Oklahoma, USA",
profilePath: URL(string: "/kU3B75TyRiCgE270EyZnHjfivoq.jpg"),
popularity: 10.647,
imdbId: "nm0000093",
homepage: nil
imdbID: "nm0000093",
homepageURL: nil
)
// swiftlint:enable line_length

Expand Down
4 changes: 2 additions & 2 deletions Tests/TMDbTests/DTOs/PersonPageableListDTOTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ class PersonPageableListDTOTests: XCTestCase {
placeOfBirth: "Shawnee, Oklahoma, USA",
profilePath: URL(string: "/kU3B75TyRiCgE270EyZnHjfivoq.jpg"),
popularity: 10.647,
imdbId: "nm0000093",
homepage: nil
imdbID: "nm0000093",
homepageURL: nil
)
],
totalResults: 1,
Expand Down
2 changes: 1 addition & 1 deletion Tests/TMDbTests/DTOs/StillURLProvidingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class StillURLProvidingTests: XCTestCase {
}

func testStillAspectRatioReturnsAspectRatio() {
let expectedResult: Float = 100 / 150
let expectedResult: Float = 500 / 281

let result = TestStillProvider.stillAspectRatio

Expand Down

0 comments on commit d8edc5b

Please sign in to comment.