Skip to content

Instantly share code, notes, and snippets.

View caok's full-sized avatar

Clark caok

View GitHub Profile
@caok
caok / gist:8049246
Last active December 31, 2015 21:49
Action Pack Variants
class ApplicationController < ActionController::Base
before_action :detect_device_variant
private
def detect_device_variant
case request.user_agent
when /iPad/i
request.variant = :tablet
when /iPhone/i
@caok
caok / gist:8018311
Created December 18, 2013 06:55
图片不存在时,显示一个默认的图片
<img src="abc.JPG" onerror="this.src='default.JPG'" />
或者
<script type="text/javascript">
t = document.getElementsByClassName("defaultImg");
for(i = 0; i < t.length; i++){
t.item(i).onerror = function(){
this.src = "test.gif"
app/views/layouts/_async_javascript.html.erb
<script type="text/javascript">
(function() {
var script = document.createElement('script');
script.type = 'text/javascript';
script.async = true;
script.src = '<%= j javascript_path(path) %>';
var other = document.getElementsByTagName('script')[0];
other.parentNode.insertBefore(script, other);
#!/usr/bin/env bash
# Pre-requisites
sudo apt-get -y update
sudo apt-get -y install git-core curl autoconf automake openssl vim build-essential libc6-dev libreadline6 libreadline6-dev zlib1g zlib1g-dev libssl-dev libyaml-dev mysql-server libmysqlclient-dev ncurses-dev libtool bison libxslt1-dev libxml2-dev
# rbenv
curl https://raw.github.com/fesplugas/rbenv-installer/master/bin/rbenv-installer | bash
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile
echo 'eval "$(rbenv init -)"' >> ~/.bash_profile
@caok
caok / 实现div的上下左右都居中
Created November 19, 2013 08:32
其实解决的思路是这样的:首们需要position:absolute;绝对定位。而层的定位点,使用外补丁margin负值的方法。负值的大小为层自身宽度高度除以二。
<style type="text/css">
<!--
div {
position:absolute;
top:50%;
left:50%;
margin:-150px 0 0 -200px;
width:400px;
height:300px;
border:1px solid #008800;
@caok
caok / gist:6067486
Created July 24, 2013 01:30
ruby 匹配 @ 艾特用户名的方法
# coding: utf-8
#正则用于匹配用户名
str = "@wikimo @中國 @水手 测试看看"
#{2,20}字符长度至少2个,不多于20个,以下任意方式匹配
#arr = str.scan(/@([一-龠\w]{2,20}\s)/u).flatten
arr = str.scan(/@([\p{Han}+\w]{2,20}\s)/u).flatten
前端:https://github.com/ichord/jquery-atwho-rails
@caok
caok / Client Side Certificate Auth in Nginx
Created July 11, 2013 02:40
Client Side Certificate Auth in Nginx
Creating and Signing Your Certs
This is SSL, so you'll need an cert-key pair for you/the server, the api users/the client and a CA pair. You will be the CA in this case (usually a role played by VeriSign, thawte, GoDaddy, etc.), signing your client's certs. There are plenty of tutorials out there on creating and signing certificates, so I'll leave the details on this to someone else and just quickly show a sample here to give a complete tutorial. NOTE: This is just a quick sample of creating certs and not intended for production.
# Create the CA Key and Certificate for signing Client Certs
openssl genrsa -des3 -out ca.key 4096
openssl req -new -x509 -days 365 -key ca.key -out ca.crt
# Create the Server Key, CSR, and Certificate
openssl genrsa -des3 -out server.key 1024
openssl req -new -key server.key -out server.csr
@caok
caok / Node.js, Socket.io And SSL
Last active December 19, 2015 14:49
Node.js, Socket.io And SSL
// Load libraries
var https = require('https');
var fs = require('fs');
var socketio = require('socket.io');
// The server options
var svrPort = 8888; // This is the port of service
var svrOptions = {
key: fs.readFileSync('certs/server.key'),
cert: fs.readFileSync('certs/server.crt'),
通过json访问新浪ip数据库,可以获得相应城市的ip地址。
例行惯例,不解释:
<script type="text/javascript" >
$(document).ready(function(){
jQuery.getScript("http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js", function(){
alert("province : "+remote_ip_info.province+", city : "+remote_ip_info.city);
});
});
@caok
caok / gist:5692040
Created June 1, 2013 23:21
下载网站文档
wget -m -p -E -k -K -np http://twitter.github.io/bootstrap/