wmp_sdk 0.1.1

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: b418c33be5371b2cf93a1de0a7e60ee5aac5d12bf90a0da3d4a1b9e6852e7686
4
+ data.tar.gz: c731c5799783ccb173d06da526572b25a452b3ecdb839f96442240259f188fff
5
+ SHA512:
6
+ metadata.gz: ce973ad623941fe021d64a01c23d43a107f82ab7ea200bdef992ee08d76888bc7198ee363dddd4289223189c26328f335d2f58c5879e53b080bd645c47a6efe8
7
+ data.tar.gz: 136a665b62d9d3c753a2c019b4a86bb71e136317c2f38d7949360fe8dfc0094d8d996d6fbf57a459b25c4a1592bee8f1be6d74c4e1a1df4ee048baea1ba8b2c7
data/.gitignore ADDED
@@ -0,0 +1,13 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+ .idea
13
+ *.gem
data/.rakeTasks ADDED
@@ -0,0 +1,7 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <Settings><!--This file was automatically generated by Ruby plugin.
3
+ You are allowed to:
4
+ 1. Remove rake task
5
+ 2. Add existing rake tasks
6
+ To add existing rake tasks automatically delete this file and reload the project.
7
+ --><RakeGroup description="" fullCmd="" taksId="rake" /></Settings>
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.5.8
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.5.8
6
+ before_install: gem install bundler -v 2.1.4
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at vincent4502237@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [https://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: https://contributor-covenant.org
74
+ [version]: https://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in wmp_sdk.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
7
+ gem "rspec", "~> 3.0"
8
+ gem 'faraday', '~> 1.0', '>= 1.0.1'
data/Gemfile.lock ADDED
@@ -0,0 +1,38 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ wmp_sdk (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.4.4)
10
+ faraday (1.0.1)
11
+ multipart-post (>= 1.2, < 3)
12
+ multipart-post (2.1.1)
13
+ rake (12.3.3)
14
+ rspec (3.9.0)
15
+ rspec-core (~> 3.9.0)
16
+ rspec-expectations (~> 3.9.0)
17
+ rspec-mocks (~> 3.9.0)
18
+ rspec-core (3.9.2)
19
+ rspec-support (~> 3.9.3)
20
+ rspec-expectations (3.9.2)
21
+ diff-lcs (>= 1.2.0, < 2.0)
22
+ rspec-support (~> 3.9.0)
23
+ rspec-mocks (3.9.1)
24
+ diff-lcs (>= 1.2.0, < 2.0)
25
+ rspec-support (~> 3.9.0)
26
+ rspec-support (3.9.3)
27
+
28
+ PLATFORMS
29
+ ruby
30
+
31
+ DEPENDENCIES
32
+ faraday (~> 1.0, >= 1.0.1)
33
+ rake (~> 12.0)
34
+ rspec (~> 3.0)
35
+ wmp_sdk!
36
+
37
+ BUNDLED WITH
38
+ 2.1.4
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 JiangYongKang
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,51 @@
1
+ # WmpSdk
2
+
3
+ 适用于微信小程序服务端的快速开发SDK
4
+
5
+ ## 如何安装
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'wmp_sdk'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle install
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install wmp_sdk
20
+
21
+ ## Usage
22
+
23
+ ```ruby
24
+ WmpSdk.configure do |config|
25
+ config.app_id = ''
26
+ config.secret = ''
27
+ config.miniprogram_state = ''
28
+ config.token = ''
29
+ config.encoding_aes_key = ''
30
+ end
31
+ WmpSdk::Api.invoke_access_token
32
+ ```
33
+
34
+ ## Development
35
+
36
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
37
+
38
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
39
+
40
+ ## Contributing
41
+
42
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/wmp_sdk. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/wmp_sdk/blob/master/CODE_OF_CONDUCT.md).
43
+
44
+
45
+ ## License
46
+
47
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
48
+
49
+ ## Code of Conduct
50
+
51
+ Everyone interacting in the WmpSdk project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/wmp_sdk/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "wmp_sdk"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,95 @@
1
+ require 'faraday'
2
+ require 'json'
3
+ require 'wmp_sdk/error'
4
+
5
+ module WmpSdk
6
+ class Api
7
+
8
+ # 登录凭证校验。通过 wx.login 接口获得临时登录凭证 code 后传到开发者服务器调用此接口完成登录流程。
9
+ def self.invoke_auth_session(code)
10
+ response = Faraday.get('https://api.weixin.qq.com/sns/jscode2session', {
11
+ appid: WmpSdk.configuration.app_id,
12
+ secret: WmpSdk.configuration.secret,
13
+ js_code: code,
14
+ grant_type: :authorization_code,
15
+ })
16
+ raise WmpServerError, "Response Status: #{response.status}" unless response.success?
17
+ JSON.parse(response.body, symbolize_names: true)
18
+ end
19
+
20
+ # 获取小程序全局唯一后台接口调用凭据(access_token)。调用绝大多数后台接口时都需使用 access_token,开发者需要进行妥善保存。
21
+ def self.invoke_access_token
22
+ response = Faraday.get('https://api.weixin.qq.com/cgi-bin/token', {
23
+ grant_type: :client_credential,
24
+ appid: WmpSdk.configuration.app_id,
25
+ secret: WmpSdk.configuration.secret,
26
+ })
27
+ raise WmpServerError, "Response Status: #{response.status}" unless response.success?
28
+ JSON.parse(response.body, symbolize_names: true)
29
+ end
30
+
31
+ # 下发小程序和公众号统一的服务消息
32
+ def self.invoke_uniform_message(access_token, openid, mp_template_msg, weapp_template_msg = nil)
33
+ response = Faraday.post('https://api.weixin.qq.com/cgi-bin/message/wxopen/template/uniform_send', { access_token: access_token }) do |request|
34
+ request_body = {
35
+ touser: openid,
36
+ mp_template_msg: mp_template_msg,
37
+ }
38
+ request_body.merge!(weapp_template_msg: weapp_template_msg) if weapp_template_msg
39
+ request.body = request_body.to_json
40
+ end
41
+ raise WmpServerError, "Response Status: #{response.status}" unless response.success?
42
+ JSON.parse(response.body, symbolize_names: true)
43
+ end
44
+
45
+ # 发送订阅消息
46
+ def self.invoke_subscribe_message_send(access_token, openid, template_id, page, data, miniprogram_state = 'formal', lang = 'zh_CN')
47
+ response = Faraday.post('https://api.weixin.qq.com/cgi-bin/message/subscribe/send', { access_token: access_token }) do |request|
48
+ request.body = {
49
+ touser: openid,
50
+ template_id: template_id,
51
+ page: page,
52
+ data: data,
53
+ miniprogram_state: miniprogram_state,
54
+ lang: lang,
55
+ }.to_json
56
+ end
57
+ raise WmpServerError, "Response Status: #{response.status}" unless response.success?
58
+ JSON.parse(response.body, symbolize_names: true)
59
+ end
60
+
61
+ # 把媒体文件上传到微信服务器。目前仅支持图片。用于发送客服消息或被动回复用户消息。
62
+ def self.invoke_upload_temp_media(access_token, file_name)
63
+ connection = Faraday.new('https://api.weixin.qq.com') do |conn|
64
+ conn.request :multipart
65
+ conn.request :url_encoded
66
+ conn.adapter :net_http
67
+ end
68
+ file = Faraday::FilePart.new Rails.root.join(file_name).to_s, 'image/jpg'
69
+ response = connection.post("/cgi-bin/media/upload?access_token=#{access_token}&type=image", { media: file })
70
+ raise WmpServerError, "Response Status: #{response.status}" unless response.success?
71
+ JSON.parse(response.body, symbolize_names: true)
72
+ end
73
+
74
+ # 发送客服消息给用户
75
+ def self.send_customer_service_message(access_token, openid, message_type, options = {})
76
+ response = Faraday.post('https://api.weixin.qq.com/cgi-bin/message/custom/send', { access_token: access_token }) do |request|
77
+ request.body = {
78
+ touser: openid,
79
+ msgtype: message_type,
80
+ }.merge!(options).to_json
81
+ end
82
+ raise WmpServerError, "Response Status: #{response.status}" unless response.success?
83
+ JSON.parse(response.body, symbolize_names: true)
84
+ end
85
+
86
+ # 消息推送验证
87
+ def self.validate_customer_service_message(signature, timestamp, nonce)
88
+ Digest::SHA1.hexdigest(
89
+ [WmpSdk.configuration.token, timestamp, nonce].sort.join
90
+ ) == signature
91
+ end
92
+
93
+
94
+ end
95
+ end
@@ -0,0 +1,5 @@
1
+ module WmpSdk
2
+ class Configuration
3
+ attr_accessor :app_id, :secret, :miniprogram_state, :token, :encoding_aes_key
4
+ end
5
+ end
@@ -0,0 +1,3 @@
1
+ module WmpSdk
2
+ VERSION = '0.1.1'
3
+ end
data/lib/wmp_sdk.rb ADDED
@@ -0,0 +1,20 @@
1
+ require 'wmp_sdk/version'
2
+ require 'wmp_sdk/api'
3
+ require 'wmp_sdk/configuration'
4
+
5
+ module WmpSdk
6
+
7
+ class << self
8
+ attr_writer :configuration
9
+
10
+ def configuration
11
+ @configuration ||= Configuration.new
12
+ end
13
+
14
+ def configure
15
+ yield configuration
16
+ end
17
+ end
18
+
19
+
20
+ end
data/wmp_sdk.gemspec ADDED
@@ -0,0 +1,27 @@
1
+ require_relative 'lib/wmp_sdk/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = 'wmp_sdk'
5
+ spec.version = WmpSdk::VERSION
6
+ spec.authors = ['JiangYongKang']
7
+ spec.email = ['vincent4502237@gmail.com']
8
+
9
+ spec.summary = 'wechat miniprogram sdk for ruby'
10
+ spec.description = 'a ruby program to facilitate accessing wechat miniprogram'
11
+ spec.homepage = 'https://github.com/JiangYongKang/wmp_sdk'
12
+ spec.license = 'MIT'
13
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
14
+
15
+ spec.metadata['homepage_uri'] = spec.homepage
16
+ spec.metadata['source_code_uri'] = 'https://github.com/JiangYongKang/wmp_sdk'
17
+ spec.metadata['changelog_uri'] = 'https://github.com/JiangYongKang/wmp_sdk'
18
+
19
+ # Specify which files should be added to the gem when it is released.
20
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
21
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
22
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
23
+ end
24
+ spec.bindir = 'exe'
25
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
26
+ spec.require_paths = ['lib']
27
+ end
metadata ADDED
@@ -0,0 +1,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: wmp_sdk
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - JiangYongKang
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-09-14 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: a ruby program to facilitate accessing wechat miniprogram
14
+ email:
15
+ - vincent4502237@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".gitignore"
21
+ - ".rakeTasks"
22
+ - ".rspec"
23
+ - ".ruby-version"
24
+ - ".travis.yml"
25
+ - CODE_OF_CONDUCT.md
26
+ - Gemfile
27
+ - Gemfile.lock
28
+ - LICENSE.txt
29
+ - README.md
30
+ - Rakefile
31
+ - bin/console
32
+ - bin/setup
33
+ - lib/wmp_sdk.rb
34
+ - lib/wmp_sdk/api.rb
35
+ - lib/wmp_sdk/configuration.rb
36
+ - lib/wmp_sdk/version.rb
37
+ - wmp_sdk.gemspec
38
+ homepage: https://github.com/JiangYongKang/wmp_sdk
39
+ licenses:
40
+ - MIT
41
+ metadata:
42
+ homepage_uri: https://github.com/JiangYongKang/wmp_sdk
43
+ source_code_uri: https://github.com/JiangYongKang/wmp_sdk
44
+ changelog_uri: https://github.com/JiangYongKang/wmp_sdk
45
+ post_install_message:
46
+ rdoc_options: []
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: 2.3.0
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ requirements: []
60
+ rubyforge_project:
61
+ rubygems_version: 2.7.6.2
62
+ signing_key:
63
+ specification_version: 4
64
+ summary: wechat miniprogram sdk for ruby
65
+ test_files: []