wechat-utils 0.1.2 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 61592689731f9291e8b5624e642e60e2b197e8c3
4
- data.tar.gz: c0ec2ae1e25271c8fac62d4f6019d4b638071ffb
3
+ metadata.gz: 23d1191d16cd76fe621247b3b14f41dac3e94af1
4
+ data.tar.gz: 9ddbcd51489bb9cb2c4860cf7236f456b026a64e
5
5
  SHA512:
6
- metadata.gz: 592b65ec177d2fc5c833855b0700f38179467164f580509599332ef0a57a59b72718393741167c60cd8ad6d3d271ac6f32cce9a0601eb3cc3ce518b9b771a7f4
7
- data.tar.gz: 42e71f897fbf0900d7dcef5073079ea492a9295c17506c8e042d4f5191f9fcd2a844dc81ff35a13e97f653e3ab3dfa07cd223163a5b3b1ebae54dc49890184a3
6
+ metadata.gz: 7dd18f109cd1945e4c758eea30e1f985c6bb7a3d437237c02453b319681f2c3541a9a40aba35a90ab34402e90247f485d97d0a87e08583adca5062e69d3443d6
7
+ data.tar.gz: 0d2465af9e8ef57fdb22c6cb25834693cdb60ebbac6db4f674f4a86dbec42128423835baa32c8b2a044480b567e3e5b99332e3306f184c4180167c3811321bce
data/lib/wechat/utils.rb CHANGED
@@ -1,56 +1,94 @@
1
1
  require 'rest-client'
2
2
  require 'wechat/utils/version'
3
+ require 'securerandom'
4
+
3
5
 
4
6
  module Wechat
5
7
  module Utils
6
- def self.create_oauth_url_for_code app_id, redirect_url, more_info = false, state=nil
7
- common_parts = {
8
- appid: app_id,
9
- redirect_uri: CGI::escape(redirect_url),
10
- response_type: 'code',
11
- scope: more_info ? 'snsapi_userinfo' : 'snsapi_base',
12
- state: state
13
- }
14
- "https://open.weixin.qq.com/connect/oauth2/authorize?#{hash_to_query common_parts}#wechat_redirect"
15
- end
8
+ class << self
9
+ def create_oauth_url_for_code app_id, redirect_url, more_info = false, state=nil
10
+ common_parts = {
11
+ appid: app_id,
12
+ redirect_uri: CGI::escape(redirect_url),
13
+ response_type: 'code',
14
+ scope: more_info ? 'snsapi_userinfo' : 'snsapi_base',
15
+ state: state
16
+ }
17
+ "https://open.weixin.qq.com/connect/oauth2/authorize?#{hash_to_query common_parts}#wechat_redirect"
18
+ end
16
19
 
17
- def self.create_oauth_url_for_openid app_id, app_secret, code
18
- query_parts = {
19
- appid: app_id,
20
- secret: app_secret,
21
- code: code,
22
- grant_type: 'authorization_code'
23
- }
24
- "https://api.weixin.qq.com/sns/oauth2/access_token?#{hash_to_query query_parts}"
25
- end
20
+ def create_oauth_url_for_openid app_id, app_secret, code
21
+ query_parts = {
22
+ appid: app_id,
23
+ secret: app_secret,
24
+ code: code,
25
+ grant_type: 'authorization_code'
26
+ }
27
+ "https://api.weixin.qq.com/sns/oauth2/access_token?#{hash_to_query query_parts}"
28
+ end
26
29
 
27
- def self.fetch_openid app_id, app_secret, code
28
- url = create_oauth_url_for_openid app_id, app_secret, code
29
- response = get_request url
30
- if response['openid'].nil?
31
- return nil, response
32
- else
33
- return response['openid'], nil
30
+ def fetch_openid_and_access_token app_id, app_secret, code
31
+ url = create_oauth_url_for_openid app_id, app_secret, code
32
+ response = get_request url
33
+ return response['openid'], response['access_token'], response
34
34
  end
35
- end
36
35
 
37
- def self.get_request url
38
- request_opts = {
39
- :url => url,
40
- :verify_ssl => false,
41
- :ssl_version => 'TLSv1',
42
- :method => 'GET',
43
- :headers => false,
44
- :open_timeout => 30,
45
- :timeout => 30
46
- }
47
- JSON.parse RestClient::Request.execute(request_opts).body
48
- end
36
+ # access_token is get from oauth
37
+ def fetch_oauth_user_info access_token, openid
38
+ get_request "https://api.weixin.qq.com/sns/userinfo?access_token=#{access_token}&openid=#{openid}&lang=zh_CN"
39
+ end
49
40
 
50
- private
41
+ # access_token is the global token
42
+ def fetch_user_info access_token, openid
43
+ get_request "https://api.weixin.qq.com/cgi-bin/user/info?access_token=#{access_token}&openid=#{openid}&lang=zh_CN"
44
+ end
45
+
46
+ def get_request url
47
+ request_opts = {
48
+ :url => url,
49
+ :verify_ssl => false,
50
+ :ssl_version => 'TLSv1',
51
+ :method => 'GET',
52
+ :headers => false,
53
+ :open_timeout => 30,
54
+ :timeout => 30
55
+ }
56
+ JSON.parse RestClient::Request.execute(request_opts).body
57
+ end
51
58
 
52
- def self.hash_to_query hash
53
- hash.map { |k, v| "#{k}=#{v}" }.join('&')
59
+ def fetch_jsapi_ticket access_token
60
+ response = get_request "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=#{access_token}&type=jsapi"
61
+ return response['ticket'], response
62
+ end
63
+
64
+ def fetch_global_access_token appid, secret
65
+ response = get_request "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=#{appid}&secret=#{secret}"
66
+ return response['access_token'], response
67
+ end
68
+
69
+ def jsapi_params appid, url, jsapi_ticket
70
+ timestamp = Time.now.to_i
71
+ noncestr = SecureRandom.urlsafe_base64(12)
72
+ signature = sign_params timestamp: timestamp, noncestr: noncestr, jsapi_ticket: jsapi_ticket, url: url
73
+ {
74
+ appid: appid,
75
+ timestamp: timestamp,
76
+ noncestr: noncestr,
77
+ signature: signature,
78
+ url: url
79
+ }
80
+ end
81
+
82
+ private
83
+
84
+ def hash_to_query hash
85
+ hash.map { |k, v| "#{k}=#{v}" }.join('&')
86
+ end
87
+
88
+ def sign_params options
89
+ to_be_singed_string = options.sort.map { |key, value| "#{key}=#{value}" }.join("&")
90
+ Digest::SHA1.hexdigest to_be_singed_string
91
+ end
54
92
  end
55
93
  end
56
94
  end
@@ -1,5 +1,5 @@
1
1
  module Wechat
2
2
  module Utils
3
- VERSION = '0.1.2'
3
+ VERSION = '0.1.3'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wechat-utils
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Oscar Jiang