Skip to content

Commit

Permalink
super basic direct answer result seems to work
Browse files Browse the repository at this point in the history
  • Loading branch information
joshrotenberg committed Dec 2, 2011
1 parent dcd856d commit e0e3b64
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 17 deletions.
31 changes: 31 additions & 0 deletions src/socrates/api/direct_answer.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
(ns socrates.api.direct-answer
(:use socrates.core
socrates.util)
(:require [clojure.xml :as xml]
[clojure.zip :as zip]
[clojure.contrib.zip-filter.xml :as zf])
(:import java.io.ByteArrayInputStream))

(defn da-parse
[body]
(let [zipped (-> (ByteArrayInputStream. body)
(xml/parse)
(zip/xml-zip))
tk-top (zf/xml1-> zipped)
tk-answered (zf/attr tk-top :answered)
tk-text-result (zf/xml1-> zipped :tk:text_result zf/text)]
{:answered (if (= "true" tk-answered)
true
false)
:result tk-text-result}))

(defn direct-answer
[& args]
(let [request-uri (str "http://" *api-url* "/direct_answer/" *api-version*)
split-args (split-positional-args args)
question (apply str (first split-args))
query-params (second split-args)
auth {:api_account_id *api-account-id* :api_password *api-password*}
request (prepare-request request-uri question query-params auth)]
(da-parse (execute-request request))))

18 changes: 4 additions & 14 deletions src/socrates/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
(def ^:dynamic *api-version* "stable")
(def user-agent (str "socrates/" *client-version*))


(defmacro with-credentials
"Use the True Knowledge API Account ID and Password."
[id password & body]
Expand All @@ -30,26 +29,17 @@
(status-is-client-error status) (throw (Exception. "Client error"))
(status-is-server-error status) (throw (Exception. "Server error"))
:else
body)))
(when-not (empty? body)
body))))

(defn prepare-request
"Prepares the HTTP request"
[request-uri question query-params auth]
{:method :get
:url request-uri
:query-params (merge query-params auth {:question question})
:headers {"User-Agent" user-agent}})

(defn direct-answer
[& args]
(let [request-uri (str "http://" *api-url* "/direct_answer/" *api-version*)
split-args (split-positional-args args)
question (apply str (first split-args))
query-params (second split-args)
auth {:api_account_id *api-account-id* :api_password *api-password*}
request (prepare-request request-uri question query-params auth)]
(prn request)
(execute-request request)))
:headers {"User-Agent" user-agent}
:as :byte-array})



9 changes: 6 additions & 3 deletions test/socrates/test/core.clj
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
(ns socrates.test.core
(:use socrates.core)
(:use socrates.core
socrates.api.direct-answer)
(:use clojure.test
socrates.test.properties))

(deftest replace-me ;; FIXME: write
(deftest direct-answer-test
(with-credentials *trueknowledge-account-id* *trueknowledge-api-password*
(prn (direct-answer "List of James Bond Actors"))))
(let [answer (direct-answer "List of James Bond Actors")]
(is (= true (:answered answer)))
(is (= "Sean Connery, George Lazenby, Roger Moore, Timothy Dalton, Pierce Brosnan and Daniel Craig" (:result answer))))))

0 comments on commit e0e3b64

Please sign in to comment.