-
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.
Showing
14 changed files
with
381 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
149 changes: 149 additions & 0 deletions
149
Sources/TMDb/Models/PersonExternalLinksCollection.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,149 @@ | ||
import Foundation | ||
|
||
/// | ||
/// A model representing a collection of media databases and social IDs and links for a person. | ||
/// | ||
public struct PersonExternalLinksCollection: Identifiable, Codable, Equatable, Hashable { | ||
|
||
/// | ||
/// The TMDb person identifier. | ||
/// | ||
public let id: Person.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? | ||
|
||
/// | ||
/// TikTok llink. | ||
/// | ||
public let tikTok: TikTokLink? | ||
|
||
/// | ||
/// Creates an external links collection for a movie. | ||
/// | ||
/// - Parameters: | ||
/// - id: The TMDb person identifier. | ||
/// - imdb: IMDb link. | ||
/// - wikiData: WikiData link. | ||
/// - facebook: Facebook link. | ||
/// - instagram: Instagram link. | ||
/// - twitter: Twitter link. | ||
/// - tikTok: TikTok link. | ||
/// | ||
public init( | ||
id: Movie.ID, | ||
imdb: IMDbLink? = nil, | ||
wikiData: WikiDataLink? = nil, | ||
facebook: FacebookLink? = nil, | ||
instagram: InstagramLink? = nil, | ||
twitter: TwitterLink? = nil, | ||
tikTok: TikTokLink? = nil | ||
) { | ||
self.id = id | ||
self.imdb = imdb | ||
self.wikiData = wikiData | ||
self.facebook = facebook | ||
self.instagram = instagram | ||
self.twitter = twitter | ||
self.tikTok = tikTok | ||
} | ||
|
||
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) | ||
hasher.combine(tikTok?.id) | ||
} | ||
|
||
public static func == (lhs: PersonExternalLinksCollection, rhs: PersonExternalLinksCollection) -> 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 | ||
&& lhs.tikTok == rhs.tikTok | ||
} | ||
|
||
} | ||
|
||
extension PersonExternalLinksCollection { | ||
|
||
private enum CodingKeys: String, CodingKey { | ||
case id | ||
case imdbID = "imdbId" | ||
case wikiDataID = "wikidataId" | ||
case facebookID = "facebookId" | ||
case instagramID = "instagramId" | ||
case twitterID = "twitterId" | ||
case tikTokID = "tiktokId" | ||
} | ||
|
||
public init(from decoder: Decoder) throws { | ||
let container = try decoder.container(keyedBy: CodingKeys.self) | ||
|
||
let id = try container.decode(Movie.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 tikTokID = try container.decodeIfPresent(String.self, forKey: .tikTokID) | ||
|
||
let imdbLink = IMDbLink(imdbNameID: imdbID) | ||
let wikiDataLink = WikiDataLink(wikiDataID: wikiDataID) | ||
let facebookLink = FacebookLink(facebookID: facebookID) | ||
let instagramLink = InstagramLink(instagramID: instagramID) | ||
let twitterLink = TwitterLink(twitterID: twitterID) | ||
let tikTokLink = TikTokLink(tikTokID: tikTokID) | ||
|
||
self.init( | ||
id: id, | ||
imdb: imdbLink, | ||
wikiData: wikiDataLink, | ||
facebook: facebookLink, | ||
instagram: instagramLink, | ||
twitter: twitterLink, | ||
tikTok: tikTokLink | ||
) | ||
} | ||
|
||
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) | ||
try container.encodeIfPresent(tikTok?.id, forKey: .tikTokID) | ||
} | ||
|
||
} |
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,53 @@ | ||
import Foundation | ||
|
||
/// | ||
/// A TikTok external link. | ||
/// | ||
/// e.g. to a person's TikTok profile. | ||
/// | ||
public struct TikTokLink: ExternalLink { | ||
|
||
/// | ||
/// TikTok profile identifier. | ||
/// | ||
public let id: String | ||
|
||
/// | ||
/// URL of the TikTok profile page. | ||
/// | ||
public let url: URL | ||
|
||
/// | ||
/// Creates a TikTok link object using a TikTok user identifier. | ||
/// | ||
/// - Parameter tikTokID: The TikTok user identifier. | ||
/// | ||
public init?(tikTokID: String?) { | ||
guard | ||
let tikTokID, | ||
let url = Self.tikTokURL(for: tikTokID) | ||
else { | ||
return nil | ||
} | ||
|
||
self.init(id: tikTokID, url: url) | ||
} | ||
|
||
} | ||
|
||
extension TikTokLink { | ||
|
||
private init(id: String, url: URL) { | ||
self.id = id | ||
self.url = url | ||
} | ||
|
||
} | ||
|
||
extension TikTokLink { | ||
|
||
private static func tikTokURL(for tikTokID: String) -> URL? { | ||
URL(string: "https://www.tiktok.com/@\(tikTokID)") | ||
} | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,3 +24,4 @@ | |
|
||
- ``knownFor(forPerson:)`` | ||
- ``popular(page:)`` | ||
- ``externalLinks(forPerson:)`` |
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
38 changes: 38 additions & 0 deletions
38
Tests/TMDbTests/Mocks/Models/PersonExternalLinksCollection+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,38 @@ | ||
import Foundation | ||
@testable import TMDb | ||
|
||
extension PersonExternalLinksCollection { | ||
|
||
static func mock( | ||
id: Person.ID, | ||
imdb: IMDbLink? = nil, | ||
wikiData: WikiDataLink? = nil, | ||
facebook: FacebookLink? = nil, | ||
instagram: InstagramLink? = nil, | ||
twitter: TwitterLink? = nil, | ||
tikTok: TikTokLink? = nil | ||
) -> PersonExternalLinksCollection { | ||
PersonExternalLinksCollection( | ||
id: id, | ||
imdb: imdb, | ||
wikiData: wikiData, | ||
facebook: facebook, | ||
instagram: instagram, | ||
twitter: twitter, | ||
tikTok: tikTok | ||
) | ||
} | ||
|
||
static var sydneySweeney: PersonExternalLinksCollection { | ||
.mock( | ||
id: 346698, | ||
imdb: IMDbLink(imdbNameID: "nm2858875"), | ||
wikiData: WikiDataLink(wikiDataID: "Q49561909"), | ||
facebook: FacebookLink(facebookID: "sydney_sweeney"), | ||
instagram: InstagramLink(instagramID: "sydney_sweeney"), | ||
twitter: TwitterLink(twitterID: "sydney_sweeney"), | ||
tikTok: TikTokLink(tikTokID: "syds_garage") | ||
) | ||
} | ||
|
||
} |
43 changes: 43 additions & 0 deletions
43
Tests/TMDbTests/Models/PersonExternalLinksCollectionTests.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,43 @@ | ||
@testable import TMDb | ||
import XCTest | ||
|
||
final class PersonExternalLinksCollectionTests: XCTestCase { | ||
|
||
func testDecodeReturnsPersonExternalLinksCollection() throws { | ||
let expectedResult = PersonExternalLinksCollection( | ||
id: 115440, | ||
imdb: IMDbLink(imdbNameID: "nm2858875"), | ||
wikiData: WikiDataLink(wikiDataID: "Q49561909"), | ||
facebook: FacebookLink(facebookID: "sydney_sweeney"), | ||
instagram: InstagramLink(instagramID: "sydney_sweeney"), | ||
twitter: TwitterLink(twitterID: "sydney_sweeney"), | ||
tikTok: TikTokLink(tikTokID: "syds_garage") | ||
) | ||
|
||
let result = try JSONDecoder.theMovieDatabase.decode( | ||
PersonExternalLinksCollection.self, | ||
fromResource: "person-external-ids" | ||
) | ||
|
||
XCTAssertEqual(result, expectedResult) | ||
} | ||
|
||
func testEncodeAndDecodeReturnsMovieExternalLinksCollection() throws { | ||
let linksCollection = PersonExternalLinksCollection( | ||
id: 115440, | ||
imdb: IMDbLink(imdbNameID: "nm2858875"), | ||
wikiData: WikiDataLink(wikiDataID: "Q49561909"), | ||
facebook: FacebookLink(facebookID: "sydney_sweeney"), | ||
instagram: InstagramLink(instagramID: "sydney_sweeney"), | ||
twitter: TwitterLink(twitterID: "sydney_sweeney"), | ||
tikTok: TikTokLink(tikTokID: "syds_garage") | ||
) | ||
|
||
let data = try JSONEncoder.theMovieDatabase.encode(linksCollection) | ||
|
||
let result = try JSONDecoder.theMovieDatabase.decode(PersonExternalLinksCollection.self, from: data) | ||
|
||
XCTAssertEqual(result, linksCollection) | ||
} | ||
|
||
} |
Oops, something went wrong.