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
#! /usr/bin/ruby -w
# 使用 net/http 发送 post 请求,带上自定义的 header
require 'net/http'
require 'uri'
require 'json'
uri = URI.parse("http://xxx.com/foo/bar")
# 设置请求头
%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)
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
@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
@beepony
beepony / ruby-ftp-get-all-files.rb
Last active January 24, 2023 17:09
ruby ftp list all files
require 'net/ftp'
def scan(ftp, dir)
ftp.chdir(dir)
puts ftp.pwd + "/."
entries = ftp.list('*')
entries.each do |entry|
if entry.split(/\s+/)[0][0,1] == "d" then
scan(ftp, entry.split.last)
else