Skip to content

Instantly share code, notes, and snippets.

View 3014zhangshuo's full-sized avatar
🎯
Coding

张硕 3014zhangshuo

🎯
Coding
View GitHub Profile
# in your spec helper...
require "pry"
require "redis"
require "mock_redis"
RSpec.configure do |config|
config.before(:each) do
mock_redis = MockRedis.new
allow(Redis).to receive(:new).and_return(mock_redis)
@3014zhangshuo
3014zhangshuo / header_checks
Created May 21, 2021 03:55 — forked from FUT/header_checks
Postfix email to Rails
# File: /etc/postfix/header_checks
# Add to the bottom of the file
/To:.*support@myapp.com.*/ FILTER send_to_my_app:
@3014zhangshuo
3014zhangshuo / aes-128-ecb.rb
Created April 14, 2021 06:51 — forked from tcaddy/aes-128-ecb.rb
Ruby AES-128-ECB encryption / decryption example. This was written to interface with a 3rd party that required string parameters to be encrypted the following way: Rinndael cipher, Electronic Code Block mode (ECB), no padding, encrypted buffer should be padded with spaces such that its length is divisible by 32.
# setup some input parameters
encrypted_decrypted = 'some string to encrypt or decrypt'
action = :encrypt # set to :encrypt or :decrypt
secret_key = 'abc123' # define shared secret key here
# encryption / decryption code...
cipher = OpenSSL::Cipher.new('AES-128-ECB')
if action == :decrypt
cipher.key = [secret_key].pack('H*')
cipher.padding = 0
@3014zhangshuo
3014zhangshuo / gist:6d34e2fe595e9576c9a3828ec9291643
Created March 31, 2021 02:49 — forked from edariedl/gist:a1c35eb68aebdf41876c
Bulk delete of documents from Elasticsearch in Ruby on Rails

Bulk delete of documents from Elasticsearch in Ruby on Rails

When someone deletes his account we need to delete data from the elasticsearch. All account data are removed by bulk SQL query but elasticsearch-model cannot be notified to destroy related elasticsearch documents. I had to find out how to do it. It had to be done in the background job otherwise it could take too long.

After looking around in the source code of elasticsearch ruby gems, Elastcisearch API and with a little help from Karmi (author of elasticsearch gems) I found a solution. It consists of the following three things.

@3014zhangshuo
3014zhangshuo / requesttest.rb
Created March 16, 2021 12:10 — forked from justinwoo/requesttest.rb
simple HTTP request testing using net/http and a JSON POST payload | Update: I guess I might start updating this again. I am pretty fickle.
#! usr/bin/ruby
require 'uri'
require 'net/http'
require 'json'
def getRequest(url)
uri = URI.parse(url)
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Get.new(uri.request_uri)
request["Accept"] = "application/json"
@3014zhangshuo
3014zhangshuo / spreadsheet_test
Created February 25, 2021 13:07 — forked from phollyer/spreadsheet_test
Spreadsheet Gem - updating an existing sheet without changing the output location or filename
#!/usr/bin/env ruby
require 'spreadsheet'
# Begin Test
print "Spreadsheet Test\n"
# Create the rows to be inserted
row_1 = ['A1', 'B1']
row_2 = ['A2', 'B2']
# Activate the gem you are reporting the issue against.
gem 'activerecord', if ENV['RAILS_3'] then '3.2.19' else '4.1.6' end
require 'active_record'
require 'minitest/autorun'
require 'logger'
# Ensure backward compatibility with Minitest 4
Minitest::Test = MiniTest::Unit::TestCase unless defined?(Minitest::Test)
# This connection will do for database-independent bug reports.
@3014zhangshuo
3014zhangshuo / open_api_v1.rb
Last active March 30, 2021 07:45
Udesk open api
SETTING = {
email: 'admin@udesk.cn',
api_token: 'ccd6ed3c-1da5-4173-aea3-e8a7ed74f68e',
url_base: 'https://demo.dog.tryudesk.com'
}
SETTING = {
email: 'admin@brazil.udesk.cn',
api_token: '2212c179-bc9e-433c-8cb9-ae6004e64848',
url_base: 'https://udesk-rd-bj-01.udesk.cn'
result = Hash.new { |h,k| h[k] = Hash.new(0) }
CSV.read("file_v1.csv")[1..-1].each do |row|
group_id = row[1]
black = !row[2].to_i.zero?
result[group_id][:total] += 1
result[group_id][:black] += 1 if black
end
def hi name
p 'hi ' + name
end
hi "zhangshuo"
method(:hi).call("good man")
def hi_back v, name
v.call name.upcase