Module: WechatKit

Extended by:
WechatKit
Included in:
WechatKit
Defined in:
lib/wechat_kit.rb,
lib/wechat_kit/version.rb

Constant Summary collapse

TokenUrl =
"https://api.weixin.qq.com/cgi-bin/token"
TicketUrl =
"https://api.weixin.qq.com/cgi-bin/ticket/getticket"
VERSION =
"0.1"

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#access_tokenObject (readonly)

Returns the value of attribute access_token.



15
16
17
# File 'lib/wechat_kit.rb', line 15

def access_token
  @access_token
end

#access_token_expired_atObject (readonly)

Returns the value of attribute access_token_expired_at.



15
16
17
# File 'lib/wechat_kit.rb', line 15

def access_token_expired_at
  @access_token_expired_at
end

#app_idObject (readonly)

Returns the value of attribute app_id.



15
16
17
# File 'lib/wechat_kit.rb', line 15

def app_id
  @app_id
end

#app_secretObject (readonly)

Returns the value of attribute app_secret.



15
16
17
# File 'lib/wechat_kit.rb', line 15

def app_secret
  @app_secret
end

#jsapi_ticketObject (readonly)

Returns the value of attribute jsapi_ticket.



15
16
17
# File 'lib/wechat_kit.rb', line 15

def jsapi_ticket
  @jsapi_ticket
end

#jsapi_ticket_expired_atObject (readonly)

Returns the value of attribute jsapi_ticket_expired_at.



15
16
17
# File 'lib/wechat_kit.rb', line 15

def jsapi_ticket_expired_at
  @jsapi_ticket_expired_at
end

Instance Method Details

#get_access_tokenObject



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/wechat_kit.rb', line 41

def get_access_token
  if @access_token_expired_at and @access_token_expired_at > Time.now
    @access_token
  else
    url = "#{TokenUrl}?grant_type=client_credential&appid=#{@app_id}&secret=#{@app_secret}"
    response = response_from_url(url)
    @access_token = response["access_token"]
    @access_token_expired_at = Time.now + 7000
    @access_token
  end
end

#get_js_api_ticketObject



53
54
55
56
57
58
59
60
61
62
# File 'lib/wechat_kit.rb', line 53

def get_js_api_ticket
  if @jsapi_ticket_expired_at and @jsapi_ticket_expired_at > Time.now
    @jsapi_ticket
  else
    url = "#{TicketUrl}?type=jsapi&access_token=#{get_access_token}"
    response = response_from_url(url)
    @jsapi_ticket = response["ticket"]
    @jsapi_ticket_expired_at = Time.now + 7000
  end
end

#response_from_url(url) ⇒ Object



64
65
66
67
68
69
70
71
72
# File 'lib/wechat_kit.rb', line 64

def response_from_url(url)
  response = {}
  Timeout::timeout(3) do
    open(url) {|f|
      response = JSON.parse f.read
    }  rescue {}
  end
  response
end

#setup(app_id, app_secret) ⇒ Object



17
18
19
20
# File 'lib/wechat_kit.rb', line 17

def setup(app_id, app_secret)
  @app_id = app_id
  @app_secret = app_secret
end

#signature(url, jsApiList = []) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/wechat_kit.rb', line 22

def signature(url, jsApiList=[])
  jsapi_ticket =  get_js_api_ticket
  timestamp = Time.now.to_i.to_s
  nonce = SecureRandom.hex 16
  # 这里参数的顺序要按照 key 值 ASCII 码升序排序
  string = "jsapi_ticket=#{jsapi_ticket}&noncestr=#{nonce}&timestamp=#{timestamp}&url=#{url}"
  signature = Digest::SHA1.hexdigest(string)

  sign_package = {
    "appId"     => @app_id,
    "nonceStr"  => nonce,
    "timeStamp" => timestamp,
    "url"       => url,
    "signature" => signature,
    "jsApiList" => jsApiList
   }
  return sign_package.to_json
end