Skip to content

Instantly share code, notes, and snippets.

View beepony's full-sized avatar
👻

beepony beepony

👻
View GitHub Profile
@beepony
beepony / rename.rb
Last active January 24, 2016 06:18
批量重命名的 Ruby 脚本
#! /usr/bin/ruby -w
# coding:utf-8
require 'fileutils'
Dir.glob('./*.txt').each do |f|
FileUtils.mv f, "#{File.basename(f,'.*')}.md"
end
@beepony
beepony / from_url_download_file
Last active April 28, 2016 20:02
批量 url 下载文件
#! /usr/bin/ruby -w
# coding:utf-8
require 'net/http'
require 'uri'
#str = 'http://htmljs.b0.upaiyun.com/uploads/1460943735482-f24c0e5ca2f9d61b48cdd7c215226849.jpg!bac'
def downloadurl(str)
uri = URI.parse(str)
filename = str.split('/')[-1].chomp
#! /usr/bin/ruby -w
# author:beepony
# description: md5 digest string
require 'digest'
def md5(string)
Digest::MD5.hexdigest(string.encode('utf-8'))
end
#! /usr/bin/ruby -w
# encode and decode use ruby base64 lib
# use strict model to avoid "\n" problem every 76 characteres
require 'base64'
decode_string = Base64.strict_encode64(string)
encode_string = Base64.strict_decode64(string)
#! /usr/bin/ruby
# 随机生成 7 位数字的
string = ('a'..'z').to_a.shuffle[0..7].join
require 'net/http'
image = Net::HTTP.get_response(URI.parse("http://www.site.com/file.png")).body
file = File.open("path-to-file.txt", "rb")
contents = file.read
%a - The abbreviated weekday name (Sun) 缩写的星期
%A - The full weekday name (Sunday)
%b - The abbreviated month name (Jan) 缩写的月份
%B - The full month name (January)
%d - Day of the month (01..31)
%e - Day of the month (1..31)
%H - Hour of the day, 24-hour clock (00..23)
%I - Hour of the day, 12-hour clock (01..12)
%l - Hour of the day ()
%j - Day of the year (001..366)
@beepony
beepony / ruby_ftp_example.rb
Created October 25, 2016 07:09 — forked from 3dd13/ruby_ftp_example.rb
Sample code of using Ruby Net::FTP library. Login to FTP server, list out files, check directory existence, upload files
require 'net/ftp'
CONTENT_SERVER_DOMAIN_NAME = "one-of-the-ftp-server.thought-sauce.com.hk"
CONTENT_SERVER_FTP_LOGIN = "saucy-ftp-server-login"
CONTENT_SERVER_FTP_PASSWORD = "saucy-ftp-server-password"
# LOGIN and LIST available files at default home directory
Net::FTP.open(CONTENT_SERVER_DOMAIN_NAME, CONTENT_SERVER_FTP_LOGIN, CONTENT_SERVER_FTP_PASSWORD) do |ftp|
files = ftp.list
#! /usr/bin/ruby -w
# 使用 net/http 发送 post 请求,带上自定义的 header
require 'net/http'
require 'uri'
require 'json'
uri = URI.parse("http://xxx.com/foo/bar")
# 设置请求头
@beepony
beepony / hmac-sha1.rb
Created January 6, 2017 08:12 — forked from kovacshuni/hmac-sha1.rb
Ruby HMAC-SHA1 digest creation
require 'base64'
require 'cgi'
require 'openssl'
base = 'POST&https%3A%2F%2Fapi.twitter.com%2F1%2Fstatuses%2Fupdate.json&include_entities%3Dtrue%26oauth_consumer_key%3Dxvz1evFS4wEEPTGEFPHBog%26oauth_nonce%3DkYjzVBB8Y0ZFabxSWbWovY3uYSQ2pTgmZeNu2VS4cg%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1318622958%26oauth_token%3D370773112-GmHxMAgYyLbNEtIKZeRNFsMKPR9EyMZeS9weJAEb%26oauth_version%3D1.0%26status%3DHello%2520Ladies%2520%252B%2520Gentlemen%252C%2520a%2520signed%2520OAuth%2520request%2521'
key = 'kAcSOqF21Fu85e7zjz7ZN2U4ZRhfV3WpwPAoE3Z7kBw&LswwdoUaIvS8ltyTt5jkRh4J50vUPVVHtR2YPi5kE'
puts CGI.escape(Base64.encode64("#{OpenSSL::HMAC.digest('sha1', key, base)}\n"))