forked from joshrotenberg/socrates
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
super basic direct answer result seems to work
- Loading branch information
1 parent
dcd856d
commit e0e3b64
Showing
3 changed files
with
41 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)))) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)))))) |