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
@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]
@nightire
nightire / Changes in Rails 4_1.md
Last active May 11, 2022 04:50
拥抱 Rails 4 —— 详述 Rails 4 的新变化

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 / 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
@huangxiangdan
huangxiangdan / check_mysql.rb
Created April 26, 2012 01:19
a ejabberd extauth script
#!/usr/bin/env ruby
class NeterDbAuthorization
def initialize(config_file = 'config.yml')
# load config
require 'yaml'
@cfg = YAML.load_file(config_file)
# load logger
if @cfg['log']['file']
require 'logger'
@bkimble
bkimble / gist:1365005
Last active May 2, 2024 01:27
List local memcached keys using Ruby
#!/usr/bin/env ruby
# List all keys stored in memcache.
# Credit to Graham King at http://www.darkcoding.net/software/memcached-list-all-keys/ for the original article on how to get the data from memcache in the first place.
require 'net/telnet'
headings = %w(id expires bytes cache_key)
rows = []
@zuk
zuk / blather-groupchat-example.rb
Created February 27, 2011 22:36
XMPP Groupchat reverse echo using Blather
require 'rubygems'
require 'blather/client'
# CONFIGURATION
jid = 'tester@proto.encorelab.org'
password = "foofoo"
chatroom = "s3@conference.proto.encorelab.org"
setup(jid, password)
#!/usr/bin/env ruby
require 'logger'
require 'rest_client'
$stdout.sync = true
$stdin.sync = true
path = "/usr/local/var/log/ejabberd/auth.log"
file = File.open(path, File::WRONLY | File::APPEND | File::CREAT)