Browse Source

params added to long urls as well

master
Christian Mueller 12 years ago
parent
commit
32238ff298
  1. 6
      API.md
  2. 16
      src/NoteHub/api.clj
  3. 10
      src/NoteHub/views/pages.clj
  4. 1
      test/NoteHub/test/api.clj
  5. 2
      test/NoteHub/test/views/pages.clj

6
API.md

@ -1,3 +1,4 @@
# NoteHub API # NoteHub API
**Version 1.2, status: released.** **Version 1.2, status: released.**
@ -72,6 +73,9 @@ Parameter | Explanation | Type
`signature` | Signature | **required** `signature` | Signature | **required**
`password` | MD5 hash of a plain password for editing | *optional* `password` | MD5 hash of a plain password for editing | *optional*
`version` | Used API version | **required** `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: 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 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`. The response of the server will contain the fields `noteID`, `longURL`, `shortURL`, `status`.
Example: Example:

16
src/NoteHub/api.clj

@ -3,10 +3,12 @@
[java.util Calendar]) [java.util Calendar])
(:use (:use
[NoteHub.settings] [NoteHub.settings]
[ring.util.codec :only [url-encode]]
[clojure.string :rename {replace sreplace} [clojure.string :rename {replace sreplace}
:only [replace blank? lower-case split-lines split]]) :only [replace blank? lower-case split-lines split]])
(:require (:require
[ring.util.codec] [ring.util.codec]
[hiccup.util :as util]
[NoteHub.storage :as storage])) [NoteHub.storage :as storage]))
(def version "1.1") (def version "1.1")
@ -23,6 +25,11 @@
(apply printf (str "%s:" string) (str (storage/get-current-date) ":LOG") args) (apply printf (str "%s:" string) (str (storage/get-current-date) ":LOG") args)
(println)) (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 ; Concatenates all fields to a string
(defn build-key (defn build-key
"Returns a storage-key for the given note coordinates" "Returns a storage-key for the given note coordinates"
@ -51,7 +58,7 @@
(let [[year month day title] (split token #" ")] (let [[year month day title] (split token #" ")]
(if description (if description
(str domain "/" (storage/create-short-url token {:year year :month month :day day :title title})) (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")] (let [md5Instance (java.security.MessageDigest/getInstance "MD5")]
(defn get-signature (defn get-signature
@ -99,12 +106,13 @@
(map #(str proposed-title "-" (+ 2 %)) (range))))) (map #(str proposed-title "-" (+ 2 %)) (range)))))
noteID (build-key date title) noteID (build-key date title)
new-params (assoc params :year year :month month :day day :title 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 (do
(storage/add-note noteID note pid password) (storage/add-note noteID note pid password)
{:noteID noteID {:noteID noteID
:longURL (get-path noteID) :longURL (if (empty? params) long-url (str (util/url long-url params)))
:shortURL (get-path short-url :url) :shortURL short-url
:status (create-response true)})) :status (create-response true)}))
{:status (create-response false (first errors))})))) {:status (create-response false (first errors))}))))

10
src/NoteHub/views/pages.clj

@ -11,7 +11,6 @@
[noir.util.crypt :only [encrypt]] [noir.util.crypt :only [encrypt]]
[hiccup.form] [hiccup.form]
[hiccup.core] [hiccup.core]
[ring.util.codec :only [url-encode]]
[hiccup.element] [hiccup.element]
[hiccup.util :only [escape-html]] [hiccup.util :only [escape-html]]
[hiccup.page :only [include-js html5]] [hiccup.page :only [include-js html5]]
@ -54,11 +53,6 @@
[code] [code]
(status code (get-page 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 ; input form for the markdown text with a preview area
(defpartial input-form [form-url command fields content passwd-msg] (defpartial input-form [form-url command fields content passwd-msg]
(let [css-class (when (= :publish command) :hidden)] (let [css-class (when (= :publish command) :hidden)]
@ -113,7 +107,7 @@
(md-node :article.bottom-space (:note note)) (md-node :article.bottom-space (:note note))
(let [links (map #(link-to (let [links (map #(link-to
(if (= :short-url %) (if (= :short-url %)
(url (storage/create-short-url noteID params)) (api/url (storage/create-short-url noteID params))
(str (:longURL note) "/" (name %))) (str (:longURL note) "/" (name %)))
(get-message %)) (get-message %))
[:stats :edit :export :short-url]) [:stats :edit :export :short-url])
@ -137,7 +131,7 @@
(when-let [params (storage/resolve-url short-url)] (when-let [params (storage/resolve-url short-url)]
(let [{:keys [year month day title]} params (let [{:keys [year month day title]} params
rest-params (dissoc params :year :month :day :title) 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))] long-url (if (empty? rest-params) core-url (util/url core-url rest-params))]
(redirect long-url)))) (redirect long-url))))

1
test/NoteHub/test/api.clj

@ -129,6 +129,7 @@
(str "/" (str "/"
(last (clojure.string/split (last (clojure.string/split
((parse-string (:body response)) "shortURL") #"/"))))) "Location")] ((parse-string (:body response)) "shortURL") #"/"))))) "Location")]
(= url ((parse-string (:body response)) "longURL"))
(substring? "theme=dark" url) (substring? "theme=dark" url)
(substring? "text-font=Felvetica" url)) (substring? "text-font=Felvetica" url))
(is (do (is (do

2
test/NoteHub/test/views/pages.clj

@ -1,6 +1,6 @@
(ns NoteHub.test.views.pages (ns NoteHub.test.views.pages
(:use [NoteHub.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] [noir.util.test]
[NoteHub.storage] [NoteHub.storage]
[clojure.test])) [clojure.test]))

Loading…
Cancel
Save