easy_wechat 0.1.0 → 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7364b15bfaffdfbd425ee3da00578995dc16dbddabc922e51bd369766524a311
4
- data.tar.gz: 8b356a8c3e57f5ce4e1a9d1f16b8a30d751476428f763aae57681b18ad8db2bb
3
+ metadata.gz: 0003f928b22948ff59d5f02c2bd5cacd621a74e29d5de77475b367da5e18a56e
4
+ data.tar.gz: de689a155969d77a44a368e58f0df45043cef9c4e1482d409afbdc9890e8b478
5
5
  SHA512:
6
- metadata.gz: 709aa701cff058d7ccf341442d27fab92c6840d160da6f79e30cf5115896fcc1952067b3cdc22fab4b9e87bb9ebcbe85c20f4c9d522b7fc2f4775b478c7dfc81
7
- data.tar.gz: b9715e4797ed0fed1b6b8d5bffcd36d943affd89157cb22296f8d173d21aa3a652e5f28fefc5b384df66e53ceb5831950c93e93a98a4c506285dfbd46e11610e
6
+ metadata.gz: 0c98054b4db81e4b8081a64d05d6cd65983332779a02b7d653011ea5079589ccf0d30bc11062511e78d7ce7c693cdbac5ffd9ea133a0ae057fb89736dd8675af
7
+ data.tar.gz: 680a7b40a98a414b1191f4ceb50d5301511ae30672c0f396a3ef0e18b00f994fa8e38ce58599258c5aa377e8abac458fcb37c7ec33404aad6504366484f93b13
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- easy_wechat (0.1.0)
4
+ easy_wechat (0.1.1)
5
5
  activesupport
6
6
  httpx
7
7
 
data/README.md CHANGED
@@ -22,7 +22,12 @@ Or install it yourself as:
22
22
 
23
23
  ## Usage
24
24
 
25
- TODO: Write usage instructions here
25
+ ```
26
+ EasyWechat.configure do |config|
27
+ config.appid = ENV.fetch("APPID") || ""
28
+ config.app_secret = ENV.fetch("APP_SECRET") || ""
29
+ end
30
+ ```
26
31
 
27
32
  ## Development
28
33
 
data/easy_wechat.gemspec CHANGED
@@ -6,8 +6,8 @@ Gem::Specification.new do |spec|
6
6
  spec.authors = ["42up"]
7
7
  spec.email = ["foobar@v2up.com"]
8
8
 
9
- spec.summary = "easy wechat sdk"
10
- spec.description = "easy wechat sdk"
9
+ spec.summary = "An unofficial easy wechat sdk"
10
+ spec.description = "An unofficial easy wechat sdk"
11
11
  spec.homepage = "https://github.com/42up/easy_wechat"
12
12
  spec.license = "MIT"
13
13
  spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
data/lib/easy_wechat.rb CHANGED
@@ -1,7 +1,32 @@
1
1
  require "easy_wechat/version"
2
+ require "easy_wechat/client"
3
+ require "easy_wechat/configuration"
4
+
5
+ require "httpx"
2
6
 
3
7
  module EasyWechat
4
8
  class Error < StandardError; end
5
- # Your code goes here...
6
- puts "foo"
9
+ def self.configuration
10
+ @configuration ||= Configuration.new
11
+ end
12
+
13
+ class << self
14
+ attr_writer :configuration
15
+
16
+ def configuration
17
+ @configuration ||= Configuration.new
18
+ end
19
+
20
+ def configure
21
+ yield(configuration)
22
+ end
23
+
24
+ def app_id
25
+ configuration.app_id
26
+ end
27
+
28
+ def app_secret
29
+ configuration.app_secret
30
+ end
31
+ end
7
32
  end
@@ -0,0 +1,77 @@
1
+ require "httpx"
2
+ require "http/form_data"
3
+ require "json"
4
+ require "active_support"
5
+
6
+ module EasyWechat
7
+ class Client
8
+ TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token"
9
+ BATCHGET_MATERIAL_URL = "https://api.weixin.qq.com/cgi-bin/material/batchget_material"
10
+ UPLOADIMG_URL = "https://api.weixin.qq.com/cgi-bin/media/uploadimg"
11
+ ADD_NEWS_URL = "https://api.weixin.qq.com/cgi-bin/material/add_news"
12
+
13
+ def initialize
14
+ end
15
+
16
+ def access_token
17
+ @access_token ||= token["access_token"]
18
+ end
19
+
20
+ # 获取access_token
21
+ # https://developers.weixin.qq.com/doc/offiaccount/Basic_Information/Get_access_token.html
22
+ def token
23
+ resp = HTTPX.get(TOKEN_URL, params: {
24
+ appid: EasyWechat.app_id,
25
+ secret: EasyWechat.app_secret,
26
+ grant_type: "client_credential",
27
+ })
28
+
29
+ r = ::JSON.parse(resp.body, quirks_mode: true)
30
+ yield r if block_given?
31
+ r
32
+ end
33
+
34
+ # 上传图文消息内的图片获取URL
35
+ # https://developers.weixin.qq.com/doc/offiaccount/Asset_Management/Adding_Permanent_Assets.html
36
+ def uploadimg(media)
37
+ # https://honeyryderchuck.gitlab.io/httpx/wiki/Multipart-Uploads.html#notes
38
+ file = if media.is_a?(String) ? HTTP::FormData::File.new(media) : meida
39
+
40
+ resp = HTTPX.plugin(:multipart).post(UPLOADIMG_URL, params: { access_token: access_token }, form: { media: file })
41
+
42
+ r = ::JSON.parse(resp.body, quirks_mode: true)
43
+ yield r if block_given?
44
+ r
45
+ end
46
+
47
+ # 获取永久素材的列表
48
+ # https://developers.weixin.qq.com/doc/offiaccount/Asset_Management/Get_materials_list.html
49
+ def batchget_material(type = "image", offset = 0, count = 20)
50
+ # TODO: 检查参数完整性
51
+ # TODO: 检查参数合法性
52
+ payload = { type: type, offset: offset, count: count }
53
+
54
+ resp = HTTPX.post(BATCHGET_MATERIAL_URL, params: { access_token: access_token }, json: payload)
55
+
56
+ r = ::JSON.parse(resp.body, quirks_mode: true)
57
+ yield r if block_given?
58
+ r
59
+ end
60
+
61
+ # 新增永久图文素材
62
+ # https://developers.weixin.qq.com/doc/offiaccount/Asset_Management/Adding_Permanent_Assets.html
63
+ def add_news(articles)
64
+ payload = { articles: articles }
65
+
66
+ resp = HTTPX.post(ADD_NEWS_URL, params: { access_token: access_token }, json: payload)
67
+
68
+ r = ::JSON.parse(resp.body, quirks_mode: true)
69
+ yield r if block_given?
70
+ r
71
+ end
72
+
73
+ def menu_create
74
+ raise NotImplementedError
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,10 @@
1
+ module EasyWechat
2
+ class Configuration
3
+ attr_accessor :app_id, :app_secret
4
+
5
+ def initialize
6
+ @app_id = ""
7
+ @app_secret = ""
8
+ end
9
+ end
10
+ end
@@ -1,3 +1,3 @@
1
1
  module EasyWechat
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: easy_wechat
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - 42up
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-06-28 00:00:00.000000000 Z
11
+ date: 2021-06-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httpx
@@ -94,7 +94,7 @@ dependencies:
94
94
  - - ">="
95
95
  - !ruby/object:Gem::Version
96
96
  version: '0'
97
- description: easy wechat sdk
97
+ description: An unofficial easy wechat sdk
98
98
  email:
99
99
  - foobar@v2up.com
100
100
  executables: []
@@ -113,6 +113,8 @@ files:
113
113
  - bin/setup
114
114
  - easy_wechat.gemspec
115
115
  - lib/easy_wechat.rb
116
+ - lib/easy_wechat/client.rb
117
+ - lib/easy_wechat/configuration.rb
116
118
  - lib/easy_wechat/version.rb
117
119
  homepage: https://github.com/42up/easy_wechat
118
120
  licenses:
@@ -139,5 +141,5 @@ requirements: []
139
141
  rubygems_version: 3.1.4
140
142
  signing_key:
141
143
  specification_version: 4
142
- summary: easy wechat sdk
144
+ summary: An unofficial easy wechat sdk
143
145
  test_files: []