Skip to content
Browse files

First commit. Whole API implemented. Quarterback sacked.

  • Loading branch information...
0 parents commit a82081ea30b89c5c9afb85429362320b5c4568e3 @Raynes committed Jan 5, 2012
Showing with 101 additions and 0 deletions.
  1. +5 −0 .gitignore
  2. +28 −0 README.md
  3. +5 −0 project.clj
  4. +56 −0 src/innuendo/core.clj
  5. +7 −0 test/innuendo/core_test.clj
5 .gitignore
@@ -0,0 +1,5 @@
+pom.xml
+*jar
+/lib/
+/classes/
+.lein-deps-sum
28 README.md
@@ -0,0 +1,28 @@
+# innuendo
+
+This is a little API library for the [RefHeap](http://refheap.com/api) pasting API. It implements the entire API.
+
+## Usage
+
+In Leiningen: `:dependencies [[innuendo "0.1.0"]]`
+
+Here are some examples:
+
+```clojure
+user=> (use 'innuendo.core)
+nil
+user=> (get-paste 21)
+{:lines 1, :date "2012-01-05T14:40:25.878Z", :paste-id "21", :language "Clojure", :private false, :url "http://refheap.com/paste/21", :user nil, :contents "test"}
+user=> (create-paste "(println \"test\")" {:username "Raynes" :token "REDACTED" :private true :language "Clojure"})
+{:lines 1, :date "2012-01-05T20:55:21.440Z", :paste-id "4f060e39535d6a2321a5aec1", :language "Clojure", :private true, :url "http://refheap.com/paste/4f060e39535d6a2321a5aec1", :user "raynes", :contents "(println \"test\")"}
+user=> (edit-paste "4f060e39535d6a2321a5aec1" {:username "Raynes" :token "REDACTED" :contents "(println \"edited\")" :private false})
+{:lines 1, :date "2012-01-05T20:55:21.440Z", :paste-id "22", :language "Clojure", :private false, :url "http://refheap.com/paste/22", :user "raynes", :contents "(println \"edited\")"}
+user=> (create-paste "(println \"unauthenticated\")" {:private true :language "Clojure"})
+{:lines 1, :date "2012-01-05T20:57:05.519Z", :paste-id "4f060ea1535d6a2321a5aec2", :language "Clojure", :private true, :url "http://refheap.com/paste/4f060ea1535d6a2321a5aec2", :user nil, :contents "(println \"unauthenticated\")"}
+user=> (delete-paste 22 {:username "Raynes" :token "REDACTED"})
+true
+```
+
+## License
+
+Distributed under the Eclipse Public License, the same as Clojure.
5 project.clj
@@ -0,0 +1,5 @@
+(defproject innuendo "0.1.0"
+ :description "A simple API library for refheap.com"
+ :dependencies [[clojure "1.3.0"]
+ [clj-http "0.2.6"]
+ [cheshire "2.0.4"]])
56 src/innuendo/core.clj
@@ -0,0 +1,56 @@
+(ns innuendo.core
+ (:require [clj-http.client :as http]
+ [cheshire.core :as json]))
+
+(defn process-response [{:keys [status] :as resp}]
+ (cond
+ (#{404 400 422} status) (assoc resp :body (json/parse-string (:body resp) true))
+ (#{201 200} status) (json/parse-string (:body resp) true)
+ (= 204 status) true
+ :else resp))
+
+(defn req [method endpoint positional optional]
+ (process-response
+ (http/request
+ (merge {:method method
+ :url (str "http://refheap.com/api/"
+ (apply format endpoint positional))}
+ {(if (= method :post) :form-params :query-params) optional}))))
+
+(defn create-paste
+ "Create a new paste.
+ Options are:
+ username -- Optional. If this and a token is not present, paste will be anonymous.
+ token -- Optional.
+ contents -- Required. Cannot be empty.
+ private -- Optional. true or false. Default is false.
+ language -- Optional. Must be in exactly the same format as it appears in the dropdown
+ on http://refheap.com/paste. If not provided, will default to Plain Text."
+ [contents & [options]]
+ (req :post "paste" [] (assoc options :contents contents)))
+
+(defn edit-paste
+ "Edit an existing paste. If any parameters listed as optional are not provided, they will default
+ to what the old paste had.
+ Options are:
+ username -- Required.
+ token -- Required.
+ contents -- Optional. Cannot be empty.
+ private -- Optional. true or false.
+ language -- Optional. Must be in exactly the same format as it appears in the dropdown
+ on http://refheap.com/paste."
+ [id options]
+ (req :post "paste/%s" [id] options))
+
+(defn get-paste
+ "Get an existing paste."
+ [id]
+ (req :get "paste/%s" [id] nil))
+
+(defn delete-paste
+ "Delete a paste.
+ Options are:
+ username -- Required.
+ token -- Required."
+ [id options]
+ (req :delete "paste/%s" [id] options))
7 test/innuendo/core_test.clj
@@ -0,0 +1,7 @@
+(ns innuendo.core-test
+ (:use clojure.test
+ innuendo.core))
+
+(deftest a-test
+ (testing "FIXME, I fail."
+ (is (= 0 1))))

0 comments on commit a82081e

Please sign in to comment.
Something went wrong with that request. Please try again.