packages feed

scotty-cookie (empty) → 0.1.0.0

raw patch · 4 files changed

+166/−0 lines, 4 filesdep +basedep +blaze-builderdep +bytestringsetup-changed

Dependencies added: base, blaze-builder, bytestring, containers, cookie, scotty, text, time, transformers

Files

+ LICENSE view
@@ -0,0 +1,30 @@+Copyright (c) 2014, Mārtiņš Mačs++All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++    * Redistributions of source code must retain the above copyright+      notice, this list of conditions and the following disclaimer.++    * Redistributions in binary form must reproduce the above+      copyright notice, this list of conditions and the following+      disclaimer in the documentation and/or other materials provided+      with the distribution.++    * Neither the name of Mārtiņš Mačs nor the names of other+      contributors may be used to endorse or promote products derived+      from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ scotty-cookie.cabal view
@@ -0,0 +1,32 @@+name:                scotty-cookie+version:             0.1.0.0+synopsis:            Cookie management helper functions for Scotty framework+description:         Cookie management helper functions for Scotty framework+homepage:            https://bitbucket.org/wniare/scotty-cookie+license:             BSD3+license-file:        LICENSE+author:              Mārtiņš Mačs+maintainer:          macs.martins@gmail.com+category:            Web+build-type:          Simple+cabal-version:       >=1.10++library+  exposed-modules:     Web.Scotty.Cookie+  hs-source-dirs:      src+  GHC-options:         -Wall -fwarn-tabs+  default-language:    Haskell2010++  build-depends:       base             ==4.*+                     , bytestring       >=0.10+                     , containers       >=0.4+                     , transformers     >=0.4+                     , text             >=0.11+                     , time             >=0.4+                     , blaze-builder    >=0.3+                     , scotty           >=0.7+                     , cookie           >=0.3++source-repository head+  Type: git+  Location: https://bitbucket.org/wniare/scotty-cookie
+ src/Web/Scotty/Cookie.hs view
@@ -0,0 +1,102 @@+{-|++Usage example: simple hit counter++@+\{\-\# LANGUAGE OverloadedStrings \#\-\}++import Control.Monad+import Data.Monoid+import Data.Maybe+import Data.Text.Read+import qualified Data.Text.Lazy as TL++import Web.Scotty+import Web.Scotty.Cookie++main :: IO ()+main = scotty 3000 $+    get "/" $ do+        hits <- liftM (fromMaybe "0") $ getCookie "hits"++        let hits' = case decimal hits of+                        Right n -> TL.pack . show . (+1) $ (fst n :: Integer)+                        Left _  -> "1"++        setSimpleCookie "hits" $ TL.toStrict hits'++        html $ mconcat [ "\<html\>\<body\>"+                       , hits'+                       , "\<\/body\>\<\/html\>"+                       ]+@++-}+{-# LANGUAGE OverloadedStrings #-}+module Web.Scotty.Cookie+    ( makeSimpleCookie+    , setCookie+    , setSimpleCookie+    , getCookie+    , getCookies+    , deleteCookie+    ) where++import Control.Monad ( liftM )++import qualified Data.Text as TS+import qualified Data.Text.Encoding as TS+import qualified Data.Text.Lazy.Encoding as TL++import qualified Data.Map as Map++import qualified Data.ByteString.Lazy as BSL++import Data.Time.Clock.POSIX ( posixSecondsToUTCTime )++import Blaze.ByteString.Builder ( toLazyByteString )++import Web.Scotty.Trans+import Web.Cookie+++makeSimpleCookie :: TS.Text -- ^ name+                 -> TS.Text -- ^ value+                 -> SetCookie+makeSimpleCookie n v = def { setCookieName  = TS.encodeUtf8 n+                           , setCookieValue = TS.encodeUtf8 v+                           }+++setCookie :: (Monad m, ScottyError e)+          => SetCookie+          -> ActionT e m ()+setCookie c = setHeader "Set-Cookie" (TL.decodeUtf8 . toLazyByteString $ renderSetCookie c)+++-- | 'makeSimpleCookie' and 'setCookie' combined.+setSimpleCookie :: (Monad m, ScottyError e)+                => TS.Text -- ^ name+                -> TS.Text -- ^ value+                -> ActionT e m ()+setSimpleCookie n v = setCookie $ makeSimpleCookie n v+++getCookie :: (Monad m, ScottyError e)+          => TS.Text -- ^ name+          -> ActionT e m (Maybe TS.Text)+getCookie c = liftM (Map.lookup c) getCookies+++-- | Returns all cookies+getCookies :: (Monad m, ScottyError e)+           => ActionT e m (Map.Map TS.Text TS.Text)+getCookies = liftM (Map.fromList . maybe [] parse) $ header "Cookie"+    where parse = parseCookiesText . BSL.toStrict . TL.encodeUtf8+++deleteCookie :: (Monad m, ScottyError e)+             => TS.Text -- ^ name+             -> ActionT e m ()+deleteCookie c = setCookie $ (makeSimpleCookie c "") { setCookieExpires = Just $ posixSecondsToUTCTime 0 }+