Skip to content

Instantly share code, notes, and snippets.

View pobing's full-sized avatar

Jed pobing

View GitHub Profile
@pobing
pobing / gist:48b37dcd69e408036b1f
Created October 22, 2014 03:05
clockwork use demo
#encoding: utf-8
# https://github.com/tomykaira/clockwork
# Usage:
# clockwork workers/clock_worker.rb & or
# clockworkd -c workers/clock_worker.rb run
require 'clockwork'
require File.expand_path('../../config/boot', __FILE__)
ENV['RACK_ENV'] = 'test'
@pobing
pobing / gist:98cf74905abbd350e285
Created August 19, 2014 06:49
ruby readline csv file and create new file with sth
require 'csv'
task :token do
require "./config/boot.rb"
res_file = File.new("db/token.txt","w+") # create res file
CSV.foreach("db/token.csv") do |row|
access_token = row.split(';')[0] # get first element
oauth_token = OauthToken.first(:access_token => access_token)
if oauth_token
client_id = oauth_token.client_id
@pobing
pobing / gist:f8d5662e0746bcc4c7da
Created August 15, 2014 03:47
Generate random str(slug) with ruby
def slug(length=10)
chars = ('A'..'Z').to_a + ('a'..'z').to_a + (0..9).to_a
chars.sample(length).join
end
#usage:
slug
=> "x5ZlWb4Ymf"
slug(6)
@pobing
pobing / gist:f546ecf643f9a35f613c
Created June 6, 2014 02:13
Get key value from Yaml file on different environment
# encoding: utf-8
module Setting
class << self
@@hashes = {}
# environments: development, test and production.
def environments()
%w[test production development]
@pobing
pobing / gist:3bbbb1aec3a033a8c2e3
Created June 6, 2014 02:00
sinatra download file demo
# list
get '/' do
list = Dir.glob("./files/*.*").map{|f| f.split('/').last}
# render list here
end
# upload
post '/' do
tempfile = params['file'][:tempfile]
filename = params['file'][:filename]
@pobing
pobing / gist:08e89527740124d4e77d
Created May 7, 2014 10:30
pony send mail demo
require 'pony'
mails = {:to=>"jidongdong@admaster.com.cn",
:subject=>"问答箱子找回密码",
:from=>"panel_sys@admaster.com.cn",
:html_body=>"html body",
:via=>"smtp",
:via_options=>{
:address => 'smtp.exmail.qq.com',
:port => '25',
:user_name => 'panel_sys@admaster.com.cn',
require "rubygems"
require "bundler/setup"
require "stringex"
## -- Rsync Deploy config -- ##
# Be sure your public key is listed in your server's ~/.ssh/authorized_keys file
ssh_user = "user@domain.com"
ssh_port = "22"
document_root = "~/website.com/"
rsync_delete = false
@pobing
pobing / gist:11238340
Last active July 4, 2018 10:16
ruby 使用HMAC算法得到加密字符串
#(1) 把timestamp和token联合;
# (2) 使用HMAC算法得到加密字符串(将appkey作为参数并使用SHA256 哈希方法);
# (3) 比较signature和得到的加密字符串。
timestamp = "1398236282767"
token = "THd78OwTxVGjTgCYSOxr4vkCPj3oIUg1TpZgkYpjerT2zNWdPQ"
signature = "df5fc888e86c8032f0c6b56e91ab91ebdfdae297e7a1dd4983e2e091042c151e"
data = timestamp + token
appkey = 'bl92t6tw-55zn-h14y-uzpd-y52k7jqe5s'
@pobing
pobing / gist:5537643
Created May 8, 2013 01:53
send email for many users
nletter = "...whatever..."
Newsletter.all.each do |user|
Mailer.deliver_letter(nletter, user)
end
# And your mailer function would look like:
def letter(nletter, user)
@nletter = nletter
mail(:to => user.email, :subject => @nletter.subject)
end
@pobing
pobing / nginx.conf
Created February 24, 2012 06:54 — forked from huacnlee/nginx.conf
Nginx http proxy cache to mirror of Rubygems.org
# 在本地服务器建立 rubygems.org 的镜像缓存,以提高 gem 的安装速度
# 此配置设置缓存过期为1天,也就是说,新上的 gem 无法马上安装
# 做这个起什么作用?
# rubygems 的很多资源文件是存放到 Amazon S3 上面的,由于 GFW 对某些 S3 服务器又连接重置或丢包,导致 gem 安装异常缓慢或有时候根本无法连接安装。
# 而通过这种跳板的方式可以很好的解决这个问题,当然前提是 Nginx反向代理 服务器需要在国外
proxy_cache_path /var/cache/rubygems levels=1:2 keys_zone=RUBYGEMS:10m
inactive=24h max_size=1g;
server {
listen 80;