Last active
May 10, 2018 20:59
-
-
Save bolivarbryan/8f9f9ab3d18f48531bb2920bfc5ad493 to your computer and use it in GitHub Desktop.
The MovieDatabase Parser
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 | |
var json = """ | |
{ | |
"results": [ | |
{ | |
"vote_average": 7.4, | |
"vote_count": 6066, | |
"id": 284053, | |
"video": false, | |
"media_type": "movie", | |
"title": "Thor: Ragnarok", | |
"popularity": 176.328774, | |
"poster_path": "/rzRwTcFvttcN1ZpX2xv4j3tSdJu.jpg", | |
"original_language": "en", | |
"original_title": "Thor: Ragnarok", | |
"genre_ids": [ | |
28, | |
12, | |
14 | |
], | |
"backdrop_path": "/kaIfm5ryEOwYg8mLbq8HkPuM1Fo.jpg", | |
"adult": false, | |
"overview": "Thor is imprisoned on the other side of the universe and finds himself in a race against time to get back to Asgard to stop Ragnarok, the prophecy of destruction to his homeworld and the end of Asgardian civilization, at the hands of an all-powerful new threat, the ruthless Hela.", | |
"release_date": "2017-10-25" | |
}, | |
{ | |
"vote_average": 7.3, | |
"vote_count": 7242, | |
"id": 315635, | |
"video": false, | |
"media_type": "movie", | |
"title": "Spider-Man: Homecoming", | |
"popularity": 52.43484, | |
"poster_path": "/c24sv2weTHPsmDa7jEMN0m2P3RT.jpg", | |
"original_language": "en", | |
"original_title": "Spider-Man: Homecoming", | |
"genre_ids": [ | |
28, | |
12, | |
35, | |
878 | |
], | |
"backdrop_path": "/vc8bCGjdVp0UbMNLzHnHSLRbBWQ.jpg", | |
"adult": false, | |
"overview": "Following the events of Captain America: Civil War, Peter Parker, with the help of his mentor Tony Stark, tries to balance his life as an ordinary high school student in Queens, New York City, with fighting crime as his superhero alter ego Spider-Man as a new threat, the Vulture, emerges.", | |
"release_date": "2017-07-05" | |
}, | |
{ | |
"vote_average": 7.6, | |
"vote_count": 7976, | |
"id": 283995, | |
"video": false, | |
"media_type": "movie", | |
"title": "Guardians of the Galaxy Vol. 2", | |
"popularity": 54.975292, | |
"poster_path": "/y4MBh0EjBlMuOzv9axM4qJlmhzz.jpg", | |
"original_language": "en", | |
"original_title": "Guardians of the Galaxy Vol. 2", | |
"genre_ids": [ | |
28, | |
12, | |
35, | |
878 | |
], | |
"backdrop_path": "/aJn9XeesqsrSLKcHfHP4u5985hn.jpg", | |
"adult": false, | |
"overview": "The Guardians must fight to keep their newfound family together as they unravel the mysteries of Peter Quill's true parentage.", | |
"release_date": "2017-04-19" | |
} | |
] | |
} | |
""".data(using: .utf8)! | |
extension DateFormatter { | |
func formatDate(from string: String) -> Date? { | |
self.dateFormat = "yyyy-MM-dd" | |
return self.date(from: string) | |
} | |
} | |
struct Movie: Codable, CustomStringConvertible { | |
let title: String | |
let voteAverage: Float | |
let id: Int | |
let video: Bool | |
let mediaType: String | |
let popularity: Float | |
let originalTitle: String | |
let overview: String | |
private let releaseDate: String | |
var releaseDateValue: Date { | |
return DateFormatter().date(from: releaseDate) ?? Date() | |
} | |
var description: String { | |
return "\n TITLE: \(title), AVERAGE: \(voteAverage), POPULARITY: \(popularity), RELEASE: \(releaseDate) \n" | |
} | |
} | |
struct MoviesResponse: Codable { | |
var movies: [Movie] | |
enum MovieClasification { | |
case popular | |
case top | |
case upcomming | |
} | |
private enum CodingKeys: String, CodingKey { | |
case movies = "results" | |
} | |
func list(by clasification: MovieClasification) -> [Movie] { | |
switch clasification { | |
case .popular: | |
return movies.sorted{ $0.popularity > $1.popularity } | |
case .top: | |
return movies.sorted{ $0.voteAverage > $1.voteAverage } | |
case .upcomming: | |
return movies.sorted{ $0.releaseDateValue < $1.releaseDateValue } | |
} | |
} | |
func filterMovie(name: String, clasification: MovieClasification) -> [Movie] { | |
return list(by: clasification).filter { $0.title.contains(name) } | |
} | |
} | |
let decoder = JSONDecoder() | |
decoder.keyDecodingStrategy = .convertFromSnakeCase | |
let moviesResponse = try! decoder.decode(MoviesResponse.self, from: json) | |
print(moviesResponse.list(by: .popular)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment