From 32238ff298e8c381b4f8f9d9b9b1e11c2d3ccb1d Mon Sep 17 00:00:00 2001 From: Christian Mueller Date: Sat, 25 Jan 2014 21:37:20 +0100 Subject: [PATCH] params added to long urls as well --- API.md | 6 ++++++ src/NoteHub/api.clj | 28 ++++++++++++++++++---------- src/NoteHub/views/pages.clj | 10 ++-------- test/NoteHub/test/api.clj | 1 + test/NoteHub/test/views/pages.clj | 2 +- 5 files changed, 28 insertions(+), 19 deletions(-) diff --git a/API.md b/API.md index 397fc0b..3c069d0 100644 --- a/API.md +++ b/API.md @@ -1,3 +1,4 @@ + # NoteHub API **Version 1.2, status: released.** @@ -72,6 +73,9 @@ Parameter | Explanation | Type `signature` | Signature | **required** `password` | MD5 hash of a plain password for editing | *optional* `version` | Used API version | **required** +`theme` | Theme name | *optional* +`text-font` | Text font name | *optional* +`header-font`| Header font name | *optional* The Signature is the MD5 hash of the following string concatenation: @@ -79,6 +83,8 @@ The Signature is the MD5 hash of the following string concatenation: The signature serves as a proof, that the request is authentic and will be issued by the publisher corresponding to the provided PID. +The parameters specifying the theme name and fonts are optional and only impact the URLs returned back. + The response of the server will contain the fields `noteID`, `longURL`, `shortURL`, `status`. Example: diff --git a/src/NoteHub/api.clj b/src/NoteHub/api.clj index 9dfe557..e0d1f33 100644 --- a/src/NoteHub/api.clj +++ b/src/NoteHub/api.clj @@ -1,13 +1,15 @@ (ns NoteHub.api (:import - [java.util Calendar]) + [java.util Calendar]) (:use - [NoteHub.settings] - [clojure.string :rename {replace sreplace} - :only [replace blank? lower-case split-lines split]]) + [NoteHub.settings] + [ring.util.codec :only [url-encode]] + [clojure.string :rename {replace sreplace} + :only [replace blank? lower-case split-lines split]]) (:require - [ring.util.codec] - [NoteHub.storage :as storage])) + [ring.util.codec] + [hiccup.util :as util] + [NoteHub.storage :as storage])) (def version "1.1") @@ -23,6 +25,11 @@ (apply printf (str "%s:" string) (str (storage/get-current-date) ":LOG") args) (println)) +(defn url + "Creates a local url from the given substrings" + [& args] + (apply str (interpose "/" (cons "" (map url-encode args))))) + ; Concatenates all fields to a string (defn build-key "Returns a storage-key for the given note coordinates" @@ -51,7 +58,7 @@ (let [[year month day title] (split token #" ")] (if description (str domain "/" (storage/create-short-url token {:year year :month month :day day :title title})) - (apply str (interpose "/" [domain year month day (ring.util.codec/url-encode title)])))))) + (str domain (url year month day title)))))) (let [md5Instance (java.security.MessageDigest/getInstance "MD5")] (defn get-signature @@ -99,12 +106,13 @@ (map #(str proposed-title "-" (+ 2 %)) (range))))) noteID (build-key date title) new-params (assoc params :year year :month month :day day :title title) - short-url (storage/create-short-url noteID new-params)] + short-url (get-path (storage/create-short-url noteID new-params) :url) + long-url (get-path noteID)] (do (storage/add-note noteID note pid password) {:noteID noteID - :longURL (get-path noteID) - :shortURL (get-path short-url :url) + :longURL (if (empty? params) long-url (str (util/url long-url params))) + :shortURL short-url :status (create-response true)})) {:status (create-response false (first errors))})))) diff --git a/src/NoteHub/views/pages.clj b/src/NoteHub/views/pages.clj index c17f71e..afbd8b0 100644 --- a/src/NoteHub/views/pages.clj +++ b/src/NoteHub/views/pages.clj @@ -11,7 +11,6 @@ [noir.util.crypt :only [encrypt]] [hiccup.form] [hiccup.core] - [ring.util.codec :only [url-encode]] [hiccup.element] [hiccup.util :only [escape-html]] [hiccup.page :only [include-js html5]] @@ -54,11 +53,6 @@ [code] (status code (get-page code))) -(defn url - "Creates a local url from the given substrings" - [& args] - (apply str (interpose "/" (cons "" (map url-encode args))))) - ; input form for the markdown text with a preview area (defpartial input-form [form-url command fields content passwd-msg] (let [css-class (when (= :publish command) :hidden)] @@ -113,7 +107,7 @@ (md-node :article.bottom-space (:note note)) (let [links (map #(link-to (if (= :short-url %) - (url (storage/create-short-url noteID params)) + (api/url (storage/create-short-url noteID params)) (str (:longURL note) "/" (name %))) (get-message %)) [:stats :edit :export :short-url]) @@ -137,7 +131,7 @@ (when-let [params (storage/resolve-url short-url)] (let [{:keys [year month day title]} params rest-params (dissoc params :year :month :day :title) - core-url (url year month day title) + core-url (api/url year month day title) long-url (if (empty? rest-params) core-url (util/url core-url rest-params))] (redirect long-url)))) diff --git a/test/NoteHub/test/api.clj b/test/NoteHub/test/api.clj index 0b3fdb6..3b5bd04 100644 --- a/test/NoteHub/test/api.clj +++ b/test/NoteHub/test/api.clj @@ -129,6 +129,7 @@ (str "/" (last (clojure.string/split ((parse-string (:body response)) "shortURL") #"/"))))) "Location")] + (= url ((parse-string (:body response)) "longURL")) (substring? "theme=dark" url) (substring? "text-font=Felvetica" url)) (is (do diff --git a/test/NoteHub/test/views/pages.clj b/test/NoteHub/test/views/pages.clj index cf2cba4..39e6f17 100644 --- a/test/NoteHub/test/views/pages.clj +++ b/test/NoteHub/test/views/pages.clj @@ -1,6 +1,6 @@ (ns NoteHub.test.views.pages (:use [NoteHub.views.pages] - [NoteHub.api :only [build-key get-signature get-date]] + [NoteHub.api :only [build-key get-signature get-date url]] [noir.util.test] [NoteHub.storage] [clojure.test]))