Last active
August 29, 2015 14:16
-
-
Save milushov/e163512207e4354e1c99 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ─$ ruby -v | |
# ruby 2.1.2p95 (2014-05-08 revision 45877) [x86_64-darwin13.0] | |
require 'json' | |
require 'digest/md5' | |
api_token = 'ololo' | |
json = '{ "signature": "55b47124341b5bed8627499b0eb5de80", "marker": "%marker%", "host": "%server_host%", "user_ip": "%user_ip_address%", "locale": "ru", "trip_class": "Y", "passengers": { "z": 123, "adults": "1", "children": "0", "infants": "0" }, "segments": [ { "z": 123, "origin": "MOW", "destination": "LED", "date": "2015-05-25" }, { "origin": "LED", "destination": "MOW", "date": "2015-06-18" } ] }' | |
params = JSON.parse(json).sort.to_h | |
res = [] | |
params.each do |_, value| | |
if value.is_a?(Array) | |
value.each do |item| | |
res.concat(item.sort.to_h.values) | |
end | |
elsif value.is_a?(Hash) | |
res.concat(value.sort.to_h.values) | |
else | |
res.push(value) | |
end | |
end | |
res.unshift(api_token) | |
with_colons = res.join(':') | |
signature = Digest::MD5.hexdigest(with_colons) | |
puts "values separated with colons: #{with_colons}" | |
puts "signature: #{signature}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment