Skip to content

Instantly share code, notes, and snippets.

View godfat's full-sized avatar

Lin Jen-Shin (godfat) godfat

View GitHub Profile
@godfat
godfat / draft.md
Last active August 29, 2015 14:10
The Promise of rest-core

rest-core 的承諾

在 [RubyConf.TW 2011][],我介紹了 [rest-core][],是用 Ruby 寫成的模組化 REST client 組合包與工具組。後來組合包的東西被抽至 [rest-more][],於是變成給 REST APIs 使用的模組化 Ruby clients interface。

在 [RubyConf.TW 2012][],我介紹了 Ruby 上的 concurrent 應用程式伺服器。之後在 rest-core 內,加入我從以上所學到的東西,使它能輕易地發出 concurrent requests。

在不同的使用情況下,我們有時候需要使用 promise、有時候需要使用 callback。這次我將介紹該怎麼利用 promise 來達成以我們所熟習的同步風格,發出

module SkipVerification
def verify_mode
OpenSSL::SSL::VERIFY_NONE
end
end
HTTPClient::SSLConfig.prepend(SkipVerification)
task :test do
sh 'ruby another.rb' # at_exit in another.rb didn't run or $stdout is broken or redirected?
# actuall: sh 'cd api; ruby -S rake test'
end
#!/bin/sh
echo RAILS_ENV={development,test} | xargs -n1 -P2 rake db:migrate
echo 'rake db:structure:dump,rake db:schema:dump' | xargs -d, -n1 -P2 sh -c
cp db/structure.sql db/schema.sql
"Lin Jen Shin (godfat) is a programmer who loves computer games, open source, Haskell and self-referential jokes, such as: %p=~/(.+)/;puts(($1%%$1)[/: (.+)/,1])"=~/(.+)/;puts(($1%$1)[/: (.+)/,1])
@godfat
godfat / api
Last active August 29, 2015 14:21
x-smtpapi: {"filters":{"templates":{"settings":{"enable":1,"template_id":"template-1"}}},
"section":{":name":"Alice",":message":"this is Bob."}}
html: <%template-2%>
class MigrateCookie < Struct.new(:app)
def call env
cookies = Rack::Utils.parse_query(env['HTTP_COOKIE'], ';,')
case cookies['_session_id']
when Array
status, headers, body = app.call(env)
headers['Set-Cookie'] =
"#{headers['Set-Cookie']}\n#{expire_old_session}"
[status, headers, body]
else
@godfat
godfat / query.sql
Last active August 29, 2015 14:22
PostgreSQL fast random rows for tables with integer id with uniform distribution
WITH RECURSIVE results(id, i, picked, roll) AS (
WITH bounds AS (SELECT min(id), max(id) - min(id) AS delta FROM table)
(
SELECT NULL::integer
, 0
, ARRAY[]::integer[]
, min + round(delta * random())
FROM bounds
)
UNION ALL
@godfat
godfat / a.rb
Last active August 29, 2015 14:24
class User < Struct.new(:enable_email, :email)
extend Validator
validate :email, v.present(:enable_email) & v.match(:email, /[^@]+@[^@]+/)
end
User.new(true, 'a@b').check
net/http:
https://github.com/jnunemaker/httparty
https://github.com/rest-client/rest-client
https://github.com/lostisland/faraday
https://github.com/drbrain/net-http-persistent
ruby:
https://github.com/excon/excon
https://github.com/httprb/http
https://github.com/nahi/httpclient