wx_miniprogram 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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 064db8150b4061181cd1038c2cfd978cf0060c184935d3ed7e8b0ee1778e9dc9
4
+ data.tar.gz: 7904e1b8be6c13a33e622b0b573bdfc62877e472aacc2d6af989fcff9c58222c
5
+ SHA512:
6
+ metadata.gz: e80780210e4800a5447656731144a6190720c3ef080fb3ed8c1eeee7d7cba1941bcc304a2d4910a6e198112b8b1bb13bf77c58ebd9b525398ccf9df5aafe3ccb
7
+ data.tar.gz: 769e0613135d2c601ef317c97d906cd81c769769ff4ba767c8369b8974a842ce0c79c42085742c240f8f2c5311d43e3a07fa1aaa23abaf27cc7f8d706a04f656
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 ranjiayu
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,58 @@
1
+ # WxMiniprogram
2
+
3
+ Ruby微信小程序服务端API
4
+
5
+ ## Installation
6
+
7
+
8
+
9
+ ## Usage
10
+
11
+ 所有接口方法以!结尾的方法调用失败抛出ApiError异常。正常方法成功返回Response Hash实例,失败返回false。
12
+
13
+ ```ruby
14
+ client = WxMiniprogram::Client.new("appid", "secret")
15
+
16
+ # Token
17
+ # 获取Token
18
+ client.get_access_token!
19
+ # 获取稳定版Token
20
+ client.get_stable_access_token!
21
+ # 刷新Token
22
+ client.refresh_access_token!
23
+
24
+ # 用户信息
25
+ # 获取插件用户openpid
26
+ client.get_plugin_open_pid("openid")
27
+
28
+ # 检查加密信息
29
+ client.check_encrypted_data("encoded_str")
30
+
31
+ # 支付后获取 Unionid
32
+ client.get_paid_unionid({
33
+ :openid => "openid",
34
+ :transaction_id => "transaction_id",
35
+ :mch_id => "mch_id",
36
+ :out_trade_no => "out_trade_no"
37
+ })
38
+
39
+ ...
40
+
41
+ ```
42
+ ## Test
43
+
44
+ Set environment variables WX_APPID and WX_SECRET and run tests.
45
+
46
+ 设置环境变量 WX_APPID 和 WX_SECRET
47
+
48
+ ```shell
49
+ rspec
50
+ ```
51
+
52
+ ## Contributing
53
+
54
+ Bug reports and pull requests are welcome on GitHub at https://github.com/ranjiayu/wx_miniprogram.
55
+
56
+ ## License
57
+
58
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
@@ -0,0 +1,9 @@
1
+ module WxMiniprogram
2
+ class ApiError < StandardError
3
+
4
+ end
5
+
6
+ class Error < StandardError
7
+
8
+ end
9
+ end
@@ -0,0 +1,108 @@
1
+ # frozen_string_literal: true
2
+
3
+ module WxMiniprogram
4
+ class Client
5
+ attr_accessor :debug
6
+ include Util
7
+ include Token
8
+ include OpenApi
9
+ include Login
10
+ include UserInfo
11
+
12
+
13
+ def initialize(appid, secret)
14
+ @url = "https://api.weixin.qq.com/"
15
+ @appid = appid
16
+ @secret = secret
17
+ @access_token = ""
18
+ @expire = nil
19
+ @debug = false
20
+ end
21
+
22
+ def refresh_token!
23
+ get_access_token!
24
+ end
25
+
26
+ def method_missing(method, *args)
27
+ method_str = method.to_s
28
+ unless method_str.end_with? "!"
29
+ origin_method = (method_str + "!").to_sym
30
+ if self.respond_to? origin_method
31
+ begin
32
+ send(origin_method, args)
33
+ rescue ApiError => e
34
+ puts "[debug]: #{e}" if @debug
35
+ false
36
+ end
37
+ else
38
+ raise Error, "undefined method #{method.to_s}"
39
+ end
40
+ else
41
+ unless self.respond_to? method
42
+ raise Error, "undefined method #{method.to_s}"
43
+ end
44
+ end
45
+ end
46
+
47
+ private
48
+ def get(path, body: nil, query: nil, need_access_token: false)
49
+ need_access_token && valid
50
+ resp = request(path, body: body, query: query, method: "get")
51
+ if !resp["errcode"].nil? && resp["errcode"] != 0
52
+ raise ApiError, resp["errmsg"]
53
+ end
54
+ resp
55
+ end
56
+
57
+ def post(path, body: nil, query: nil, need_access_token: false)
58
+ need_access_token && valid
59
+ resp = request(path, body: body, query: query, method: "post")
60
+ if !resp["errcode"].nil? && resp["errcode"] != 0
61
+ raise ApiError, resp["errmsg"]
62
+ end
63
+ resp
64
+ end
65
+
66
+ # This method is from [gist-api-ruby](https://github.com/gistplatform/gist-api-ruby)
67
+ def request(path, body: nil, query: nil, method: "get")
68
+ unless query.nil?
69
+ path += "?#{URI::encode_www_form(query)}"
70
+ end
71
+ @uri = URI(@url + path)
72
+ request = send("http_#{method}").new(@uri.request_uri, {
73
+ "Content-Type" => "application/json"
74
+ })
75
+ request.body = body.to_json unless body.nil?
76
+ http = Net::HTTP.new(@uri.host, @uri.port)
77
+ http.use_ssl = true
78
+ http.read_timeout = 5
79
+ response = http.request(request)
80
+ raise ApiError, response.body unless response.is_a?(Net::HTTPSuccess)
81
+ json_response = JSON.parse(response.body)
82
+ return json_response
83
+ end
84
+
85
+ def http_get
86
+ Net::HTTP::Get
87
+ end
88
+
89
+ def http_post
90
+ Net::HTTP::Post
91
+ end
92
+
93
+ def http_patch
94
+ Net::HTTP::Patch
95
+ end
96
+
97
+ def http_delete
98
+ Net::HTTP::Delete
99
+ end
100
+
101
+ def valid
102
+ if @access_token.empty?
103
+ raise ApiError, "You must invoke 'get_access_token' method at first"
104
+ end
105
+ end
106
+
107
+ end
108
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ module WxMiniprogram
4
+ module Login
5
+ def code2session!(js_code)
6
+ get("sns/jscode2session", query: {
7
+ :appid => @appid,
8
+ :secret => @secret,
9
+ :js_code => js_code,
10
+ :grant_type => "authorization_code",
11
+ })
12
+ end
13
+
14
+ def check_session_key!(openid, session_key)
15
+ get("wxa/checksession", query: {
16
+ :access_token => @access_token,
17
+ :openid => openid,
18
+ :signature => hmac_sha256(session_key, ""),
19
+ :sigmethod => "hmac_sha256"
20
+ })
21
+ end
22
+
23
+ def reset_user_session_key!(openid, session_key)
24
+ get("wxa/resetusersessionkey", query: {
25
+ :access_token => @access_token
26
+ }, body: {
27
+ :openid => openid,
28
+ :signature => hmac_sha256(session_key, ""),
29
+ :sigmethod => "hmac_sha256"
30
+ })
31
+ end
32
+
33
+ end
34
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ module WxMiniprogram
4
+ module OpenApi
5
+ def clear_quota!
6
+ post("cgi-bin/clear_quota",
7
+ body: {:appid => @appid},
8
+ query: {:access_token => @access_token}, need_access_token: true
9
+ )
10
+ end
11
+
12
+ def get_api_quota
13
+ post("cgi-bin/openapi/quota/get",
14
+ query: {:access_token => @access_token}, need_access_token: true)
15
+ end
16
+
17
+ def get_rid_info(rid)
18
+ post("cgi-bin/openapi/rid/get",
19
+ body: {:rid => rid},
20
+ query: {:access_token => @access_token}, need_access_token: true)
21
+ end
22
+
23
+ def clear_quota_by_app_secret(appid, appsecret)
24
+ post("cgi-bin/clear_quota/v2",
25
+ body: {:appid => appid, :appsecret => appsecret})
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ module WxMiniprogram
4
+ module Token
5
+ def get_access_token!
6
+ resp = get("cgi-bin/token", query: {
7
+ :grant_type => "client_credential",
8
+ :appid => @appid,
9
+ :secret => @secret
10
+ })
11
+ unless resp["access_token"].nil?
12
+ @access_token = resp["access_token"]
13
+ @expire = resp["expires_in"] + Time.now.to_i
14
+ end
15
+ resp
16
+ end
17
+
18
+ def get_stable_access_token!(force_refresh=false)
19
+ resp = post("cgi-bin/stable_token", body: {
20
+ :grant_type => "client_credential",
21
+ :appid => @appid,
22
+ :secret => @secret,
23
+ :force_refresh => force_refresh
24
+ })
25
+ unless resp["access_token"].nil?
26
+ @access_token = resp["access_token"]
27
+ @expire = resp["expires_in"] + Time.now.to_i
28
+ end
29
+ resp
30
+ end
31
+
32
+ def expire
33
+ @expire
34
+ end
35
+
36
+ def expired?
37
+ Time.now.to_i >= @expire
38
+ end
39
+
40
+ end
41
+ end
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ module WxMiniprogram
4
+ module UserInfo
5
+
6
+ def get_plugin_open_pid!(code)
7
+ post("wxa/getpluginopenpid", query: {
8
+ :access_token => @access_token
9
+ }, body: {
10
+ :code => code
11
+ })
12
+ end
13
+
14
+ def check_encrypted_data!(str)
15
+ post("wxa/business/checkencryptedmsg", query: {
16
+ :access_token => @access_token
17
+ }, body: {
18
+ :encrypted_msg_hash => str
19
+ })
20
+ end
21
+
22
+ def get_paid_unionid!(options)
23
+ get("wxa/getpaidunionid", query: {
24
+ :access_token => @access_token
25
+ }, body: options)
26
+ end
27
+
28
+ def get_user_encryptKey!(openid, session_key)
29
+ get("wxa/business", query: {
30
+ :access_token => @access_token,
31
+ :openid => openid,
32
+ :signature => hmac_sha256(session_key, ""),
33
+ :sig_method => "hmac_sha256"
34
+ })
35
+ end
36
+
37
+ def get_phone_number!(code, openid)
38
+ post("wxa/business/getuserphonenumber", query: {
39
+ :access_token => @access_token
40
+ }, body: {
41
+ :code => code,
42
+ :openid => openid
43
+ })
44
+ end
45
+
46
+ end
47
+ end
@@ -0,0 +1,7 @@
1
+ module WxMiniprogram
2
+ module Util
3
+ def hmac_sha256(data, key)
4
+ OpenSSL::HMAC.hexdigest("SHA256", key, data)
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module WxMiniprogram
4
+ VERSION = "0.1.3"
5
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+ require_relative "wx_miniprogram/version"
3
+ require_relative "wx_miniprogram/api_error"
4
+ require_relative "wx_miniprogram/util"
5
+ require_relative "wx_miniprogram/token"
6
+ require_relative "wx_miniprogram/open_api"
7
+ require_relative "wx_miniprogram/user_info"
8
+ require_relative "wx_miniprogram/login"
9
+ require_relative "wx_miniprogram/client"
10
+ require "net/http"
11
+ require "openssl"
12
+ require "json"
13
+
14
+ module WxMiniprogram
15
+
16
+ end
metadata ADDED
@@ -0,0 +1,59 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: wx_miniprogram
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.3
5
+ platform: ruby
6
+ authors:
7
+ - ranjiayu
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-07-03 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: wechat mini-program api client
14
+ email:
15
+ - ranjiayu@outlook.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".rspec"
21
+ - LICENSE.txt
22
+ - README.md
23
+ - Rakefile
24
+ - lib/wx_miniprogram.rb
25
+ - lib/wx_miniprogram/api_error.rb
26
+ - lib/wx_miniprogram/client.rb
27
+ - lib/wx_miniprogram/login.rb
28
+ - lib/wx_miniprogram/open_api.rb
29
+ - lib/wx_miniprogram/token.rb
30
+ - lib/wx_miniprogram/user_info.rb
31
+ - lib/wx_miniprogram/util.rb
32
+ - lib/wx_miniprogram/version.rb
33
+ homepage: https://ranjiayu.github.io
34
+ licenses:
35
+ - MIT
36
+ metadata:
37
+ homepage_uri: https://ranjiayu.github.io
38
+ source_code_uri: https://github.com/ranjiayu/wx_miniprogram_api
39
+ changelog_uri: https://github.com/ranjiayu/wx_miniprogram_api/CHANGELOG
40
+ post_install_message:
41
+ rdoc_options: []
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: 3.0.0
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ requirements: []
55
+ rubygems_version: 3.5.9
56
+ signing_key:
57
+ specification_version: 4
58
+ summary: wechat mini-program api client
59
+ test_files: []