Skip to content

Instantly share code, notes, and snippets.

View jimhj's full-sized avatar
🕶️
Hard working....

Jimmy jimhj

🕶️
Hard working....
  • Beijing
View GitHub Profile
@jimhj
jimhj / gist:9827616
Created March 28, 2014 08:04
ssh...you know it
ssh -qTfnN -D 7070 user@remote-server
@jimhj
jimhj / translate.rb
Last active December 20, 2015 00:49
Translate chars into Chinese pinyin or English. Lies on https://github.com/flyerhzm/chinese_pinyin
# coding: utf-8
class Translate
YOUDAO_HOST = "http://fanyi.youdao.com"
YOUDAO_QUERY_PATH = "openapi.do"
YOUDAO_KEY_FROM = "Paikeyun"
YOUDAO_KEY = "987423680"
def initialize(chars, opts = {})
@chars = chars
@opts = opts

Routes

小心地使用 Match(Rails 3 已实现)

Rails 3 提供了 match 方法供我们自定义 routes,然而我们要小心使用它以避免“跨站脚本攻击”(XSS Attack)。比如像这样的 routes:

注:(r3 代表 Rails 3,r4 代表 Rails 4)

# routes.rb
@jimhj
jimhj / controller.rb
Created December 18, 2012 10:32
极简验证码生成 ImageMagick & MiniMagick
require 'mini_magick'
class CustomAdmin::MiniCaptchaController < CustomAdmin::ApplicationController
before_filter :clear_captcha_session, :only => :mini_captcha
@jimhj
jimhj / uncached.rb
Created November 15, 2012 05:35
Uncache query in ActiveRecord
def self.get_responded_inquiry(mobile_no, company_id)
uncached do
self.find(:last, :conditions => ["mobile_no = ? and company_id = ? and response_status = 1", mobile_no, company_id])
# self.find_all_by_mobile_no_and_company_id_and_response_status(mobile_no, company_id, 1).last
end
end
def self.last_req(mobile_no, company_id)
uncached do
self.find(:last, :conditions => ["mobile_no = ? and company_id = ?", mobile_no, company_id])
@jimhj
jimhj / balance_server.rb
Created October 30, 2012 13:27
Eventmachine with AR demo
require 'socket'
require 'rubygems'
require 'eventmachine'
require 'logger'
require 'active_record'
# Rails -v: 2.3.5
REF_RAILS_ROOT_DIR = File.expand_path(File.join(File.dirname(__FILE__),'..','..'))
class MobileBalanceServer < EventMachine::Connection
@jimhj
jimhj / base.rb
Created July 11, 2012 08:28
Use Mini_Magick to crop animated gif images.
# coding: utf-8
module SimpleUpload
class Base
class << self
def max_size
10485760 #(10M)
end
def extension_white_list