JobOffer.purs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. module LinkedIn.Jobs.JobOffer where
  2. import Prelude
  3. import Data.Argonaut.Encode (class EncodeJson)
  4. import Data.Argonaut.Encode.Generic (genericEncodeJson)
  5. import Data.Either (Either, note)
  6. import Data.Generic.Rep (class Generic)
  7. import Data.Lens (findOf)
  8. import Data.List (List(..), (:))
  9. import Data.List.Types (NonEmptyList(..))
  10. import Data.Maybe (Maybe(..), isJust)
  11. import Data.NonEmpty ((:|))
  12. import Data.Show.Generic (genericShow)
  13. import LinkedIn.UI.Basic.Types (JobFlexibility)
  14. import LinkedIn.UI.Components.JobsUnifiedTopCard (JobsUnifiedTopCardElement, TopCardInsight(..), TopCardInsightContent(..), _top_to_action_buttons, _top_to_insights, toHeader, toPrimaryDescriptionLink, toPrimaryDescriptionText)
  15. import LinkedIn.UI.Elements.Types (UIElement(..))
  16. import LinkedIn.UI.Strings.Types (UIString(..))
  17. type JobOfferObject = {
  18. title :: String,
  19. companyName :: String,
  20. companyLink :: String,
  21. location :: Maybe String,
  22. flexibility :: Maybe JobFlexibility,
  23. companyDomain :: Maybe String,
  24. companySize :: Maybe String,
  25. hasSimplifiedApplicationProcess :: Boolean
  26. }
  27. newtype JobOffer = JobOffer JobOfferObject
  28. derive instance Eq JobOffer
  29. derive instance Generic JobOffer _
  30. instance Show JobOffer where show = genericShow
  31. instance EncodeJson JobOffer where encodeJson a = genericEncodeJson a
  32. fromUI ∷ JobsUnifiedTopCardElement UIElement → Either String JobOffer
  33. fromUI card = ado
  34. title <- note "No title found" $ extractTitle header
  35. companyName <- note "No company found" $ extractCompany link
  36. companyLink <- note "No company link found" $ extractCompanyLink link
  37. in
  38. JobOffer {
  39. title,
  40. companyName,
  41. companyLink,
  42. location: extractLocation primaryDescText,
  43. flexibility: extractJobRemote =<< jobInsight,
  44. companyDomain: extractCompanyDomain =<< companyInsight,
  45. companySize: extractCompanySize =<< companyInsight,
  46. hasSimplifiedApplicationProcess: isJust $ extractSimplifiedApplicationProcess =<< applyButton
  47. }
  48. where
  49. header = toHeader card
  50. link = toPrimaryDescriptionLink card
  51. primaryDescText = toPrimaryDescriptionText card
  52. jobInsight = getInsight "job" card
  53. companyInsight = getInsight "company" card
  54. applyButton = findOf _top_to_action_buttons isApply card
  55. where
  56. isApply = case _ of
  57. UIButton {mainClass: Just main} -> main == "jobs-apply-button"
  58. _ -> false
  59. getInsight ∷ String → JobsUnifiedTopCardElement UIElement → Maybe (TopCardInsight UIElement)
  60. getInsight iType card = findOf _top_to_insights (isIcon iType) card
  61. where
  62. isIcon icon = case _ of
  63. TopCardInsight {icon: UIIcon i} -> i == icon
  64. _ -> false
  65. extractTitle :: UIElement -> Maybe String
  66. extractTitle = case _ of
  67. UIElement (UIStringPlain str) -> Just str
  68. _ -> Nothing
  69. extractCompany :: UIElement -> Maybe String
  70. extractCompany = case _ of
  71. UILink _ (UIStringPlain str) -> Just str
  72. _ -> Nothing
  73. extractCompanyLink :: UIElement -> Maybe String
  74. extractCompanyLink = case _ of
  75. UILink link _ -> Just link
  76. _ -> Nothing
  77. extractCompanyDomain ∷ TopCardInsight UIElement → Maybe String
  78. extractCompanyDomain = case _ of
  79. TopCardInsight {content: TopCardInsightContentSingle (UIElement (UIStringDotSeparated (NonEmptyList (_ :| (UIStringPlain str) : _))))} -> Just str
  80. _ -> Nothing
  81. extractCompanySize ∷ TopCardInsight UIElement → Maybe String
  82. extractCompanySize = case _ of
  83. TopCardInsight {content: TopCardInsightContentSingle (UIElement (UIStringDotSeparated (NonEmptyList(UIStringPlain str :| _))))} -> Just str
  84. _ -> Nothing
  85. extractLocation :: UIElement -> Maybe String
  86. extractLocation = case _ of
  87. UIElement (UIStringDotSeparated (NonEmptyList ((UIStringPlain str) :| Nil))) -> Just str
  88. _ -> Nothing
  89. extractJobRemote :: TopCardInsight UIElement -> Maybe JobFlexibility
  90. extractJobRemote = case _ of
  91. TopCardInsight {content: TopCardInsightContentSecondary {primary: (UIElement (UIStringJobFlex flex))}} -> Just flex
  92. _ -> Nothing
  93. extractSimplifiedApplicationProcess ∷ UIElement → Maybe Boolean
  94. extractSimplifiedApplicationProcess = case _ of
  95. UIButton {role: Nothing} -> Just true
  96. _ -> Nothing