Forráskód Böngészése

Add more intermediate functions in output

jherve 1 éve
szülő
commit
0961836db4
2 módosított fájl, 31 hozzáadás és 20 törlés
  1. 26 14
      src/LinkedIn/Output.purs
  2. 5 6
      test/Utils.purs

+ 26 - 14
src/LinkedIn/Output.purs

@@ -1,4 +1,4 @@
-module LinkedIn.Output (module LinkedIn.Output.Types, run, runToDetached, runToUI, toOutput) where
+module LinkedIn.Output (module LinkedIn.Output.Types, run, detachNodes, runToUI, toOutput) where
 
 import Prelude
 
@@ -16,7 +16,6 @@ import LinkedIn.Page.Projects (ProjectsPage)
 import LinkedIn.Page.Skills (SkillsPage)
 import LinkedIn.Page.WorkExperiences (WorkExperiencesPage)
 import LinkedIn.PageUrl (PageUrl(..))
-import LinkedIn.QueryRunner (QueryError)
 import LinkedIn.UI.Elements.Parser (toUIElement)
 import LinkedIn.UI.Elements.Types (UIElement)
 import Parsing (parseErrorMessage)
@@ -30,29 +29,42 @@ run :: forall root t.
   => Proxy t
   -> root
   -> ExceptT OutputError Effect Output
-run prox dom = do
-  asUI <- runToUI prox dom
-  withExceptT (\err -> ErrorOnExtract err) $ except $ LE.extract asUI
+run prox dom = detachNodes prox dom >>= convertToUI >>= extractData
 
-runToDetached :: forall root t.
+runToUI :: forall root t.
   Traversable t
   => CanBeQueried root t
   => Proxy t
   -> root
-  -> ExceptT QueryError Effect (t DetachedNode)
-runToDetached _ dom = do
-  qRes <- CBQ.query dom
-  lift $ traverse toDetached qRes
+  -> ExceptT OutputError Effect (t UIElement)
+runToUI prox dom = detachNodes prox dom >>= convertToUI
 
-runToUI :: forall root t.
+detachNodes :: forall root t.
   Traversable t
   => CanBeQueried root t
   => Proxy t
   -> root
+  -> ExceptT OutputError Effect (t DetachedNode)
+detachNodes _ dom = withExceptT (\err -> ErrorOnDetach err) detached
+  where
+    detached = do
+      qRes <- CBQ.query dom
+      lift $ traverse toDetached qRes
+
+convertToUI :: forall t.
+  Traversable t
+  => t DetachedNode
   -> ExceptT OutputError Effect (t UIElement)
-runToUI prox dom = do
-  detached <- withExceptT (\err -> ErrorOnDetach err) $ runToDetached prox dom
-  withExceptT (\err -> ErrorOnUIConversion $ parseErrorMessage err) $ except $ traverse toUIElement detached
+convertToUI detached = withExceptT
+  (\err -> ErrorOnUIConversion $ parseErrorMessage err) $
+  except $ traverse toUIElement detached
+
+extractData :: forall t.
+  Traversable t
+  => Extractible t
+  => t UIElement
+  -> ExceptT OutputError Effect Output
+extractData asUI = withExceptT (\err -> ErrorOnExtract err) $ except $ LE.extract asUI
 
 toOutput ∷ PageUrl → (Document → ExceptT OutputError Effect Output)
 toOutput = case _ of

+ 5 - 6
test/Utils.purs

@@ -13,8 +13,7 @@ import Effect.Class (liftEffect)
 import LinkedIn.CanBeQueried (class CanBeQueried)
 import LinkedIn.DetachedNode (DetachedNode)
 import LinkedIn.Extractible (class Extractible)
-import LinkedIn.Output (Output, OutputError, run, runToDetached)
-import LinkedIn.QueryRunner (QueryError)
+import LinkedIn.Output (Output, OutputError, detachNodes, run)
 import LinkedIn.UI.Basic.Parser (toYear)
 import LinkedIn.UI.Basic.Types (MonthYear(..))
 import LinkedIn.UI.Elements.Parser (toUIElement)
@@ -38,20 +37,20 @@ detachFromFile :: forall t.
   => CanBeQueried Document t
   => Proxy t
   -> String
-  -> Aff (Either QueryError (t DetachedNode))
+  -> Aff (Either OutputError (t DetachedNode))
 detachFromFile proxy filePath = do
   dom <- liftEffect $ jsDomFromFile filePath
-  liftEffect $ runExceptT $ runToDetached proxy dom
+  liftEffect $ runExceptT $ detachNodes proxy dom
 
 detachFromString :: forall t.
   Traversable t
   => CanBeQueried Document t
   => Proxy t
   -> String
-  -> Aff (Either QueryError (t DetachedNode))
+  -> Aff (Either OutputError (t DetachedNode))
 detachFromString proxy string = do
   dom <- liftEffect $ jsDomParse string
-  liftEffect $ runExceptT $ runToDetached proxy dom
+  liftEffect $ runExceptT $ detachNodes proxy dom
 
 getOutputFromFile :: forall t.
   Traversable t