Class: SimpleWechat::JsApi
- Inherits:
-
Object
- Object
- SimpleWechat::JsApi
- Defined in:
- lib/simple_wechat/jsapi.rb
Defined Under Namespace
Classes: Ticket
Instance Method Summary collapse
- #connection ⇒ Object
- #get_config(jsapi_ticket, url, appid, api_list, options = {}) ⇒ Object
- #get_ticket(access_token) ⇒ Object
- #sign(jsapi_ticket, noncestr, timestamp, url) ⇒ Object
Instance Method Details
#connection ⇒ Object
44 45 46 47 48 49 50 |
# File 'lib/simple_wechat/jsapi.rb', line 44 def connection @connection ||= begin conn = Faraday.new do |faraday| faraday.adapter Faraday.default_adapter end end end |
#get_config(jsapi_ticket, url, appid, api_list, options = {}) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/simple_wechat/jsapi.rb', line 32 def get_config(jsapi_ticket, url, appid, api_list, = {}) = ([:timestamp] || Time.now).to_i noncestr = [:noncestr] || SecureRandom.hex(10) signature = sign(jsapi_ticket, noncestr, , url) { appId: appid, timestamp: , signature: signature, jsApiList: api_list } end |
#get_ticket(access_token) ⇒ Object
14 15 16 17 |
# File 'lib/simple_wechat/jsapi.rb', line 14 def get_ticket(access_token) response = connection.get("https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=#{access_token}&type=jsapi") Ticket.new MultiJson.load(response.body) end |
#sign(jsapi_ticket, noncestr, timestamp, url) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/simple_wechat/jsapi.rb', line 19 def sign(jsapi_ticket, noncestr, , url) params = { noncestr: noncestr, timestamp: , jsapi_ticket: jsapi_ticket, url: url } text = params.keys.sort.map {|key| "#{key}=#{params[key]}"}.join('&') Digest::SHA1.hexdigest(text) end |