Skip to content

Instantly share code, notes, and snippets.

@jbarber
Last active August 29, 2015 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jbarber/143351e2db1bebb4c910 to your computer and use it in GitHub Desktop.
Save jbarber/143351e2db1bebb4c910 to your computer and use it in GitHub Desktop.
Example of liberator resource with post redirect and setting flash
(ns user
(:require [liberator core representation]
[ring.middleware params session flash]
[ring.mock.request]))
;; Return HTTP 303 - have to manage Location ourselves
((->
(liberator.core/resource
:available-media-types ["text/html" "application/x-www-form-urlencoded"]
:allowed-methods [:get :post]
:post! (fn [ctx] ["true/false from post is ignored (context isn't)" {:post-output ::bar}])
:handle-ok (fn [ctx] (str "get handled" (get-in ctx [:request :flash])))
:post-redirect? {:location "/"}
:handle-see-other (fn [ctx]
(liberator.representation/ring-response
{:flash ::submitted
:headers {"Location" (ctx :location)}})))
ring.middleware.params/wrap-params
ring.middleware.session/wrap-session
ring.middleware.flash/wrap-flash
; (liberator.dev/wrap-trace :header :ui)
)
(ring.mock.request/request :post "/any" {:email "foo@example.com"}))
;; Returns 201 - Location managed by liberator
((->
(liberator.core/resource
:available-media-types ["text/html" "application/x-www-form-urlencoded"]
:allowed-methods [:get :post]
:post! (fn [ctx] ["true/false from post is ignored (context isn't)" {:post-output ::bar}])
:handle-ok (fn [ctx] (str "get handled" (get-in ctx [:request :flash])))
:post-redirect? [false {:location "/"}]
:handle-created (fn [ctx]
(liberator.representation/ring-response
{:flash ::submitted})))
ring.middleware.params/wrap-params
ring.middleware.session/wrap-session
ring.middleware.flash/wrap-flash)
(ring.mock.request/request :post "/any" {:email "foo@example.com"}))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment