Browse Source

title derivation improved and added to API

master
Christian Mueller 12 years ago
parent
commit
ad632f96ce
  1. 3
      API.md
  2. 11
      src/NoteHub/api.clj
  3. 2
      src/NoteHub/views/pages.clj
  4. 7
      test/NoteHub/test/api.clj

3
API.md

@ -26,12 +26,13 @@ Parameter | Explanation | Type
`noteID` | Note-ID | **required** `noteID` | Note-ID | **required**
`version` | Used API version | **required** `version` | Used API version | **required**
will return a JSON object containing following self explaining fields: `note`, `longURL`, `shortURL`, `statistics`, `status`. will return a JSON object containing following self explaining fields: `note`, `title`, `longURL`, `shortURL`, `statistics`, `status`, `publisher`.
Example: Example:
{ {
note: <markdown source>, note: <markdown source>,
title: "Lorem Ipsum.",
longURL: "http://notehub.org/2014/1/3/lorem-ipsum", longURL: "http://notehub.org/2014/1/3/lorem-ipsum",
shortURL: "http://notehub.org/0vrcp", shortURL: "http://notehub.org/0vrcp",
statistics: { statistics: {

11
src/NoteHub/api.clj

@ -25,6 +25,11 @@
[[year month day] title] [[year month day] title]
(print-str year month day title)) (print-str year month day title))
(defn derive-title [md-text]
(apply str
(remove #{\# \_ \*}
(first (split-lines md-text)))))
(defn get-date (defn get-date
"Returns today's date" "Returns today's date"
[] []
@ -54,12 +59,14 @@
(defn get-note [noteID] (defn get-note [noteID]
(if (storage/note-exists? noteID) (if (storage/note-exists? noteID)
{:note (storage/get-note noteID) (let [note (storage/get-note noteID)]
{:note note
:title (derive-title note)
:longURL (get-path noteID) :longURL (get-path noteID)
:shortURL (get-path noteID :short) :shortURL (get-path noteID :short)
:statistics (storage/get-note-statistics noteID) :statistics (storage/get-note-statistics noteID)
:status (create-response true) :status (create-response true)
:publisher (storage/get-publisher noteID)} :publisher (storage/get-publisher noteID)})
(create-response false "noteID '%s' unknown" noteID))) (create-response false "noteID '%s' unknown" noteID)))
(defn post-note (defn post-note

2
src/NoteHub/views/pages.clj

@ -73,7 +73,7 @@
; Converts given markdown to html and wraps with the main layout ; Converts given markdown to html and wraps with the main layout
(defn- wrap [short-url params md-text] (defn- wrap [short-url params md-text]
(when md-text (when md-text
(layout params (params :title) (layout params (api/derive-title md-text)
[:article.bottom-space.markdown md-text] [:article.bottom-space.markdown md-text]
(let [links (map #(link-to (let [links (map #(link-to
(if (= :short-url %) (if (= :short-url %)

7
test/NoteHub/test/api.clj

@ -6,11 +6,11 @@
[noir.util.test] [noir.util.test]
[clojure.test])) [clojure.test]))
(def note "hello world! This is a _test_ note!") (def note "hello world!\nThis is a _test_ note!")
(def pid "somePlugin") (def pid "somePlugin")
(def pid2 "somePlugin2") (def pid2 "somePlugin2")
(def note-title (str (apply print-str (get-date)) " hello-world-this-is-a-test-note")) (def note-title (str (apply print-str (get-date)) " hello-world"))
(def note-url (str (apply str domain "/" (interpose "/" (get-date))) "/hello-world-this-is-a-test-note")) (def note-url (str (apply str domain "/" (interpose "/" (get-date))) "/hello-world"))
(defn substring? [a b] (not (= nil (re-matches (re-pattern (str "(?s).*" a ".*")) b)))) (defn substring? [a b] (not (= nil (re-matches (re-pattern (str "(?s).*" a ".*")) b))))
(defmacro isnt [arg] `(is (not ~arg))) (defmacro isnt [arg] `(is (not ~arg)))
@ -46,6 +46,7 @@
(is (= (:longURL post-response) (:longURL get-response) note-url)) (is (= (:longURL post-response) (:longURL get-response) note-url))
(is (= (:shortURL post-response) (:shortURL get-response))) (is (= (:shortURL post-response) (:shortURL get-response)))
(is (= (:publisher get-response) pid)) (is (= (:publisher get-response) pid))
(is (= (:title get-response) (derive-title note)))
(is (= "1" (get-in get-response [:statistics :views]))) (is (= "1" (get-in get-response [:statistics :views])))
(isnt (get-in get-response [:statistics :edited])) (isnt (get-in get-response [:statistics :edited]))
(is (= "2" (get-in (get-note (:noteID post-response)) [:statistics :views]))))) (is (= "2" (get-in (get-note (:noteID post-response)) [:statistics :views])))))

Loading…
Cancel
Save