Skip to content

Instantly share code, notes, and snippets.

View swordray's full-sized avatar

Jianqiu Xiao swordray

View GitHub Profile
@swordray
swordray / random_password.rb
Last active July 18, 2016 04:30
Random password
puts 16.times.map { ('!'..'~').to_a.sample }.join
require 'rack/reverse_proxy'
require 'sinatra'
use Rack::ReverseProxy do
reverse_proxy /^.*$/, 'http://api.secipin.com/guide/products.json?per=1'
end
@swordray
swordray / validates_timeliness.zh_CN.yml
Last active August 29, 2015 14:11
validates_timeliness.zh_CN.yml
zh-CN:
errors:
messages:
invalid_date: "是无效的日期"
invalid_time: "是无效的时间"
invalid_datetime: "是无效的时间"
is_at: "必须在 %{restriction}"
before: "必须早于 %{restriction}"
on_or_before: "必须在或早于 %{restriction}"
after: "必须晚于 %{restriction}"
@swordray
swordray / game2048.rb
Last active November 27, 2021 07:47
Implement game 2048 in less than 30 lines of Ruby code
class Game2048
def initialize
@array = 4.times.map { [ nil ] * 4 }
2.times { fill }
end
def fill
i, j = rand(4), rand(4)
return fill if @array[i][j]
@array[i][j] = [2, 2, 2, 2, 4].shuffle.first
@swordray
swordray / nginx_gzip.conf
Last active December 16, 2015 13:11
Nginx GZip configuration
gzip on;
gzip_comp_level 9;
gzip_min_length 1024;
gzip_buffers 4 8k;
gzip_types application/javascript application/json application/x-javascript application/xml text/css text/csv text/plain;
gzip_disable "MSIE [1-6]\.";
---
pid: tmp/pids/thin.pid
wait: 30
log: log/thin.log
timeout: 30
max_conns: 1024
port: 4000
environment: production
chdir: /opt/project/
max_persistent_conns: 512
@swordray
swordray / gist:9292104
Created March 1, 2014 16:14
Uninstall all ruby gems
for i in `gem list --no-versions`; do gem uninstall -aIx $i; done
@swordray
swordray / gist:9168494
Last active August 29, 2015 13:56
ActiveRecord serialization with 1000% performance improvement by skipping instantiation
records = ActiveRecordClass.select().where().order().paginate()
# before
records.to_json
# after - with 1000% performance improvement
records.klass.connection.execute(records.to_sql).to_a.map { |record| [records.select_values, record].transpose.to_h } }.to_json
@swordray
swordray / gist:8882330
Last active August 29, 2015 13:56
Alipay checkout url within three line Ruby code - 三行 Ruby 代码实现支付宝付款地址
def alipay_url(options)
options.merge!(seller_email: EMAIL, partner: ACCOUNT, _input_charset: 'utf-8')
options.merge!(sign_type: 'MD5', sign: Digest::MD5.hexdigest(options.sort_by(&:first).to_h.to_param + KEY))
HTTParty.get("https://www.alipay.com/cooperate/gateway.do?#{options.sort.map{ |k, v| "#{CGI::escape(k.to_s)}=#{CGI::escape(v.to_s)}"}.join('&') }")
end
@swordray
swordray / gist:8882187
Last active August 29, 2015 13:56
Pure static server within one line Ruby code
gem install sinatra # run once
ruby -e "require 'sinatra'; set :port, 80; set :environment, :production; set :public_folder, File.dirname(__FILE__)"