Class: Junziqian::Tool::RequestTool
- Inherits:
-
Object
- Object
- Junziqian::Tool::RequestTool
- Defined in:
- lib/junziqian/tool/request_tool.rb
Constant Summary collapse
- CV =
'1.1.1'- BOUNDARY =
'00content0boundary00'
Class Method Summary collapse
- .build_form_data(query_params, boundary = BOUNDARY) ⇒ Object
- .contactValues(query_params, ignores = [], content_type = 'application/x-www-form-urlencoded; charset=UTF-8') ⇒ Object
- .create_head_hash(head_map, ext_info, sign, content_type = "application/x-www-form-urlencoded; charset=UTF-8") ⇒ Object
- .create_http_sign(body_hash, timestamp) ⇒ Object
- .do_post(query_params, ignores_params, head_map, content_type = 'application/x-www-form-urlencoded; charset=UTF-8') ⇒ Object
- .do_post_by_requestObj(requestObj) ⇒ Object
- .encrypt_ext_info(ext_hash) ⇒ Object
- .get_ext_info ⇒ Object
- .get_heads(method = 'POST', version = '1.0') ⇒ Object
- .sign(query_params, ignore_params, header_map, ext_info, content_type = 'application/x-www-form-urlencoded; charset=UTF-8') ⇒ Object
Class Method Details
.build_form_data(query_params, boundary = BOUNDARY) ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/junziqian/tool/request_tool.rb', line 98 def build_form_data(query_params, boundary = BOUNDARY) body = [] query_params.each do |k, v| if v.present? body << "--"+ boundary + "\r\n" body << "Content-Disposition: form-data; name=\"#{k}\"\r\n\r\n"; if v.is_a?(Junziqian::Tool::AttacheUtils) body << v.uploadStr+"\r\n"; else body << "#{v}\r\n"; end end end if body.size > 0 body << "--"+ boundary + "--\r\n"; end body.join('') end |
.contactValues(query_params, ignores = [], content_type = 'application/x-www-form-urlencoded; charset=UTF-8') ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/junziqian/tool/request_tool.rb', line 25 def contactValues(query_params, ignores = [], content_type = 'application/x-www-form-urlencoded; charset=UTF-8') contactStr = [] is_multipart = content_type.downcase.match('multipart/form-data') ? true : false unless query_params.empty? req_array = query_params.select {|k, v| !ignores.include?(k)} req_array.sort.each do |array_item| contactStr << array_item[0] if array_item[1] contactStr << (array_item[1].is_a?(Junziqian::Tool::AttacheUtils) ? array_item[1].value : array_item[1].to_s.strip) end end end contactStr.join('') end |
.create_head_hash(head_map, ext_info, sign, content_type = "application/x-www-form-urlencoded; charset=UTF-8") ⇒ Object
63 64 65 66 67 68 69 70 71 72 |
# File 'lib/junziqian/tool/request_tool.rb', line 63 def create_head_hash(head_map, ext_info, sign, content_type="application/x-www-form-urlencoded; charset=UTF-8") hash = {} hash['ext'] = encrypt_ext_info(ext_info) hash['sign'] = sign hash['user-agent'] = 'php' hash['Content-type'] = content_type hash['accept'] = 'text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2' hash['connection'] = 'keep-alive' hash.merge(head_map) end |
.create_http_sign(body_hash, timestamp) ⇒ Object
117 118 119 120 121 122 123 |
# File 'lib/junziqian/tool/request_tool.rb', line 117 def create_http_sign body_hash, array = [contactValues(body_hash)] array << "timestamp#{timestamp}" array << "appKey#{Junziqian::Cfg::ClientInfo.app_key}" array << "appSecret#{Junziqian::Cfg::ClientInfo.app_secret}" Digest::SHA1.hexdigest(array.join('')) end |
.do_post(query_params, ignores_params, head_map, content_type = 'application/x-www-form-urlencoded; charset=UTF-8') ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/junziqian/tool/request_tool.rb', line 74 def do_post(query_params, ignores_params, head_map, content_type = 'application/x-www-form-urlencoded; charset=UTF-8') sign_value = sign(query_params, ignores_params, head_map, get_ext_info,content_type); uri = URI.parse(Cfg::ClientInfo.services_url) head_hash = create_head_hash(head_map, get_ext_info, sign_value, content_type) content = if content_type.match('multipart/form-data') head_hash = head_hash.merge({"Content-Type"=> "multipart/form-data, boundary=#{BOUNDARY}"}) build_form_data(query_params) else URI.encode_www_form query_params end head_hash['Content-Length'] = content.length.to_s http = Net::HTTP.new(uri.host, uri.port) response = http.post(uri.path, content, head_hash) return JSON.parse(response.body) end |
.do_post_by_requestObj(requestObj) ⇒ Object
90 91 92 93 94 95 |
# File 'lib/junziqian/tool/request_tool.rb', line 90 def do_post_by_requestObj( requestObj) # hash = hash.serialize_keys headerMap = get_heads(requestObj.method, requestObj.version) query_params = requestObj.query_params do_post(query_params, requestObj.ignores_params, headerMap, requestObj.content_type) end |
.encrypt_ext_info(ext_hash) ⇒ Object
52 53 54 55 56 57 58 59 60 61 |
# File 'lib/junziqian/tool/request_tool.rb', line 52 def encrypt_ext_info ext_hash contactStr = [] if ext_hash ext_hash.each do |k, v| contactStr << "#{k}\001#{v}\002" end end string = contactStr.join('') string == '' ? '' : (string[0, string.length-1].to_s) end |
.get_ext_info ⇒ Object
48 49 50 |
# File 'lib/junziqian/tool/request_tool.rb', line 48 def get_ext_info {'cv' => CV} end |
.get_heads(method = 'POST', version = '1.0') ⇒ Object
40 41 42 43 44 45 46 |
# File 'lib/junziqian/tool/request_tool.rb', line 40 def get_heads(method = 'POST', version = '1.0') {"ts" => HttpSignUtils.get_millisecond, "locale" => "zh_CN", "v" => version, "method" => method, "appKey" => Cfg::ClientInfo.app_key} end |
.sign(query_params, ignore_params, header_map, ext_info, content_type = 'application/x-www-form-urlencoded; charset=UTF-8') ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/junziqian/tool/request_tool.rb', line 13 def sign(query_params, ignore_params, header_map, ext_info, content_type = 'application/x-www-form-urlencoded; charset=UTF-8') #raise '头参数不能为空' if secret.blank? || header_map.blank? contactStr = [Cfg::ClientInfo.app_secret] contactStr << contactValues(query_params, ignore_params, content_type) contactStr << contactValues(header_map) contactStr << contactValues(ext_info) # print contactStr.join('') Digest::SHA1.hexdigest(contactStr.join('')).upcase end |