Skip to content

Instantly share code, notes, and snippets.

View maxweber's full-sized avatar

Max Weber maxweber

View GitHub Profile
@maxweber
maxweber / gist:843901
Created February 25, 2011 15:03
Slurping something from the Facebook Graph API
user> (slurp "https://graph.facebook.com/btaylor")
"{\"id\":\"220439\",
\"name\":\"Bret Taylor\",
\"first_name\":\"Bret\",
\"last_name\":\"Taylor\",
\"link\":\"http:\\/\\/www.facebook.com\\/btaylor\",
\"gender\":\"male\",
\"locale\":\"en_US\"}"
@maxweber
maxweber / gist:845123
Created February 26, 2011 11:24
Slurping something from the Facebook Graph API into a Clojure data structure
user> (use 'clojure.contrib.json)
nil
user> (read-json (slurp "https://graph.facebook.com/btaylor"))
{:id "220439",
:name "Bret Taylor",
:first_name "Bret",
:last_name "Taylor",
:link "http://www.facebook.com/btaylor",
:gender "male",
:locale "en_US"}
@maxweber
maxweber / gist:845261
Created February 26, 2011 14:47
Fetching something from the Facebook Graph API with clj-http
user> (require '[clj-http.client :as client])
nil
user> (use 'clojure.pprint)
nil
user> (pprint (client/get "https://graph.facebook.com/220439"))
{:status 200,
:headers
{"p3p"
"CP=\"Facebook does not have a P3P policy. Learn why here: http://fb.me/p3p\"",
"content-type" "text/javascript; charset=UTF-8",
@maxweber
maxweber / gist:848926
Created March 1, 2011 10:05
A clj-http middleware which automatically converts a JSON response into a corresponding Clojure data structure (from the clj-facebook-graph project)
(use 'clojure.contrib.json)
(defn wrap-json-response-conversion [client]
"Automatically transforms the body of a response of a Facebook Graph API request from JSON to a Clojure
data structure through the use of clojure.contrib.json. It checks if the header Content-Type
is 'text/javascript' which the Facebook Graph API returns in the case of a JSON response."
(fn [req]
(let [{:keys [headers] :as resp} (client req)
content-type (headers "content-type")]
(if (and (not (nil? content-type))
(.startsWith content-type "text/javascript"))
@maxweber
maxweber / gist:848948
Created March 1, 2011 10:26
Using the clj-facebook-graph middleware wrap-json-response-conversion to convert Facebook JSON responses automatically to a corresponding Clojure data structure
user> (use 'clojure.pprint)
nil
user> (require '[clj-http.client :as client])
nil
user> (def request (wrap-json-response-conversion (client/wrap-request #'clj-http.core/request)))
#'user/request
user> (pprint (:body (request {:method :get :url "https://graph.facebook.com/btaylor"})))
{:id "220439",
:name "Bret Taylor",
:first_name "Bret",
@maxweber
maxweber / gist:851009
Created March 2, 2011 14:27
The clj-http.client get function.
(defn get
"Like #'request, but sets the :method and :url as appropriate."
[url & [req]]
(request (merge req {:method :get :url url})))
@maxweber
maxweber / gist:851090
Created March 2, 2011 15:30
Start your engines
(def server (start-server))
{
"error": {
"type": "OAuthException",
"message": "Error validating application."
}
}
clj-facebook-graph.example> (facebook-auth-by-name)
{"Max Weber" {:access-token "134211159981787|2.NFMD8_yemOzJ6rIH3Ezukw__.3600.129951..."}}
@maxweber
maxweber / gist:858684
Created March 7, 2011 16:00
A list of friends fetched by clj-facebook-graph
clj-facebook-graph.example> (with-facebook-auth-by-name "Max Weber" (client/get "https://graph.facebook.com/me/friends"))
{:status 200, :headers {"content-type" "text/javascript; charset=UTF-8",
"date" "Mon, 07 Mar 2011 15:56:41 GMT",
"cache-control" "private, no-cache, no-store, must-revalidate",
"expires" "Sat, 01 Jan 2000 00:00:00 GMT",
"etag" "\"bb3aff0d25be9397d666254583743b7a8bcfb60e\"",
"x-powered-by" "HPHP", "content-length" "4462",
"pragma" "no-cache", "connection" "close", "x-fb-server" "10.32.53.102"},
:body {:data [{:name "Friend 1", :id "id of friend one"} {:name "Friend 2", :id "id of friend two"} ...