Skip to content

Instantly share code, notes, and snippets.

@kulicuu
Last active August 29, 2015 14:12
Show Gist options
  • Save kulicuu/8daed21ae72548bcf252 to your computer and use it in GitHub Desktop.
Save kulicuu/8daed21ae72548bcf252 to your computer and use it in GitHub Desktop.
life's simple pleasures
# jan2 request bluebird stuff
c= console.log
_= require 'lodash'
Promise= require 'bluebird'
rp= require 'request-promise'
qStr= require 'querystring'
parse_quals= (quals)->
return _.reduce quals,
(acc0, value, key)->
if value isnt null
switch key
when 'in'
return acc0 + _.reduce value, (acc1, item)->
if (value.indexOf(item) < (value.length - 1)) and (value.indexOf(item) > 0)
return acc1 + "+" + key + ":" + item + "AND"
else
return acc1 + "+" + key + ":" + item
, ""
when 'size','forks', 'created','pushed','user','repo','language', 'stars'
return acc0 + "+" + key + ":" + value
else return acc0
, ""
baseURI= "https://api.github.com/search/repositories?"
stubQ= q: 'britvic' # string only
stubQuals=
in: ['name', 'description'] #string
size: '>=264'
forks: '<=3'
stars: '>=0'
stubOpts=
order: 'asc' # string asc or desc
sort: 'updated'#'stars' # string 'stars', 'forks', 'updated', this needed to be an array for some reason
make_strang= (base ,q, opts, quals)->
strQ= qStr.stringify q
strOpts= qStr.stringify opts
strQuals= _.reduce quals,
(acc0, value, key)->
if value isnt null
switch key
when 'in'
return acc0 + _.reduce value, (acc1, item)->
if (value.indexOf(item) < (value.length - 1)) and (value.indexOf(item) > 0)
return acc1 + "+" + key + ":" + item + "AND"
else
return acc1 + "+" + key + ":" + item
, ""
when 'size','forks', 'created','pushed','user','repo','language', 'stars'
return acc0 + "+" + key + ":" + value
else return acc0
, ""
return base + strQ + strQuals + '&' +strOpts
strang= make_strang(baseURI, stubQ, stubOpts, stubQuals)
c strang
go= (opts)->
rp opts
.on 'response', (response)->
c response.headers
.then (res)->
arq= JSON.parse res
c arq
getOpts=
method: 'GET'
uri: strang
headers:
'User-Agent': 'kulicuu'
go getOpts
Promise= require 'bluebird'
c= console.log
redis= require 'redis'
client= redis.createClient()
d= Promise.promisifyAll client
d.setAsync "foo", "bar"
.then (re)->
c re
rayy= []
rayy.push d.setAsync "foo0", "bar0"
rayy.push d.setAsync "foo1", "bar1"
rayy.push d.setAsync "foo2", "bar2"
Promise.all(rayy)
.then (rayy1)->
c rayy1
rayy2= []
rayy2.push d.getAsync "foo0"
rayy2.push d.getAsync "foo1"
rayy2.push d.getAsync "foo2"
Promise.all(rayy2)
.then (rayy3)->
c rayy3
c= console.log
Bluebird= require 'bluebird'
obj=
func0: (a, cb)->
c a
cb null, {message: 'this is func0'}
func1: (a, cb)->
c a
func0Async= Bluebird.promisify @func0, @
func0Async "hello"
.bind @
.then (go0)->
c "@", @
c go0
obj.func1 "testing testing"
c= console.log
# becoming more aware of functional protocol
_= require 'lodash'
rp= require 'request-promise'
qu= require 'querystring'
beez= ""
beez+= "+++++"
c beez
qqq=
timezone: 'alaska'
rayy= ['one', 'two', 'three']
caalb= (acc, a)->
return acc + (a + @timezone)
strang= _.reduce rayy, caalb, "", qqq
streng= _.reduce rayy, (acc, a)->
return acc + (@timezone + a + 'inline')
, "", qqq
c strang
c streng
arq= {five: 'four', three: 'two'}
x= _.reduce arq, (acc, value, key)->
return acc + key + value
, "access:"
c x
stub=
keywords: ['britvic', 'kulicuu']
qualifiers:
in: ['name']# becoming more aware of functional protocol
size: null
forks: null
created: null
pushed: null
user: null
repo: null
language: null
stars: null
sort: null
order: null
make_QueryString= (qO)->
# make the query string element of the uri
qS= _.reduce qO.keywords, (acc, word)->
if ( qO.keywords.indexOf(word) < qO.keywords.length ) and ( qO.keywords.indexOf(word) > 0 )
return acc + "," + word
else
return acc + word
, "q="
qS+= _.reduce qO.qualifiers, (acc0, value, key)->
if value isnt null
switch key
when 'in'
return acc0 + _.reduce value, (acc1, item)->
if (value.indexOf(item) < value.length) and (value.indexOf(item) > 0)
return acc1 + "+" + key + ":" + item + "AND"
else
return acc1 + "+" + key + ":" + item
, ""
when 'size'
return acc0 + "+" + key + ":" + value
when 'forks'
return acc0 + "+" + key + ":" + value
when 'created'
return acc0 + "+" + key + ":" + value
when 'pushed'
return acc0 + "+" + key + ":" + value
when 'user'
return acc0 + "+" + key + ":" + value
when 'repo'
return acc0 + "+" + key + ":" + value
when 'language'
return acc0 + "+" + key + ":" + value
when 'stars'
return acc0 + "+" + key + ":" + value
else return acc0
, ""
return qS
strang= make_QueryString stub
c "strang", strang
baseURI= "https://api.github.com/"
stub_X=
q: ['scaffold/seedApp+in:description']
in: null
size: null
forks: null
created: null
pushed: null
user: null
repo: null
language: null
stars: null
sort: null
order: null
strang_X= qu.stringify stub_X
c strang_X
options=
method: 'GET'
uri: baseURI + "search/repositories?#{strang_X}"
headers:
'User-Agent': 'kulicuu'
go= (opts)->
rp opts
.on 'response', (response)->
c response.headers
.then (res)->
arq= JSON.parse res
c arq
#go()
# what all of this means is that we can use qu.stringify at the
# outer layer to prepare our querystring but that the q variable
# itself needs to be parsed with reduction functions first to prepare the q variable
# so:
stubY=
q: ['kulicuu','britvic']
opts=
in: ['name', 'description']
forks: null
language: 'coffeescript'
stubZ=
sort: null
order: null
qS= ""
qS+= _.reduce opts, (acc0, value, key)->
if value isnt null
switch key
when 'in'
return acc0 + _.reduce value, (acc1, item)->
if (value.indexOf(item) < (value.length - 1)) and (value.indexOf(item) > 0)
return acc1 + "+" + key + ":" + item + "AND"
else
return acc1 + "+" + key + ":" + item
, ""
when 'size'
return acc0 + "+" + key + ":" + value
when 'forks'
return acc0 + "+" + key + ":" + value
when 'created'
return acc0 + "+" + key + ":" + value
when 'pushed'
return acc0 + "+" + key + ":" + value
when 'user'
return acc0 + "+" + key + ":" + value
when 'repo'
return acc0 + "+" + key + ":" + value
when 'language'
return acc0 + "+" + key + ":" + value
when 'stars'
return acc0 + "+" + key + ":" + value
else return acc0
, ""
c "qS", qS
x= qu.stringify stubY
blasto= x + qS
c "blasto", blasto
options2=
method: 'GET'
uri: baseURI + "search/repositories?#{blasto}"
headers:
'User-Agent': 'kulicuu'
go options2
# from the responses i'm getting it looks like every addition to querystring var q can have it's own qualifiers, so this makes things more complicated, but i'm seeing the light. [edit:] also possible they only take on string instead of an optional array of strings for the q.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment