Skip to content

Instantly share code, notes, and snippets.

@sw-samuraj
sw-samuraj / blog-currying-2.clj
Created April 4, 2017 09:14
An example of currying in Clojure with more parameters at once, for a blog post.
(defn spicy [first second third]
(println "I like food with" first "," second "and" third "."))
(def curry-and-chilli
(partial spicy "curry" "curry" "chilli"))
(curry-and-chilli)
;; -> I like food with curry , curry and chilli .
@sw-samuraj
sw-samuraj / blog-ring-handler-plain.clj
Last active May 1, 2017 19:25
An example of plain Ring handler (for a blog post).
(ns blog-ring.core
(:require [ring.adapter.jetty :as jetty]))
(defn handler [request]
{:status 200
:headers {"Content-Type" "text/html"}
:body "<h1>Hello, world!</h1>"})
(defn -main []
(jetty/run-jetty handler
@sw-samuraj
sw-samuraj / blog-ring-lein.clj
Created April 11, 2017 21:08
An example of the Leiningen project for a simple Ring handler (for a blog post). Raw
(defproject blog-ring "0.1.0-SNAPSHOT"
:dependencies [[org.clojure/clojure "1.8.0"]
[ring "1.6.0-RC2"]]
:main blog-ring.core)
@sw-samuraj
sw-samuraj / blog-ring-response.clj
Created April 29, 2017 16:41
An example of Ring responses (for a blog post).
(ns blog-ring.response
(:require [ring.util.response :as res]))
(def simple-res (res/response "Hello, world!"))
simple-res
;; -> {:status 200,
;; :headers {},
;; :body "Hello, world!"}
@sw-samuraj
sw-samuraj / blog-ring-request.clj
Created April 29, 2017 20:29
An example of Ring request (for a blog post).
(defn handler [request]
{:status 200
:headers {"Content-Type" "text/html"}
:body (str "<h1>One Ring rules them all!</h1>"
"<ul><li>Request from IP: "
(:remote-addr request)
"</li><li>Request method: "
(:request-method request)
"</li><li>Headers: "
(select-keys
@sw-samuraj
sw-samuraj / blog-ring-wrap-params.clj
Created May 1, 2017 06:43
An example of Ring middlewares wrap-params and wrap-keyword-params (for a blog post).
(ns blog-ring.middleware
(:require [ring.middleware.params :refer [wrap-params]]
[ring.middleware.keyword-params :refer [wrap-keyword-params]]))
((wrap-params identity)
{:query-string "clojure=yes&lisp=maybe"})
;; -> {:query-string "clojure=yes&lisp=maybe",
;; :form-params {},
;; :params {"clojure" "yes", "lisp" "maybe"},
;; :query-params {"clojure" "yes", "lisp" "maybe"}}
@sw-samuraj
sw-samuraj / blog-ring-middleware-threading.clj
Last active May 1, 2017 11:15
An example of Ring middleware threading (for a blog post).
(def app
(-> handler
(wrap-keyword-params)
(wrap-params)))
@sw-samuraj
sw-samuraj / blog-ring-app.clj
Last active May 1, 2017 19:33
An example of the simple Ring application (for a blog post).
(ns blog-ring.core
(:require [ring.adapter.jetty :as jetty]
[ring.util.response :refer [response]]
[ring.middleware.params :refer [wrap-params]]
[ring.middleware.keyword-params :refer [wrap-keyword-params]]))
(defn handler [request]
(response
(str "<h1>One Ring rules them all!</h1>"
"<ul><li>Query string: "
@sw-samuraj
sw-samuraj / ultimate-question-spec.yaml
Created May 8, 2017 17:33
A Swagger specification for a blog post about REST contract-first approach.
swagger: "2.0"
info:
version: "0.1.0"
title: "The Ultimate Question."
paths:
/question:
get:
summary: "Asks the ultimate question"
@sw-samuraj
sw-samuraj / blog-swagger-gradle-build.gradle
Last active May 8, 2017 19:41
Partial Gradle build script for a blog post about REST contract-first approach.
plugins {
id 'org.hidetake.swagger.generator' version '2.4.2'
}
repositories {
mavenCentral()
}
dependencies {
compile 'io.swagger:swagger-annotations:1.5.10'