Skip to content

Commit fcd8ccb

Browse files
committed
Merge pull request drone29a#10 from tavisrudd/master
update to clj 1.3
2 parents 6d1b6df + 52ca097 commit fcd8ccb

File tree

2 files changed

+97
-82
lines changed

2 files changed

+97
-82
lines changed

project.clj

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
(defproject clojure-twitter "1.2.5"
1+
(defproject clojure-twitter "1.2.6-SNAPSHOT"
22
:description "Twitter Client API for Clojure"
3-
:dependencies [[org.clojure/clojure "1.2.0"]
4-
[org.clojure/clojure-contrib "1.2.0"]
5-
[clj-oauth "1.2.10"]
6-
[com.twinql.clojure/clj-apache-http "2.3.1"]]
7-
:dev-dependencies [[swank-clojure "1.2.1"]])
3+
:dependencies [[org.clojure/clojure "1.3.0"]
4+
[org.clojure/data.json "0.1.2"]
5+
[clj-oauth "1.3.1-SNAPSHOT"]
6+
[org.clojars.tavisrudd/clj-apache-http "2.3.2-SNAPSHOT"]])

src/twitter.clj

Lines changed: 92 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
(ns twitter
2-
(:use [clojure.contrib.json :only [read-json]]
3-
[clojure.contrib.java-utils :only [as-str]])
2+
(:use [clojure.data.json :only [read-json]])
43
(:require [clojure.set :as set]
54
[clojure.string :as string]
65
[com.twinql.clojure.http :as http]
@@ -13,12 +12,12 @@
1312

1413
(declare status-handler)
1514

16-
(def *oauth-consumer* nil)
17-
(def *oauth-access-token* nil)
18-
(def *oauth-access-token-secret* nil)
19-
(def *protocol* "http")
15+
(def ^:dynamic *oauth-consumer* nil)
16+
(def ^:dynamic *oauth-access-token* nil)
17+
(def ^:dynamic *oauth-access-token-secret* nil)
18+
(def ^:dynamic *protocol* "http")
2019

21-
;; Get JSON from clj-apache-http
20+
;; Get JSON from clj-apache-http
2221
(defmethod http/entity-as :json [entity as state]
2322
(read-json (http/entity-as entity :string state)))
2423

@@ -28,13 +27,13 @@
2827
`(binding [*oauth-consumer* ~consumer
2928
*oauth-access-token* ~access-token
3029
*oauth-access-token-secret* ~access-token-secret]
31-
(do
30+
(do
3231
~@body)))
3332

3433
(defmacro with-https
3534
[ & body]
3635
`(binding [*protocol* "https"]
37-
(do
36+
(do
3837
~@body)))
3938

4039
(defmacro def-twitter-method
@@ -51,21 +50,34 @@ take any required and optional arguments and call the associated Twitter method.
5150
rest-map# (apply hash-map rest#)
5251
provided-optional-params# (set/intersection (set ~optional-params)
5352
(set (keys rest-map#)))
54-
required-query-param-names# (map (fn [x#]
55-
(keyword (string/replace (name x#) #"-" "_" )))
56-
~required-params)
57-
optional-query-param-names-mapping# (map (fn [x#]
58-
[x# (keyword (string/replace (name x#) #"-" "_"))])
59-
provided-optional-params#)
60-
query-params# (merge (apply hash-map
61-
(vec (interleave required-query-param-names# ~required-fn-params)))
62-
(apply merge
63-
(map (fn [x#] {(second x#) ((first x#) rest-map#)}) optional-query-param-names-mapping#)))
53+
required-query-param-names#
54+
(map (fn [x#]
55+
(keyword (string/replace (name x#) #"-" "_" )))
56+
~required-params)
57+
58+
optional-query-param-names-mapping#
59+
(map (fn [x#]
60+
[x# (keyword (string/replace (name x#) #"-" "_"))])
61+
provided-optional-params#)
62+
63+
query-params#(merge (apply hash-map
64+
(vec (interleave
65+
required-query-param-names#
66+
~required-fn-params)))
67+
(apply merge
68+
(map (fn [x#] {(second x#)
69+
((first x#)
70+
rest-map#)})
71+
optional-query-param-names-mapping#)))
6472
need-to-url-encode# (if (= :get ~req-method)
65-
(into {} (map (fn [[k# v#]] [k# (oauth.signature/url-encode v#)]) query-params#))
73+
(into {}
74+
(map (fn [[k# v#]]
75+
[k#
76+
(oauth.signature/url-encode v#)])
77+
query-params#))
6678
query-params#)
67-
oauth-creds# (when (and *oauth-consumer*
68-
*oauth-access-token*)
79+
oauth-creds# (when (and *oauth-consumer*
80+
*oauth-access-token*)
6981
(oauth/credentials *oauth-consumer*
7082
*oauth-access-token*
7183
*oauth-access-token-secret*
@@ -76,7 +88,7 @@ take any required and optional arguments and call the associated Twitter method.
7688
req-uri#
7789
:query (merge query-params#
7890
oauth-creds#)
79-
:parameters (http/map->params
91+
:parameters (http/map->params
8092
{:use-expect-continue false})
8193
:as :json))))))
8294

@@ -282,7 +294,7 @@ take any required and optional arguments and call the associated Twitter method.
282294
(def-twitter-method friends-of-name
283295
:get
284296
"api.twitter.com/1/friends/ids.json"
285-
[:screen-name]
297+
[:screen-name]
286298
[]
287299
(comp #(:content %) status-handler))
288300

@@ -349,7 +361,7 @@ take any required and optional arguments and call the associated Twitter method.
349361

350362
(defn update-profile-image [^String image]
351363
(let [req-uri__9408__auto__ "http://api.twitter.com/1/account/update_profile_image.json"
352-
364+
353365
oauth-creds__9414__auto__ (when
354366
(and
355367
*oauth-consumer*
@@ -379,45 +391,47 @@ take any required and optional arguments and call the associated Twitter method.
379391
(comp #(:content %) status-handler)))
380392

381393
(defn update-profile-background-image [^String image & rest__2570__auto__]
382-
(let [req-uri__2571__auto__ "http://api.twitter.com/1/account/update_profile_background_image.json"
383-
rest-map__2572__auto__ (apply hash-map rest__2570__auto__)
384-
provided-optional-params__2573__auto__ (set/intersection
385-
(set [:title])
386-
(set
387-
(keys
388-
rest-map__2572__auto__)))
389-
query-param-names__2574__auto__ (sort
390-
(map
391-
(fn
392-
[x__2575__auto__]
393-
(keyword
394-
(string/replace
395-
(name
396-
x__2575__auto__)
397-
#"-"
398-
"_"
399-
)))
400-
provided-optional-params__2573__auto__))
401-
query-params__2576__auto__ (apply
402-
hash-map
403-
(interleave
404-
query-param-names__2574__auto__
405-
(vec
406-
(vals
407-
(sort
408-
(select-keys
409-
rest-map__2572__auto__
410-
provided-optional-params__2573__auto__))))))
411-
oauth-creds__2577__auto__ (when
412-
(and
413-
*oauth-consumer*
414-
*oauth-access-token*)
415-
(oauth/credentials
416-
*oauth-consumer*
417-
*oauth-access-token*
418-
:post
419-
req-uri__2571__auto__
420-
query-params__2576__auto__))]
394+
(let [req-uri__2571__auto__
395+
"http://api.twitter.com/1/account/update_profile_background_image.json"
396+
397+
rest-map__2572__auto__ (apply hash-map rest__2570__auto__)
398+
provided-optional-params__2573__auto__ (set/intersection
399+
(set [:title])
400+
(set
401+
(keys
402+
rest-map__2572__auto__)))
403+
query-param-names__2574__auto__ (sort
404+
(map
405+
(fn
406+
[x__2575__auto__]
407+
(keyword
408+
(string/replace
409+
(name
410+
x__2575__auto__)
411+
#"-"
412+
"_"
413+
)))
414+
provided-optional-params__2573__auto__))
415+
query-params__2576__auto__ (apply
416+
hash-map
417+
(interleave
418+
query-param-names__2574__auto__
419+
(vec
420+
(vals
421+
(sort
422+
(select-keys
423+
rest-map__2572__auto__
424+
provided-optional-params__2573__auto__))))))
425+
oauth-creds__2577__auto__ (when
426+
(and
427+
*oauth-consumer*
428+
*oauth-access-token*)
429+
(oauth/credentials
430+
*oauth-consumer*
431+
*oauth-access-token*
432+
:post
433+
req-uri__2571__auto__
434+
query-params__2576__auto__))]
421435
((comp #(:content %) status-handler)
422436
(http/post req-uri__2571__auto__
423437
:query (merge query-params__2576__auto__ oauth-creds__2577__auto__)
@@ -430,7 +444,7 @@ take any required and optional arguments and call the associated Twitter method.
430444
:post
431445
"api.twitter.com/1/account/update_profile.json"
432446
[]
433-
[:name
447+
[:name
434448
:email
435449
:url
436450
:location
@@ -605,21 +619,23 @@ take any required and optional arguments and call the associated Twitter method.
605619
"Handle the various HTTP status codes that may be returned when accessing
606620
the Twitter API."
607621
[result]
608-
(condp #(if (coll? %1)
622+
(condp #(if (coll? %1)
609623
(first (filter (fn [x] (== x %2)) %1))
610624
(== %2 %1)) (:code result)
611625
200 result
612626
304 nil
613-
[400 401 403 404 406 500 502 503] (let [body (:content result)
614-
headers (into {} (:headers result))
615-
error-msg (:error body)
616-
error-code (:code result)
617-
request-uri (:request body)]
618-
(throw (proxy [Exception] [(str "[" error-code "] " error-msg ". [" request-uri "]")]
619-
(request [] (body "request"))
620-
(remaining-requests [] (headers "X-RateLimit-Remaining"))
621-
(rate-limit-reset [] (java.util.Date.
622-
(long (headers "X-RateLimit-Reset")))))))))
627+
[400 401 403 404 406 500 502 503]
628+
(let [body (:content result)
629+
headers (into {} (:headers result))
630+
error-msg (:error body)
631+
error-code (:code result)
632+
request-uri (:request body)]
633+
(throw (proxy [Exception]
634+
[(str "[" error-code "] " error-msg ". [" request-uri "]")]
635+
(request [] (body "request"))
636+
(remaining-requests [] (headers "X-RateLimit-Remaining"))
637+
(rate-limit-reset [] (java.util.Date.
638+
(long (headers "X-RateLimit-Reset")))))))))
623639

624640
(defn make-rate-limit-handler
625641
"Creates a handler that will only be called if the API rate limit has been exceeded."

0 commit comments

Comments
 (0)