Skip to content

Instantly share code, notes, and snippets.

View zhangyuan's full-sized avatar

Yuan Zhang zhangyuan

  • Xi'an, China
  • 14:35 (UTC +08:00)
View GitHub Profile
require 'rbconfig'
module Celluloid
module CPUCounter
case RbConfig::CONFIG['host_os'][/^[A-Za-z]+/]
when 'darwin'
@cores = Integer(`sysctl hw.ncpu`[/\d+/])
when 'linux'
@cores = if File.exists?("/sys/devices/system/cpu/present")
File.read("/sys/devices/system/cpu/present").split('-').last.to_i+1
@zhangyuan
zhangyuan / console.rb
Last active December 26, 2015 20:29
scripts/console : a ruby soncole with reload! method.
#!/usr/bin/env ruby
require "active_support/dependencies"
require "irb"
root = File.expand_path('../../', __FILE__)
%w(lib).each do |path|
ActiveSupport::Dependencies.autoload_paths << "#{root}/#{path}"
end
@zhangyuan
zhangyuan / rails3_template.rb
Last active November 9, 2020 06:44
Rails Template
# encoding: utf-8
git :init
remove_file ".gitignore"
file '.gitignore', <<-IGNORE
*.rbc
*.sassc
.sass-cache
capybara-*.html
@zhangyuan
zhangyuan / .vimrc
Created March 22, 2014 02:41
.vimrc
syntax on
set nocompatible " be iMproved
filetype off " required!
set number
set rtp+=~/.vim/bundle/vundle/
call vundle#rc()
" let Vundle manage Vundle
" required!
@zhangyuan
zhangyuan / merge_images_to_pdf.rb
Created March 27, 2014 14:12
Merge multiple images to single PDF document.
require "rubygems"
require "prawn"
Prawn::Docuemtn.generate("zh.pdf") do |pdf|
pdf.image "./001.jpg", at: [0, 792], width: 612
pdf.start_new_page
pdf.image "./002.jpg". at: [0, 792], width: 612
end
@zhangyuan
zhangyuan / sendDeleteRequest.ps1
Created July 3, 2014 15:56
Send Delete Request with PowerShell 2.x
Function SendDeleteRequest($url) {
try {
$request = [Sytem.Net.WebRequest]::Create("$url")
$request.Method = "DELETE"
$request.GetResponse()
}
catch
{
Write-Host "Fail to send DELETE request to $url"
}
@zhangyuan
zhangyuan / nginx.conf
Created July 6, 2014 13:12
nginx configuration of echo-ing remote addr
server {
listen 7977;
server_name example.com;
set_real_ip_from 127.0.0.1;
real_ip_header X-Real-IP;
location /ip {
default_type text/plain;
echo "$remote_addr";
@zhangyuan
zhangyuan / proxy_ip.rb
Created July 6, 2014 13:35
Crawl HTTP Proxy IPs
#!/usr/bin/env ruby
require "mechanize"
require 'logger'
logger = Logger.new(STDOUT)
TEST_URL = "http://baidu.com"
Entry = Struct.new(:host, :port) do
attr_accessor :alive
@zhangyuan
zhangyuan / Watir_sign_in.rb
Last active August 29, 2015 14:03
Sign in website using Watir
# encoding: utf-8
require "watir"
email, password = ARGV[0], ARGV[1]
puts "email: #{email}"
puts "password: #{password}"
browser = Watir::Browser.new
browser.goto("http://zhe800.com")
@zhangyuan
zhangyuan / wechat.rb
Created July 19, 2014 02:18
Wechat Signature Validator
module Wechat
class SignatureValidator
attr_accessor :token, :params
def initialize(token, params)
@token = token
@params = params
end
def calculate