translate-cli (empty) → 0.1.0.0
raw patch · 8 files changed
+211/−0 lines, 8 filesdep +HUnitdep +aesondep +aeson-lenssetup-changed
Dependencies added: HUnit, aeson, aeson-lens, base, lens, text, translate-cli, turtle, wreq
Files
- LICENSE +21/−0
- README.md +12/−0
- Setup.hs +2/−0
- app/Main.hs +30/−0
- src/Lib.hs +39/−0
- src/Model.hs +27/−0
- test/Spec.hs +30/−0
- translate-cli.cabal +50/−0
+ LICENSE view
@@ -0,0 +1,21 @@+MIT License++Copyright (c) 2017 Andy++Permission is hereby granted, free of charge, to any person obtaining a copy+of this software and associated documentation files (the "Software"), to deal+in the Software without restriction, including without limitation the rights+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell+copies of the Software, and to permit persons to whom the Software is+furnished to do so, subject to the following conditions:++The above copyright notice and this permission notice shall be included in all+copies or substantial portions of the Software.++THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE+SOFTWARE.
+ README.md view
@@ -0,0 +1,12 @@+# translate-cli++Command Line Interface to translate words++## Build++`stack build`++## Run++* `stack exec translate` to show help+* `stack exec translate -- <text-to-translate>` to translate
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ app/Main.hs view
@@ -0,0 +1,30 @@+{-# LANGUAGE OverloadedStrings #-}++module Main where++import Turtle+import Lib+import Paths_translate_cli (version)+import Data.Version (showVersion)+import Data.Text (unpack)++parseVersion :: Parser (IO ())+parseVersion =+ subcommand "version" "Show version information" $ pure printVersion++parseMain :: Parser (IO ())+parseMain = fmap (translateText . unpack) parseArgs++parser :: Parser (IO ())+parser = parseVersion <|> parseMain++main :: IO ()+main = do+ cmd <- options "Translation tool for the commandline" parser+ cmd++printVersion :: IO()+printVersion = putStrLn $ showVersion version++parseArgs :: Parser Text+parseArgs = argText "text" "Text to translate"
+ src/Lib.hs view
@@ -0,0 +1,39 @@+{-# LANGUAGE OverloadedStrings, ScopedTypeVariables #-}++module Lib(translateText) where++import Network.Wreq+import Control.Lens ((&), (^.), (.~))+import Data.Text (Text, pack)+import GHC.Generics (Generic)+import Data.Aeson++import Model++instance FromJSON Phrase+instance FromJSON Translation+instance FromJSON RestResponse++translateText :: String -> IO()+translateText phrase = do+ r <- getWith (getOptions phrase) $ apiBase+ let body = r ^. responseBody+ decoded = eitherDecode body :: Either String RestResponse+ printPhrases $ fmap (take 5 . toPhrases) decoded++apiBase :: String+apiBase = "https://glosbe.com/gapi_v0_1/translate"++getOptions :: String -> Options+getOptions phrase = defaults+ & param "phrase" .~ [pack phrase]+ & param "from" .~ ["de"]+ & param "dest" .~ ["en"]+ & param "format" .~ ["json"]++printPhrases :: Either String [String] -> IO()+printPhrases phrases =+ case phrases of+ Left _ -> print "No result"+ Right phrases -> mapM_ putStrLn phrases+
+ src/Model.hs view
@@ -0,0 +1,27 @@+{-# LANGUAGE DeriveGeneric, OverloadedStrings #-}+module Model(RestResponse(..), Translation(..), Phrase(..), toPhrases) where++import Data.Text (Text)+import GHC.Generics (Generic)+import Data.Maybe+++data Phrase = Phrase { text :: String+ , language :: String+ } deriving (Generic, Show)++data Translation = Translation { phrase :: Maybe Phrase+ } deriving (Generic, Show)++data RestResponse = RestResponse { result :: String+ , tuc :: [Translation]+ } deriving (Generic, Show)++toPhrases :: RestResponse -> [String]+toPhrases = fmap formatPhrase . getPhrases . tuc++getPhrases :: [Translation] -> [Phrase]+getPhrases = catMaybes . fmap phrase++formatPhrase :: Phrase -> String+formatPhrase (Phrase text lang) = "[" ++ lang ++ "] " ++ text
+ test/Spec.hs view
@@ -0,0 +1,30 @@+{-# LANGUAGE OverloadedStrings #-}++import Test.HUnit+import Model++testToPhrasesEmptyList :: Test+testToPhrasesEmptyList = TestCase+ $ assertEqual "Should return empty list if no input"+ []+ (toPhrases response1)++testToPhrasesWithResponse :: Test+testToPhrasesWithResponse = TestCase+ $ assertEqual "Should return empty list if no input"+ ["[en] cat", "[en] dog"]+ (toPhrases response2)++main :: IO Counts+main = runTestTT $ TestList [testToPhrasesEmptyList, testToPhrasesWithResponse]++-- fake data++enPhrase1 = Phrase "cat" "en"+enPhrase2 = Phrase "dog" "en"++response1 = RestResponse "ok" ([]::[Translation])+response2 = RestResponse "ok" ([translation1, translation2])++translation1 = Translation (Just enPhrase1)+translation2 = Translation (Just enPhrase2)
+ translate-cli.cabal view
@@ -0,0 +1,50 @@+name: translate-cli+version: 0.1.0.0+synopsis: Translation cli tool+description: Translation cli tool+homepage: https://github.com/andys8/translate-cli#readme+license: MIT+license-file: LICENSE+author: andys8+maintainer: as@99n.de+copyright: 2017+category: Cli+build-type: Simple+extra-source-files: README.md+cabal-version: >=1.10++library+ hs-source-dirs: src+ exposed-modules: Lib+ , Model+ build-depends: base >= 4.7 && < 5+ , wreq+ , lens+ , text+ , aeson+ , aeson-lens+ default-language: Haskell2010++executable translate+ hs-source-dirs: app+ main-is: Main.hs+ ghc-options: -threaded -rtsopts -with-rtsopts=-N+ build-depends: base+ , translate-cli+ , turtle+ , text+ default-language: Haskell2010++test-suite translate-cli-test+ type: exitcode-stdio-1.0+ hs-source-dirs: test+ main-is: Spec.hs+ build-depends: base+ , translate-cli+ , HUnit+ ghc-options: -threaded -rtsopts -with-rtsopts=-N+ default-language: Haskell2010++source-repository head+ type: git+ location: https://github.com/andys8/translate-cli