{- Fetch Repository Events json.
   And parse to pandoc Blocks -}
fetch :: Maybe BasicAuth -> String -> IO Blocks
fetch mauth url = do
  (opts, _) <- getOptsArgs (makeOptions options) [] []
  let filterDate = (lookup "date" opts >>= parseTime defaultTimeLocale "%F") :: Maybe Day
      filterAuthor = liftM pack $ lookup "author" opts
  request <- parseUrlWithAuth (toEventsApiUrl url) mauth
  withManager $ \manager -> do
    Response _ _ _ src <- httpLbs request manager
    let objects = (fromJust (decode src :: Maybe Array))
        events = DV.map (DAT.parseMaybe parseJSON) objects :: DV.Vector (Maybe Event)
        bs = DV.map (\e -> toBlock (fromJust e) filterAuthor) $ DV.filter (\e -> isJust e && e `compareDate` filterDate) events
        bs' = header 2 (str url) <> DV.foldl1 (\acc b -> acc <> b) bs
    return bs'
  where
    compareDate Nothing _ = False
    compareDate (Just event) mDate = (localDay $ utcToJstTime $ createdAt event) `justEql` mDate
    parseUrlWithAuth u Nothing = parseUrl u
    parseUrlWithAuth u (Just (BasicAuth l p)) = applyBasicAuth (BC.pack l) (BC.pack p) <$> parseUrl u

{- compare Only Maybe is not Nothing, otherwise always return True -}
fetch :: Maybe BasicAuth -> String -> IO Blocks
fetch mauth url = do
  (opts, _) <- getOptsArgs (makeOptions options) [] []
  let filterDate = (lookup "date" opts >>= parseTime defaultTimeLocale "%F") :: Maybe Day
      filterAuthor = liftM pack $ lookup "author" opts
  request <- parseUrlWithAuth (toEventsApiUrl url) mauth
  withManager $ \manager -> do
    Response _ _ _ src <- httpLbs request manager
    let objects = (fromJust (decode src :: Maybe Array))
        events = DV.map (DAT.parseMaybe parseJSON) objects :: DV.Vector (Maybe Event)
        bs = DV.map (\e -> toBlock (fromJust e) filterAuthor) $ DV.filter (\e -> isJust e && e `compareDate` filterDate) events
        bs' = header 2 (str url) <> DV.foldl1 (\acc b -> acc <> b) bs
    return bs'
  where
    compareDate Nothing _ = False
    compareDate (Just event) mDate = (localDay $ utcToJstTime $ createdAt event) `justEql` mDate
    parseUrlWithAuth u Nothing = parseUrl u
    parseUrlWithAuth u (Just (BasicAuth l p)) = applyBasicAuth (BC.pack l) (BC.pack p) <$> parseUrl u

{- compare Only Maybe is not Nothing, otherwise always return True -}
fetch mauth url = do
  (opts, _) <- getOptsArgs (makeOptions options) [] []
  let filterDate = (lookup "date" opts >>= parseTime defaultTimeLocale "%F") :: Maybe Day
      filterAuthor = liftM pack $ lookup "author" opts
  request <- parseUrlWithAuth (toEventsApiUrl url) mauth
  withManager $ \manager -> do
    Response _ _ _ src <- httpLbs request manager
    let objects = (fromJust (decode src :: Maybe Array))
        events = DV.map (DAT.parseMaybe parseJSON) objects :: DV.Vector (Maybe Event)
        bs = DV.map (\e -> toBlock (fromJust e) filterAuthor) $ DV.filter (\e -> isJust e && e `compareDate` filterDate) events
        bs' = header 2 (str url) <> DV.foldl1 (\acc b -> acc <> b) bs
    return bs'
  where
    compareDate Nothing _ = False
    compareDate (Just event) mDate = (localDay $ utcToJstTime $ createdAt event) `justEql` mDate
    parseUrlWithAuth u Nothing = parseUrl u
    parseUrlWithAuth u (Just (BasicAuth l p)) = applyBasicAuth (BC.pack l) (BC.pack p) <$> parseUrl u

{- compare Only Maybe is not Nothing, otherwise always return True -}