-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathutils.rb
53 lines (42 loc) · 1.16 KB
/
utils.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
require 'digest/sha1'
require 'fileutils'
module Wecheat
module Utils
extend self
def rand_openid
chars.shuffle.slice(0,28).join
end
def rand_appid
chars.shuffle.slice(0,16).unshift('wx').join.downcase
end
def rand_secret
chars.shuffle.slice(0,32).join.downcase
end
def rand_token
rand_openid + rand_openid
end
def sign_params params = {}, token
sign = Digest::SHA1.hexdigest([token, params[:timestamp], params[:nonce]].collect(&:to_s).compact.sort.join)
params.merge(signature: sign)
end
def log_received_message message
File.open(message_file, 'a'){|f| f.puts message }
File.open(message_tmp_file, 'w'){|f| f.puts message }
end
def read_received_message
msg = JSON.parse(File.open(message_tmp_file, 'r').read) rescue nil
FileUtils.rm_rf(message_tmp_file)
msg
end
private
def message_file
File.expand_path('../db/message.log', __FILE__)
end
def message_tmp_file
File.expand_path('../db/message.tmp', __FILE__)
end
def chars
@chars ||= (('a'..'z').to_a | ('A'..'Z').to_a | (0..9).to_a)
end
end
end