Skip to content

Instantly share code, notes, and snippets.

@he9qi
Last active December 25, 2015 21:09
Show Gist options
  • Save he9qi/7040174 to your computer and use it in GitHub Desktop.
Save he9qi/7040174 to your computer and use it in GitHub Desktop.
require 'digest/sha1'
require 'httparty'
require 'uri'
serverUrl = "http://api.dianping.com/"
apiPath = "v1/business/find_businesses"
appkey = "xxxxxxxxx"
secret = "xxxxxxxxxxxxxxxxxxxxxxxxxx"
param = {}
param["format"] = "json"
param["city"] = "上海"
param["latitude"] = "31.21524"
param["longitude"] = "121.420033"
param["category"] = "美食"
param["region"] = "长宁区"
param["limit"] = "20"
param["radius"] = "2000"
param["offset_type"] = "2"
param["has_coupon"] = "1"
param["has_deal"] = "1"
param["keyword"] = "泰国菜"
param["sort"] = "7"
array = []
# puts params in array and sort them
for key, value in param
array << key
end
array.sort!
# build string for sha1
paramArray = []
paramArray.push appkey
for key in array
paramArray.push key + param[key]
end
paramArray.push secret
shaSource = paramArray.join("")
# encrypt using sha1
sign = Digest::SHA1.hexdigest shaSource
# build query string
queryArray = []
queryArray << ("appkey=" + appkey)
queryArray << ("sign=" + sign)
for key, value in param
queryArray << (key + "=" + value)
end
queryString = queryArray.join("&")
# request url
url = serverUrl + apiPath + "?" + queryString
res = HTTParty.get URI.escape(url)
puts res
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment