Skip to content

Commit d6b5f23

Browse files
committed
Merge branch 'release/0.1.3'
2 parents 620d67d + 0e4adaa commit d6b5f23

File tree

6 files changed

+45
-40
lines changed

6 files changed

+45
-40
lines changed

project.clj

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
(defproject gfycat-api "0.1.2"
1+
(defproject gfycat-api "0.1.3"
22
:description "Clojure wrapper for Gfycat API"
33
:url "http://example.com/FIXME"
44
:license {:name "EPL-2.0"

src/gfycat_api/core.clj

+16-18
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,30 @@
11
(ns gfycat-api.core
22
(:require [clojure.data.json :as json]
33
[clj-http.client :as client]
4-
)
4+
[gfycat-api.util :as util])
55
(:gen-class))
66

7-
(defn- clojure-stylify [key]
8-
(keyword (clojure.string/replace key #"_" "-")))
9-
(def client-info (json/read-str (slurp ".client_info.json")
10-
:key-fn clojure-stylify))
11-
127
(defn- gfycat-request [path query-params]
138
(-> (client/get (str "https://api.gfycat.com/v1/" path)
149
{:content-type :json
1510
:accept "text/html"
1611
:query-params query-params})
1712
:body
18-
(json/read-str :key-fn clojure-stylify)))
13+
(json/read-str :key-fn util/clojure-stylify)))
1914

20-
(defn get-token []
21-
(try
22-
(gfycat-request "oauth/token"
23-
{"grant_type" "client_credentials"
24-
"client_id" (:client-id client-info)
25-
"client_secret" (:client-secret client-info)})
26-
(catch Exception e (prn (ex-data e) ))))
15+
(defn get-token [client-id client-secret]
16+
(if (and client-id client-secret)
17+
(try
18+
(gfycat-request "oauth/token"
19+
{"grant_type" "client_credentials"
20+
"client_id" client-id
21+
"client_secret" client-secret})
22+
(catch Exception e (prn (ex-data e))))))
2723

2824
(defn search
29-
([token query & [count cursor]]
30-
(gfycat-request "gfycats/search" {"search_text" query
31-
"count" count
32-
"cursor" cursor})))
25+
"doesnt require a token"
26+
([query & [count cursor token]]
27+
(let [{found :found :as search-result} (gfycat-request "gfycats/search" {"search_text" query
28+
"count" count
29+
"cursor" cursor})]
30+
(if (> found 0) search-result))))

src/gfycat_api/util.clj

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
(ns gfycat-api.util)
22

3-
(defn get-gfycats-with [keys search-result]
4-
"returns list of gfycats with selected parameters,
5-
removes all other parameters"
6-
(map #(select-keys % keys) (:gfycats search-result)))
3+
(defn clojure-stylify [key]
4+
(keyword (clojure.string/replace key #"_" "-")))

test/gfycat_api/core_test.clj

+24-11
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,28 @@
11
(ns gfycat-api.core-test
22
(:require [clojure.test :refer [deftest is testing]]
3+
[clojure.data.json :as json]
34
[gfycat-api.core :as core]
4-
))
5+
[gfycat-api.util :as util]))
56

6-
#_(deftest get-token-test
7-
(let [token (core/get-token)]
8-
(is (not (nil? token)))
9-
(is (= (:expires-in token) 3600))))
10-
#_(def token (:access-token (core/get-token)))
11-
#_(deftest search-test
12-
(testing "should return one gfycat search result"
13-
(let [search-result (core/search token "thanos power up" 1)]
14-
(is (= (:cursor search-result) ""))))
15-
(testing "searchnig with cursor"))
7+
(def client-info (json/read-str (slurp ".client_info.json")
8+
:key-fn util/clojure-stylify))
9+
10+
(deftest get-token-test
11+
(let [token (core/get-token (:client-id client-info) (:client-secret client-info))]
12+
(is (not (nil? token)))
13+
(is (= (:expires-in token) 3600)))
14+
(testing "should return nil if no client id or client-secret"
15+
(is (= nil (core/get-token nil nil)))))
16+
17+
(def token (:access-token (core/get-token (:client-id client-info) (:client-secret client-info))))
18+
(deftest search-test
19+
(testing "should return one gfycat search result"
20+
(let [search-result (core/search "thanos power up" 1)]
21+
(is (= (:cursor search-result) ""))))
22+
#_(testing "search with token"
23+
(let [search-result (core/search nil "thanos power up" 1)]
24+
(is (= (count (:gfycats search-result)) 1))))
25+
(testing "should return nil on empty search"
26+
(let [search-result (core/search "" 1)]
27+
(is (= search-result nil))))
28+
(testing "searchnig with cursor"))

test/gfycat_api/fixtures.clj

+2-1
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@
8888
:published 1
8989
:rating "PG"
9090
:height 720
91-
:views 27828}]
91+
:views 27828
92+
}]
9293
:related ["compositing" "facebook" "free" "gif" "graphics" "illustrator" "instagram" "motion" "photoshop" "snapchat" "illustrator" " instagram" " facebook" " graphics" " snapchat" " free" " adobe creative cloud" " visual effects" " ambient effects" " free footage" " after effects" " compositing" " photoshop" " gif" " motion"]
9394
:found 2})

test/gfycat_api/util_test.clj

-5
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,3 @@
33
[clojure.test :refer [deftest is testing]]
44
[gfycat-api.fixtures :as fixtures]
55
[gfycat-api.util :as util]))
6-
7-
(deftest search-result-filtering
8-
(is (= (util/get-gfycats-with [:title] fixtures/single-gfycat-search-result)
9-
'({:title
10-
"Canadian Flag waving animated using MIR plug in after effects - free motion graphics"}))))

0 commit comments

Comments
 (0)