Преглед на файлове

Add a missing case for buttons

jherve преди 1 година
родител
ревизия
ed8977bd7f
променени са 2 файла, в които са добавени 40 реда и са изтрити 8 реда
  1. 11 2
      src/LinkedIn/JobsUnifiedTopCard.purs
  2. 29 6
      src/LinkedIn/QueryRunner.purs

+ 11 - 2
src/LinkedIn/JobsUnifiedTopCard.purs

@@ -13,10 +13,11 @@ import Data.Maybe (Maybe(..))
 import Data.Show.Generic (genericShow)
 import Data.Traversable (class Traversable, sequence, traverse, traverseDefault)
 import LinkedIn (DetachedNode(..))
-import LinkedIn.QueryRunner (QueryError(..), QueryRunner, chooseOne, ignoreNotFound, queryAll, queryOne, queryText)
+import LinkedIn.QueryRunner (QueryError(..), QueryRunner, chooseOne, chooseOne3, ignoreNotFound, queryAll, queryOne, queryText)
 import LinkedIn.Types (ParseError(..), Parser)
 import LinkedIn.Utils (detachNonEmptyTextChild, parseDetachedNode, queryAndDetachMany, queryAndDetachOne, queryManyAndParse, queryOneAndParse)
 import Web.DOM (Node)
+import Web.DOM.Node as N
 
 data JobsUnifiedTopCardElement a = JobsUnifiedTopCardElement {
   header :: a,
@@ -233,6 +234,14 @@ parseTopCardInsightContent n = do
 queryTopCardInsightContentSingle :: QueryRunner (TopCardInsightContent Node)
 queryTopCardInsightContentSingle n = pure $ TopCardInsightContentSingle n
 
+queryTopCardInsightContentButton :: QueryRunner (TopCardInsightContent Node)
+queryTopCardInsightContentButton n =
+  if type_ == "BUTTON"
+  then pure $ TopCardInsightContentButton n
+  else throwError (QNodeUnexpectedType "BUTTON" type_)
+
+  where type_ = N.nodeName n
+
 queryTopCardInsightContentSecondary :: QueryRunner (TopCardInsightContent Node)
 queryTopCardInsightContentSecondary n = do
   primary <- queryOne ":scope > span:first-child span[aria-hidden=true]" n
@@ -242,7 +251,7 @@ queryTopCardInsightContentSecondary n = do
 
 queryTopCardInsightContent :: QueryRunner (TopCardInsightContent Node)
 queryTopCardInsightContent n =
-  chooseOne queryTopCardInsightContentSecondary queryTopCardInsightContentSingle n
+  chooseOne3 queryTopCardInsightContentSecondary queryTopCardInsightContentButton queryTopCardInsightContentSingle n
 
 parseTopCardInsight :: Parser (TopCardInsight DetachedNode)
 parseTopCardInsight n = do

+ 29 - 6
src/LinkedIn/QueryRunner.purs

@@ -2,6 +2,7 @@ module LinkedIn.QueryRunner where
 
 import Prelude
 
+import Control.Alt ((<|>))
 import Control.Monad.Except (ExceptT(..), mapExceptT, runExceptT, throwError)
 import Data.Array as A
 import Data.Either (Either(..), note)
@@ -19,6 +20,7 @@ import Web.DOM.NodeList as NL
 data QueryError =
   QNodeNotFoundError String
   | QNodeListNotFoundError String
+  | QNodeUnexpectedType String String
   | QTextNotFoundError
   | QChooseError
 
@@ -40,6 +42,13 @@ ignoreNotFound = mapExceptT (map ignoreNotFound')
       (Left q) -> Left q
       (Right n') -> Right (Just n')
 
+ignoreErrors ∷ ∀ a f. Functor f ⇒ ExceptT QueryError f a → ExceptT QueryError f (Maybe a)
+ignoreErrors = mapExceptT (map ignoreErrors')
+  where
+    ignoreErrors' = case _ of
+      (Left q) -> Right Nothing
+      (Right n') -> Right (Just n')
+
 queryOne ∷ String → QueryRunner Node
 queryOne selector node = ExceptT $ do
   maybeNode <- U.queryOne selector node
@@ -66,10 +75,24 @@ subQueryOne query selector n = query =<< queryOne selector n
 
 chooseOne ∷ ∀ a t m. Monad m ⇒ (t → ExceptT QueryError m a) → (t → ExceptT QueryError m a) → (t → ExceptT QueryError m a)
 chooseOne q1 q2 n = do
-  maybeN1 <- ignoreNotFound $ q1 n
-  maybeN2 <- ignoreNotFound $ q2 n
+  maybeN1 <- (ignoreErrors <<< q1) n
+  maybeN2 <- (ignoreErrors <<< q2) n
+
+  case maybeN1 <|> maybeN2 of
+    Nothing -> throwError QChooseError
+    Just n' -> pure n'
+
+chooseOne3 ∷ ∀ a t m.
+  Monad m
+  ⇒ (t → ExceptT QueryError m a)
+  → (t → ExceptT QueryError m a)
+  → (t → ExceptT QueryError m a)
+  → (t → ExceptT QueryError m a)
+chooseOne3 q1 q2 q3 n = do
+  maybeN1 <- (ignoreErrors <<< q1) n
+  maybeN2 <- (ignoreErrors <<< q2) n
+  maybeN3 <- (ignoreErrors <<< q3) n
 
-  case maybeN1, maybeN2 of
-    Nothing, Nothing -> throwError QChooseError
-    Just n1, _ -> pure n1
-    _, Just n2 -> pure n2
+  case maybeN1 <|> maybeN2 <|> maybeN3 of
+    Nothing -> throwError QChooseError
+    Just n' -> pure n'