Skip to content

Instantly share code, notes, and snippets.

View mwmitchell's full-sized avatar

Matt Mitchell mwmitchell

View GitHub Profile
* Adding handle: conn: 0x7fad39804000
* Adding handle: send: 0
* Adding handle: recv: 0
* Curl_addHandleToPipeline: length: 1
* - Conn 0 (0x7fad39804000) send_pipe: 1, recv_pipe: 0
* About to connect() to localhost port 8765 (#0)
* Trying ::1...
* Connected to localhost (::1) port 8765 (#0)
> GET /api HTTP/1.1
> User-Agent: curl/7.30.0
(ns truth.core)
(defn get-data []
{:data {:name {:first "matt"}}})
(defmacro truth [lbl val tests redefs]
(loop [t tests]
(if-not (seq t)
true
`(with-redefs [~@(mapcat identity redefs)]
@mwmitchell
mwmitchell / test.clj
Created March 20, 2014 01:54
clojure scribe / oauth
(ns foo
(:import [org.scribe.builder ServiceBuilder]
[org.scribe.builder.api.FlickrApi]
[org.scribe.model Verifier OAuthRequest Verb]))
(let [builder (doto (ServiceBuilder.)
(.provider FlickrApi)
(.apiKey "YOURKEY")
(.apiSecret "YOURSECRET"))]
(def service (.build builder)))
@mwmitchell
mwmitchell / proxy-express.js
Last active August 29, 2015 14:04
proxy-express
// "dependencies": {
// "http-server": "0.6.1",
// "express": "4.6.1",
// "request": "2.37.0",
// "errorhandler": "1.0.1"
// }
var express = require('express');
var request = require('request');
//Gruber wrote this regex for matching URLs, but it took a small amount of massage to use it in JavaScript. So here.
//Sauce: http://daringfireball.net/2010/07/improved_regex_for_matching_urls
var p = /^\b((?:https?:\/\/|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))/i;
p.exec('party fun www.twitter.com yay') //winning.
{
"id":"1",
"signalTypes":["reward"],
"selectQuery": "{!edismax bf=\"recip(abs(ms(NOW/HOUR,timestamp_dt)),3.16e-11,1,.1)\"}*:*",
"aggregates":[
{"type":"count", "sourceFields":["id"], "targetField": "count_f"},
{
"type": "script",
"params": {
"aggregateScript": "result.addField('rewards_fs', event.getFirstFieldValue('params.reward_f'))",
@mwmitchell
mwmitchell / map_names.clj
Created December 6, 2014 18:32
xmas gift swap
(require '[clojure.string :as str])
(require '[clojure.set :as set])
(require '[clojure.pprint :as pp])
(defn map-names [names]
(first
(reduce
(fn [mem n1]
(let [avail (set/difference names (get mem 1))
sel (->> avail shuffle (some #(when (not= n1 %1) %1)))]
@mwmitchell
mwmitchell / maybe_update_in.clj
Created January 29, 2015 04:30
maybe-update-in
;; Like update-in, but doesn't call the fn
;; (or set a key) if the key doesn't already exist.
(defn maybe-update-in [data ks fn]
(if ((last ks) (-> data (get-in (butlast ks)) keys set))
(update-in data ks fn)
data))
@mwmitchell
mwmitchell / macro.clj
Created January 29, 2015 15:33
defmacro for dynamic def name
(def names {:foo 'FOO})
(defmacro def* [dname dval]
`(def ~dname ~dval))
;; This doesn't resolve the (get names :foo) => 'FOO
(macroexpand-1 '(def* (get names :foo) 300))
@mwmitchell
mwmitchell / http_kit_timeout_test.clj
Created February 27, 2015 17:26
http_kit_timeout_test.clj
;; test server blocks for 5 seconds
;; this code gets the response successfully
;; the time macro reports just a little over 5 seconds
(time
(let [response (http/get "http://localhost:4567/" {:timeout 3000})]
@response))