]> Git — Sourcephile - gargantext.git/blob - src/Gargantext/Text/Parsers/GrandDebat.hs
add rebranching to link distante branches
[gargantext.git] / src / Gargantext / Text / Parsers / GrandDebat.hs
1 {-|
2 Module : Gargantext.Text.Parsers.GrandDebat
3 Description : Grand Debat Types
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
8 Portability : POSIX
9
10 TODO: create a separate Lib.
11
12 -}
13
14 {-# LANGUAGE NoImplicitPrelude #-}
15 {-# LANGUAGE OverloadedStrings #-}
16 {-# LANGUAGE DeriveGeneric #-}
17 {-# LANGUAGE FlexibleInstances #-}
18
19 module Gargantext.Text.Parsers.GrandDebat
20 where
21
22 import GHC.IO (FilePath)
23 import Data.Aeson (ToJSON, FromJSON)
24 import qualified Data.JsonStream.Parser as P
25 --import Data.Either (either)
26 import Data.Maybe (Maybe())
27 import Data.Text (Text)
28 import qualified Data.Text as Text
29 import qualified Data.ByteString.Lazy as DBL
30 import GHC.Generics (Generic)
31 import Gargantext.Prelude
32 import Gargantext.Database.Types.Node
33 import Gargantext.Core (Lang(..))
34
35
36 data GrandDebatReference = GrandDebatReference
37 { id :: !(Maybe Text)
38 , reference :: !(Maybe Text)
39 , title :: !(Maybe Text)
40
41 , createdAt :: !(Maybe Text)
42 , publishedAt :: !(Maybe Text)
43 , updatedAt :: !(Maybe Text)
44
45 , trashed :: !(Maybe Bool)
46 , trashedStatus :: !(Maybe Text)
47
48 , authorId :: !(Maybe Text)
49 , authorType :: !(Maybe Text)
50 , authorZipCode :: !(Maybe Text)
51
52 , responses :: !(Maybe [GrandDebatResponse])
53 }
54 deriving (Show, Generic)
55
56
57 data GrandDebatResponse = GrandDebatResponse
58 { questionId :: !(Maybe Text)
59 , questionTitle :: !(Maybe Text)
60 , value :: !(Maybe Text)
61 , formattedValue :: !(Maybe Text)
62 }
63 deriving (Show, Generic)
64
65 instance FromJSON GrandDebatResponse
66 instance FromJSON GrandDebatReference
67
68 instance ToJSON GrandDebatResponse
69 instance ToJSON GrandDebatReference
70
71
72 instance ToHyperdataDocument GrandDebatReference
73 where
74 toHyperdataDocument (GrandDebatReference id' _ref title'
75 _createdAt' publishedAt' _updatedAt
76 _trashed _trashedStatus
77 _authorId authorType' authorZipCode'
78 responses') =
79 HyperdataDocument (Just "GrandDebat") id'
80 Nothing Nothing Nothing Nothing
81 title' authorType' authorType' authorZipCode'
82 (toAbstract <$> responses')
83 publishedAt'
84 Nothing Nothing Nothing Nothing Nothing Nothing
85 (Just $ Text.pack $ show FR)
86 where
87 toAbstract = (Text.intercalate " . ") . ((filter (/= "")) . (map toSentence))
88 toSentence (GrandDebatResponse _id _qtitle _qvalue r) = case r of
89 Nothing -> ""
90 Just r' -> case Text.length r' > 10 of
91 True -> r'
92 False -> ""
93
94 class ReadFile a
95 where
96 readFile :: FilePath -> IO a
97
98 instance ReadFile [GrandDebatReference]
99 where
100 -- | read json: 3 version below are working but with increased optimization
101 --readFile fp = maybe [] identity <$> decode <$> DBL.readFile fp
102 --readFile fp = either (panic . Text.pack) identity <$> P.eitherDecode <$> DBL.readFile fp
103 readFile fp = P.parseLazyByteString (P.arrayOf P.value) <$> DBL.readFile fp
104
105
106