Skip to content

Instantly share code, notes, and snippets.

View huacnlee's full-sized avatar

Jason Lee huacnlee

View GitHub Profile
# Ruby Scrapi 从中国天气预报网上面抓取天气
# 被抓取页面:http://www.weather.com.cn/html/weather/101270101.shtml
require 'rubygems'
require 'scrapi'
require 'htmlentities'
# 天气预报
class Weather
def self.find(id,force = false)
# Deploy Rails on Ubuntu with shell
#!/bin/sh
echo "=== YTRIPTHIRD UBUNTU INSTALL ==="
cd /tmp
# Ruby
echo "=== Install Ruby library..."
sudo apt-get install g++ ruby1.8 rubygems rake irb ruby1.8-dev build-essential libopenssl-ruby libssl-dev imagemagick rdtool
echo "=== Ruby library install done."
# Rails 事务
begin
Post.transaction do
# 删除旧数据
Post.delete_all
# 例子 新数据
datas [{:title => "title1", :slug => "title-1"},{:title => "Title 2", :slug => "Title 2"}]
counter = 0
datas.each do |d|
# Rails wice_grid 5.0 插件实现跳过 default_scope 的问题
# vendor/plugins/wice_grid/lib/wice_grid.rb
# 修正 WiceGrid 与 default_scope 的形式冲突的问题,修改此插件的源代码加入了 skip_default_scope 参数(默认 true),用于让使用者可选择是否需要在 initialize_grid 方法调用的时候跳过 default_scope。因为之前有试过直接在 initialize_grid 外面加上Model.send(:with_exclusive_scope) { } 是无法跳过 default_scope 的。
# 使用方法:
# initialize_grid(Post,:per_page => 10, :skip_default_scope => false ) # 开启 default_scope
# initialize_grid(Post,:per_page => 10, :skip_default_scope => true ) # 跳过 Post 里面定义的 default_scope
# 代码:
require 'wice_grid_misc.rb'
begin
# Rails migrate 克隆表结构
class CreateArticleClones < ActiveRecord::Migration
def self.up
create_table :article_clones do |t|
# 从 Article 的 columns 里面找,注意! Article 的表结构必须在这个 Migrate 之前有创建!
Article.columns.each do |col|
next if col.name == "id"
t.send(col.type.to_sym, col.name.to_sym, :null => col.null,
:limit => col.limit, :default => col.default, :scale => col.scale,
@huacnlee
huacnlee / .vimrc
Last active September 5, 2015 14:34
Vim 配置文件 for Bash
"Vim 配置文件 for Bash
filetype indent on
syntax on
set paste
set sw=2
set ts=2
set history=100
set iskeyword+=_,$,@,%,#,-
set backspace=2
set autoindent
# RSS采集并自动发帖到某网站
require 'mechanize'
require 'simple-rss'
puts "loading exists guid list..."
# 读取已存在的guids
old_guids = []
guid_file = File.open('guids.dat',"a+")
guid_file.each_line { |line| old_guids << line.gsub("\n",'') }
# Ruby Mechanize 使用例子
# 请先安装 mechanize
# $ sudo gem install mechanize
require "mechanize"
agent = WWW:Mechanize.new
page = agent.get("http://www.douban.com/login")
form = page.forms.first
form.form_username = 'huacnlee@gmail.com'
form.form_password = '123123'
# MongoDB Service 服务脚本 /etc/init.d/mongod
#!/bin/sh
#
# init.d script with LSB support.
#
# Copyright (c) 2007 Javier Fernandez-Sanguino <jfs@debian.org>
#
# This is free software; you may redistribute it and/or modify
# it under the terms of the GNU General Public License as
# Linux 创建 SSL 证书的方法
[huacnlee@ubuntu-server]$ sudo openssl genrsa -out ca.key
Generating RSA private key, 512 bit long modulus
...........++++++++++++
.++++++++++++
e is 65537 (0x10001)
[huacnlee@ubuntu-server]$ sudo openssl req -new -key ca.key -out ca.csr
You are about to be asked to enter information that will be incorporated