Class: Junziqian::Tool::HttpSignUtils

Inherits:
Object
  • Object
show all
Defined in:
lib/junziqian/tool/http_sign_utils.rb

Class Method Summary collapse

Class Method Details

.create_http_sign(body_params, timestamp = nil) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/junziqian/tool/http_sign_utils.rb', line 21

def create_http_sign body_params, timestamp = nil
  timestamp ||= get_millisecond
  contactStr = []
  contactStr << get_params(body_params)
  contactStr << ("timestamp"+timestamp)
  contactStr << ("appKey"+ Cfg::ClientInfo.app_key)
  contactStr << ("appSecret"+ Cfg::ClientInfo.app_secret;)
  Digest::SHA1.hexdigest(contactStr.join(''));
end

.create_http_url(body_params, timestamp = nil) ⇒ Object



7
8
9
10
11
12
13
14
15
# File 'lib/junziqian/tool/http_sign_utils.rb', line 7

def create_http_url body_params, timestamp = nil
  timestamp ||= get_millisecond
  sign=create_http_sign(body_params, timestamp)
  url='timestamp='+timestamp+'&'; #timestamp必须放在最前面民
  body_params.each do |k, v|
    url += k.to_s + URI::escape(v.strip)
  end
  url += "sign=#{sign}"
end

.get_millisecondObject



17
18
19
# File 'lib/junziqian/tool/http_sign_utils.rb', line 17

def get_millisecond
  (Time.now.to_f*1000).to_i.to_s
end

.get_params(hash) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/junziqian/tool/http_sign_utils.rb', line 31

def get_params(hash)
  contactStr=[]
  if hash.present?
    hash.sort.map {|array_item|
      contactStr << [array_item[0], array_item[1].strip]
    }
  end
  contactStr.flatten.join('')
end