Skip to content

Instantly share code, notes, and snippets.

View huacnlee's full-sized avatar

Jason Lee huacnlee

View GitHub Profile
def api_request(action, params = {})
Rails.logger.info "Started request #{host_root} action: #{action}"
# Rails.logger.info " Params: #{params.inspect}"
t1 = Time.now
n = 0
begin
res = Faraday.post do |req|
req.url "http://api.kanbox.com"
req.body = Kanbox::Param.parse(action, params)
req.options[:open_timeout] = 30
@huacnlee
huacnlee / gist:9907235
Created April 1, 2014 03:35
Benchmark for redis-search creating indexes by 0.9.6 VS previous versions
## Before
user system total real
Index 1,000 items 2.160000 0.820000 2.980000 ( 4.633564)
## After (0.9.6)
user system total real
Index 1,000 items 0.780000 0.070000 0.850000 ( 0.851309)
@huacnlee
huacnlee / gist:9525727
Created March 13, 2014 10:20
Ruby Array include? vs index
require 'benchmark'
total_times = 30000
items1 = []
items2 = []
items3 = []
Benchmark.bm do |x|
x.report("include?") {
total_times.times do |i|
@huacnlee
huacnlee / grape_runtime_log.rb
Last active July 3, 2016 09:56
Output request time spend and parameters in log for Grape
require 'grape'
class API < Grape::API
before do
@log_start_t = Time.now
Rails.logger.info " Parameters: #{params.to_hash.except("route_info")}"
end
after do
@log_end_t = Time.now
@huacnlee
huacnlee / .tm_properties
Created November 1, 2013 02:20
~/.tm_properties TextMate 2 for Ruby, Ruby on Rails, RSpec 配置文件
[ "{*.yml,*.yml.default}" ]
fileType = source.yaml
[ "{Capfile,Gemfile,Gemfile.lock,Guardfile}" ]
fileType = source.ruby
[ *_spec.rb ]
fileType = source.ruby.rspec
[ "{app/{models,controllers,mailers,helpers,cells,uploaders}/**/*.rb,*_controller.rb,*_helper.rb,*_mailer.rb}" ]
@huacnlee
huacnlee / god.sh
Created October 22, 2013 10:25
God init.d script 需要 Ubuntu 用 sudo apt-get install god God 配置文件写到 /usr/local/etc/god.rb 里面
#!/bin/bash
#
# god Startup script for god (http://god.rubyforge.org)
#
# chkconfig: - 85 15
# description: God is an easy to configure, easy to extend monitoring \
# framework written in Ruby.
#
CONF=/usr/local/etc/god.rb
@huacnlee
huacnlee / move_files.rb
Last active December 20, 2015 17:49
将一个目前下面的文件,根据文件名前缀分散到不同的文件夹,比如 a12.jpg -> a/1/a12.jpg, b287.jpg -> b/2/b287.jpg 此方法用于处理由于最初设计上传文件夹没有分层次导致一个目录文件过多的问题。可以将一个目录下面的文件分散到子目录中
require "fileutils"
root_dir = "/Users/jason/Downloads/images"
Dir.chdir(root_dir)
puts Dir.pwd
Dir.glob("**/*.{jpg}").each do |fname|
tfname = fname.split("/").last
if fname.match("[small|large|normal]_")
tfname = fname.split("_").last
end
p1,p2 = tfname[0,1],tfname[1,1]
@huacnlee
huacnlee / auto_build_version.sh
Last active December 20, 2015 12:00
Xcode 编译的时候自动修改 App 的 Build 为 Git 的 commit 次数,同时根据不同的 Build 模式,给 build 号加特别的后缀
GIT=/usr/bin/git
INFO_PLIST=$BUILT_PRODUCTS_DIR/$WRAPPER_NAME/Contents/Info.plist
BUILD_SUFIX=""
if [ $CONFIGURATION == Debug ]
then
BUILD_SUFIX="D"
fi
@huacnlee
huacnlee / autofix_sqlite_db
Created May 3, 2013 03:01
自动修复损坏的 SQLite 3 数据库:“database disk image is malformed”
#!/bin/bash
mv "$1" "old_$1"
cat <( sqlite3 "old_$1" .dump | grep "^ROLLBACK" -v ) <( echo "COMMIT;" ) | sqlite3 "$1"
@huacnlee
huacnlee / XMBaseModel.h
Created April 18, 2013 11:32
BaseModel,用于 JSON 转 Objective-C Model
#import <Foundation/Foundation.h>
#import "XMImageURL.h"
enum {
// 保密
XMGenderUnknow = 0,
// 男
XMGenderMale = 1,
// 女
XMGenderFemale = 2,