Преглед изворни кода

Move functions to Output module

jherve пре 1 година
родитељ
комит
4cd312ff85
2 измењених фајлова са 52 додато и 54 уклоњено
  1. 3 51
      src/LinkedIn.purs
  2. 49 3
      src/LinkedIn/Output.purs

+ 3 - 51
src/LinkedIn.purs

@@ -1,22 +1,14 @@
-module LinkedIn (run, runToDetached, getContext) where
+module LinkedIn (module LinkedIn.Output, getContext) where
 
 import Prelude
 
 import Data.Either (Either(..))
 import Data.Maybe (Maybe(..))
-import Data.Traversable (class Traversable, traverse)
 import Effect (Effect)
-import LinkedIn.DetachedNode (DetachedNode, toDetached)
-import LinkedIn.Extractible (class Extractible)
-import LinkedIn.Extractible as LE
-import LinkedIn.Output.Types (Output)
+import LinkedIn.Output (run, runToDetached, toOutput)
 import LinkedIn.PageUrl (PageUrl, pageUrlP)
-import LinkedIn.QueryRunner (QueryError, QueryRunner', runQuery)
-import LinkedIn.UI.Elements.Parser (fromDetachedToUI)
-import LinkedIn.UI.Elements.Types (UIElement)
 import Parsing (runParser)
-import Type.Proxy (Proxy)
-import Web.DOM (Document, Node)
+import Web.DOM (Document)
 import Web.DOM.Document (url)
 import Web.URL as U
 
@@ -28,43 +20,3 @@ getContext dom = do
     Just u' -> case runParser (U.pathname u') pageUrlP of
       Left _ -> Left "Unexpected URL"
       Right page -> Right page
-
-run :: forall t.
-  Traversable t
-  => Extractible t
-  => Proxy t
-  -> Document
-  -> Effect (Either String Output)
-run prox dom = do
-  detached <- runToDetached prox dom
-  pure $ extract LE.extract $ toUI detached
-
-runToDetached :: forall t.
-  Traversable t
-  => Extractible t
-  => Proxy t
-  -> Document
-  -> Effect (Either QueryError (t DetachedNode))
-runToDetached _ dom = do
-  qRes <- doQuery LE.query dom
-  detach qRes
-
-doQuery ∷ ∀ b. QueryRunner' Document b → Document → Effect (Either QueryError b)
-doQuery query dom = runQuery $ query dom
-
-detach ∷ ∀ a t. Traversable t ⇒ Either a (t Node) → Effect (Either a (t DetachedNode))
-detach = case _ of
-  Left l -> pure $ Left l
-  Right q -> do
-    d <- traverse toDetached q
-    pure $ Right d
-
-toUI ∷ ∀ a t. Traversable t ⇒ Either a (t DetachedNode) → Either String (t UIElement)
-toUI = case _ of
-  Left _ -> Left "could not convert to UI"
-  Right d -> fromDetachedToUI d
-
-extract ∷ ∀ t a. (t UIElement → Either String a) → Either String (t UIElement) → Either String a
-extract parsePage = case _ of
-  Left l -> Left l
-  Right ui -> parsePage ui

+ 49 - 3
src/LinkedIn/Output.purs

@@ -1,18 +1,64 @@
-module LinkedIn.Output where
+module LinkedIn.Output (run, runToDetached, toOutput) where
 
 import Prelude
 
 import Data.Either (Either(..))
+import Data.Traversable (class Traversable, traverse)
 import Effect (Effect)
-import LinkedIn (run)
+import LinkedIn.DetachedNode (DetachedNode, toDetached)
+import LinkedIn.Extractible (class Extractible)
+import LinkedIn.Extractible as LE
 import LinkedIn.Output.Types (Output)
 import LinkedIn.Page.JobOffer (JobOfferPage)
 import LinkedIn.Page.Projects (ProjectsPage)
 import LinkedIn.Page.Skills (SkillsPage)
 import LinkedIn.Page.WorkExperiences (WorkExperiencesPage)
 import LinkedIn.PageUrl (PageUrl(..))
+import LinkedIn.QueryRunner (QueryError, QueryRunner', runQuery)
+import LinkedIn.UI.Elements.Parser (fromDetachedToUI)
+import LinkedIn.UI.Elements.Types (UIElement)
 import Type.Proxy (Proxy(..))
-import Web.DOM (Document)
+import Web.DOM (Document, Node)
+
+run :: forall t.
+  Traversable t
+  => Extractible t
+  => Proxy t
+  -> Document
+  -> Effect (Either String Output)
+run prox dom = do
+  detached <- runToDetached prox dom
+  pure $ extract LE.extract $ toUI detached
+
+runToDetached :: forall t.
+  Traversable t
+  => Extractible t
+  => Proxy t
+  -> Document
+  -> Effect (Either QueryError (t DetachedNode))
+runToDetached _ dom = do
+  qRes <- doQuery LE.query dom
+  detach qRes
+
+doQuery ∷ ∀ b. QueryRunner' Document b → Document → Effect (Either QueryError b)
+doQuery query dom = runQuery $ query dom
+
+detach ∷ ∀ a t. Traversable t ⇒ Either a (t Node) → Effect (Either a (t DetachedNode))
+detach = case _ of
+  Left l -> pure $ Left l
+  Right q -> do
+    d <- traverse toDetached q
+    pure $ Right d
+
+toUI ∷ ∀ a t. Traversable t ⇒ Either a (t DetachedNode) → Either String (t UIElement)
+toUI = case _ of
+  Left _ -> Left "could not convert to UI"
+  Right d -> fromDetachedToUI d
+
+extract ∷ ∀ t a. (t UIElement → Either String a) → Either String (t UIElement) → Either String a
+extract parsePage = case _ of
+  Left l -> Left l
+  Right ui -> parsePage ui
 
 toOutput ∷ PageUrl → (Document → Effect (Either String Output))
 toOutput = case _ of