-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FEATURE: TV series external IDs (#134)
* FEATURE: TV series external ids * Update documentation
- Loading branch information
1 parent
0c4fd7a
commit 8974cbf
Showing
15 changed files
with
296 additions
and
8 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
134 changes: 134 additions & 0 deletions
134
Sources/TMDb/Models/TVSeriesExternalLinksCollection.swift
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
import Foundation | ||
|
||
/// | ||
/// A model representing a collection of media databases and social IDs and links for a TV series. | ||
/// | ||
public struct TVSeriesExternalLinksCollection: Identifiable, Codable, Equatable, Hashable { | ||
|
||
/// | ||
/// The TMDb TV series identifier. | ||
/// | ||
public let id: TVSeries.ID | ||
|
||
/// | ||
/// IMDb link. | ||
/// | ||
public let imdb: IMDbLink? | ||
|
||
/// | ||
/// WikiData link. | ||
/// | ||
public let wikiData: WikiDataLink? | ||
|
||
/// | ||
/// Facebook link. | ||
/// | ||
public let facebook: FacebookLink? | ||
|
||
/// | ||
/// Instagram link. | ||
/// | ||
public let instagram: InstagramLink? | ||
|
||
/// | ||
/// Twitter link. | ||
/// | ||
public let twitter: TwitterLink? | ||
|
||
/// | ||
/// Creates an external links collection for a movie. | ||
/// | ||
/// - Parameters: | ||
/// - id: The TMDb TV series identifier. | ||
/// - imdb: IMDb link. | ||
/// - wikiData: WikiData link. | ||
/// - facebook: Facebook link. | ||
/// - instagram: Instagram link. | ||
/// - twitter: Twitter link. | ||
/// | ||
public init( | ||
id: TVSeries.ID, | ||
imdb: IMDbLink? = nil, | ||
wikiData: WikiDataLink? = nil, | ||
facebook: FacebookLink? = nil, | ||
instagram: InstagramLink? = nil, | ||
twitter: TwitterLink? = nil | ||
) { | ||
self.id = id | ||
self.imdb = imdb | ||
self.wikiData = wikiData | ||
self.facebook = facebook | ||
self.instagram = instagram | ||
self.twitter = twitter | ||
} | ||
|
||
public func hash(into hasher: inout Hasher) { | ||
hasher.combine(id) | ||
hasher.combine(imdb?.id) | ||
hasher.combine(wikiData?.id) | ||
hasher.combine(facebook?.id) | ||
hasher.combine(instagram?.id) | ||
hasher.combine(twitter?.id) | ||
} | ||
|
||
public static func == (lhs: TVSeriesExternalLinksCollection, rhs: TVSeriesExternalLinksCollection) -> Bool { | ||
lhs.id == rhs.id | ||
&& lhs.imdb == rhs.imdb | ||
&& lhs.wikiData == rhs.wikiData | ||
&& lhs.facebook == rhs.facebook | ||
&& lhs.instagram == rhs.instagram | ||
&& lhs.twitter == rhs.twitter | ||
} | ||
|
||
} | ||
|
||
extension TVSeriesExternalLinksCollection { | ||
|
||
private enum CodingKeys: String, CodingKey { | ||
case id | ||
case imdbID = "imdbId" | ||
case wikiDataID = "wikidataId" | ||
case facebookID = "facebookId" | ||
case instagramID = "instagramId" | ||
case twitterID = "twitterId" | ||
} | ||
|
||
public init(from decoder: Decoder) throws { | ||
let container = try decoder.container(keyedBy: CodingKeys.self) | ||
|
||
let id = try container.decode(TVSeries.ID.self, forKey: .id) | ||
|
||
let imdbID = try container.decodeIfPresent(String.self, forKey: .imdbID) | ||
let wikiDataID = try container.decodeIfPresent(String.self, forKey: .wikiDataID) | ||
let facebookID = try container.decodeIfPresent(String.self, forKey: .facebookID) | ||
let instagramID = try container.decodeIfPresent(String.self, forKey: .instagramID) | ||
let twitterID = try container.decodeIfPresent(String.self, forKey: .twitterID) | ||
|
||
let imdbLink = IMDbLink(imdbTitleID: imdbID) | ||
let wikiDataLink = WikiDataLink(wikiDataID: wikiDataID) | ||
let facebookLink = FacebookLink(facebookID: facebookID) | ||
let instagramLink = InstagramLink(instagramID: instagramID) | ||
let twitterLink = TwitterLink(twitterID: twitterID) | ||
|
||
self.init( | ||
id: id, | ||
imdb: imdbLink, | ||
wikiData: wikiDataLink, | ||
facebook: facebookLink, | ||
instagram: instagramLink, | ||
twitter: twitterLink | ||
) | ||
} | ||
|
||
public func encode(to encoder: Encoder) throws { | ||
var container = encoder.container(keyedBy: CodingKeys.self) | ||
|
||
try container.encode(id, forKey: .id) | ||
try container.encodeIfPresent(imdb?.id, forKey: .imdbID) | ||
try container.encodeIfPresent(wikiData?.id, forKey: .wikiDataID) | ||
try container.encodeIfPresent(facebook?.id, forKey: .facebookID) | ||
try container.encodeIfPresent(instagram?.id, forKey: .instagramID) | ||
try container.encodeIfPresent(twitter?.id, forKey: .twitterID) | ||
} | ||
|
||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
35 changes: 35 additions & 0 deletions
35
Tests/TMDbTests/Mocks/Models/TVSeriesExternalLinksCollection+Mocks.swift
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import Foundation | ||
@testable import TMDb | ||
|
||
extension TVSeriesExternalLinksCollection { | ||
|
||
static func mock( | ||
id: TVSeries.ID, | ||
imdb: IMDbLink? = nil, | ||
wikiData: WikiDataLink? = nil, | ||
facebook: FacebookLink? = nil, | ||
instagram: InstagramLink? = nil, | ||
twitter: TwitterLink? = nil | ||
) -> TVSeriesExternalLinksCollection { | ||
TVSeriesExternalLinksCollection( | ||
id: id, | ||
imdb: imdb, | ||
wikiData: wikiData, | ||
facebook: facebook, | ||
instagram: instagram, | ||
twitter: twitter | ||
) | ||
} | ||
|
||
static var lockeAndKey: TVSeriesExternalLinksCollection { | ||
.mock( | ||
id: 86423, | ||
imdb: IMDbLink(imdbTitleID: "tt3007572"), | ||
wikiData: nil, | ||
facebook: FacebookLink(facebookID: "lockeandkeynetflix"), | ||
instagram: InstagramLink(instagramID: "lockeandkeynetflix"), | ||
twitter: TwitterLink(twitterID: "lockekeynetflix") | ||
) | ||
} | ||
|
||
} |
41 changes: 41 additions & 0 deletions
41
Tests/TMDbTests/Models/TVSeriesExternalLinksCollectionTests.swift
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
@testable import TMDb | ||
import XCTest | ||
|
||
final class TVSeriesExternalLinksCollectionTests: XCTestCase { | ||
|
||
func testDecodeReturnsMovieExternalLinksCollection() throws { | ||
let expectedResult = TVSeriesExternalLinksCollection( | ||
id: 86423, | ||
imdb: IMDbLink(imdbTitleID: "tt3007572"), | ||
wikiData: nil, | ||
facebook: FacebookLink(facebookID: "lockeandkeynetflix"), | ||
instagram: InstagramLink(instagramID: "lockeandkeynetflix"), | ||
twitter: TwitterLink(twitterID: "lockekeynetflix") | ||
) | ||
|
||
let result = try JSONDecoder.theMovieDatabase.decode( | ||
TVSeriesExternalLinksCollection.self, | ||
fromResource: "tv-series-external-ids" | ||
) | ||
|
||
XCTAssertEqual(result, expectedResult) | ||
} | ||
|
||
func testEncodeAndDecodeReturnsMovieExternalLinksCollection() throws { | ||
let linksCollection = TVSeriesExternalLinksCollection( | ||
id: 86423, | ||
imdb: IMDbLink(imdbTitleID: "tt3007572"), | ||
wikiData: nil, | ||
facebook: FacebookLink(facebookID: "lockeandkeynetflix"), | ||
instagram: InstagramLink(instagramID: "lockeandkeynetflix"), | ||
twitter: TwitterLink(twitterID: "lockekeynetflix") | ||
) | ||
|
||
let data = try JSONEncoder.theMovieDatabase.encode(linksCollection) | ||
|
||
let result = try JSONDecoder.theMovieDatabase.decode(TVSeriesExternalLinksCollection.self, from: data) | ||
|
||
XCTAssertEqual(result, linksCollection) | ||
} | ||
|
||
} |
12 changes: 12 additions & 0 deletions
12
Tests/TMDbTests/Resources/json/tv-series-external-ids.json
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"id": 86423, | ||
"imdb_id": "tt3007572", | ||
"freebase_mid": null, | ||
"freebase_id": null, | ||
"tvdb_id": 361594, | ||
"tvrage_id": null, | ||
"wikidata_id": null, | ||
"facebook_id": "lockeandkeynetflix", | ||
"instagram_id": "lockeandkeynetflix", | ||
"twitter_id": "lockekeynetflix" | ||
} |
This file contains 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
Oops, something went wrong.