Browse Source

Start moving types into their own files

tags/v0.2.2.0
Peter J. Jones 6 years ago
parent
commit
2a2ade751e

+ 9
- 57
Network/API/TheMovieDB/Types.hs View File

@@ -21,14 +21,11 @@ module Network.API.TheMovieDB.Types
) where

-- Imports.
import Data.Aeson
import Data.Aeson.Types (typeMismatch)
import Control.Applicative
import System.Locale
import Data.Time
import Data.Text (unpack)
import Network.API.TheMovieDB.Types.Genre
import Network.API.TheMovieDB.Types.Movie
import Network.API.TheMovieDB.Types.ReleaseDate

-- | Type for the API key issued by TheMovieDB.
type APIKey = String

-- | Possible errors returned by the API.
@@ -37,59 +34,8 @@ data APIError
| ParseError String -- ^ Invalid or error response from the API.
deriving (Eq, Show)

newtype ReleaseDate =
ReleaseDate {releaseDate :: Day}
deriving (Eq, Show)

instance FromJSON ReleaseDate where
parseJSON (String t) =
case parseTime defaultTimeLocale "%Y-%m-%d" (unpack t) of
Just d -> pure $ ReleaseDate d
_ -> fail "could not parse release_date"
parseJSON v = typeMismatch "ReleaseDate" v

type GenreID = Int

data Genre =
Genre
{ genreID :: GenreID -- ^ TheMovieDB unique ID.
, genreName :: String -- ^ The name of the genre.
} deriving (Eq, Show)

instance FromJSON Genre where
parseJSON (Object v) = Genre <$> v .: "id" <*> v .: "name"
parseJSON v = typeMismatch "Genre" v

type MovieID = Int

data Movie =
Movie
{ movieID :: MovieID -- ^ TheMovieDB unique ID.
, movieTitle :: String -- ^ The name/title of the movie.
, movieOverview :: String -- ^ Short plot summary.
, movieGenres :: [Genre] -- ^ List of genre names.
, moviePopularity :: Double -- ^ Popularity ranking.
, moviePosterPath :: String -- ^ FIXME:
, movieReleaseDate :: ReleaseDate -- ^ Movie release date.
} deriving (Eq, Show)

instance FromJSON Movie where
parseJSON (Object v) = do
genres <- maybe (pure []) parseJSON <$> (v .:? "genres")
poster <- maybe (pure defaultPoster) posterURL <$> (v .:? "poster_path")
Movie <$> v .: "id"
<*> v .: "title"
<*> v .:? "overview" .!= ""
<*> genres
<*> v .:? "popularity" .!= 0.0
<*> poster
<*> v .:? "release_date" .!= defaultDate
where defaultDate = ReleaseDate $ ModifiedJulianDay 0
defaultPoster = "" :: String
posterURL p = parseJSON p
parseJSON _ = empty
-- data Config =
-- Config
-- { cfgAPIKey :: Key
-- , cfgIOFunc :: Config -> Query -> IO (Either APIError BodyContent)
-- }

+ 34
- 0
Network/API/TheMovieDB/Types/Genre.hs View File

@@ -0,0 +1,34 @@
{-# LANGUAGE OverloadedStrings #-}

{-

This file is part of the Haskell package themoviedb. It is subject to
the license terms in the LICENSE file found in the top-level directory
of this distribution and at git://pmade.com/themoviedb/LICENSE. No
part of themoviedb package, including this file, may be copied,
modified, propagated, or distributed except according to the terms
contained in the LICENSE file.

-}
module Network.API.TheMovieDB.Types.Genre
( GenreID
, Genre(..)
) where

import Control.Applicative
import Data.Aeson
import Data.Aeson.Types (typeMismatch)

-- | Type for representing unique genre IDs.
type GenreID = Int

-- | Metadata for a genre.
data Genre =
Genre
{ genreID :: GenreID -- ^ TheMovieDB unique ID.
, genreName :: String -- ^ The name of the genre.
} deriving (Eq, Show)

instance FromJSON Genre where
parseJSON (Object v) = Genre <$> v .: "id" <*> v .: "name"
parseJSON v = typeMismatch "Genre" v

+ 54
- 0
Network/API/TheMovieDB/Types/Movie.hs View File

@@ -0,0 +1,54 @@
{-# LANGUAGE OverloadedStrings #-}

{-

This file is part of the Haskell package themoviedb. It is subject to
the license terms in the LICENSE file found in the top-level directory
of this distribution and at git://pmade.com/themoviedb/LICENSE. No
part of themoviedb package, including this file, may be copied,
modified, propagated, or distributed except according to the terms
contained in the LICENSE file.

-}
module Network.API.TheMovieDB.Types.Movie
( MovieID
, Movie(..)
) where

import Control.Applicative
import Data.Aeson
import Data.Aeson.Types (typeMismatch)
import Data.Time
import Network.API.TheMovieDB.Types.Genre
import Network.API.TheMovieDB.Types.ReleaseDate

-- | Type for representing unique movie IDs.
type MovieID = Int

-- | Metadata for a movie.
data Movie =
Movie
{ movieID :: MovieID -- ^ TheMovieDB unique ID.
, movieTitle :: String -- ^ The name/title of the movie.
, movieOverview :: String -- ^ Short plot summary.
, movieGenres :: [Genre] -- ^ List of genre names.
, moviePopularity :: Double -- ^ Popularity ranking.
, moviePosterPath :: String -- ^ FIXME:
, movieReleaseDate :: ReleaseDate -- ^ Movie release date.
} deriving (Eq, Show)

instance FromJSON Movie where
parseJSON (Object v) = do
genres <- maybe (pure []) parseJSON <$> (v .:? "genres")
poster <- maybe (pure defaultPoster) posterURL <$> (v .:? "poster_path")
Movie <$> v .: "id"
<*> v .: "title"
<*> v .:? "overview" .!= ""
<*> genres
<*> v .:? "popularity" .!= 0.0
<*> poster
<*> v .:? "release_date" .!= defaultDate
where defaultDate = ReleaseDate $ ModifiedJulianDay 0
defaultPoster = "" :: String
posterURL p = parseJSON p
parseJSON _ = empty

+ 34
- 0
Network/API/TheMovieDB/Types/ReleaseDate.hs View File

@@ -0,0 +1,34 @@
{-# LANGUAGE OverloadedStrings #-}

{-

This file is part of the Haskell package themoviedb. It is subject to
the license terms in the LICENSE file found in the top-level directory
of this distribution and at git://pmade.com/themoviedb/LICENSE. No
part of themoviedb package, including this file, may be copied,
modified, propagated, or distributed except according to the terms
contained in the LICENSE file.

-}
module Network.API.TheMovieDB.Types.ReleaseDate
( ReleaseDate(..)
) where

import Control.Applicative
import Data.Aeson
import Data.Aeson.Types (typeMismatch)
import Data.Text (unpack)
import Data.Time
import System.Locale

-- | Type wrapper for Day to parse a movie's release date.
newtype ReleaseDate =
ReleaseDate {releaseDate :: Day}
deriving (Eq, Show)

instance FromJSON ReleaseDate where
parseJSON (String t) =
case parseTime defaultTimeLocale "%Y-%m-%d" (unpack t) of
Just d -> pure $ ReleaseDate d
_ -> fail "could not parse release_date"
parseJSON v = typeMismatch "ReleaseDate" v

+ 5
- 1
themoviedb.cabal View File

@@ -21,7 +21,11 @@ library
other-modules: Network.API.TheMovieDB.Fetch,
Network.API.TheMovieDB.Search,
Network.API.TheMovieDB.HTTP,
Network.API.TheMovieDB.Types
Network.API.TheMovieDB.Types,
Network.API.TheMovieDB.Types.Genre,
Network.API.TheMovieDB.Types.Movie,
Network.API.TheMovieDB.Types.ReleaseDate

build-depends:
base ==4.3.*,
old-locale ==1.0.*,

Loading…
Cancel
Save