wechat-access_token 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
+ SHA1:
3
+ metadata.gz: 98bae0e0895f35d06b5fab8f330b6b0e137f83bf
4
+ data.tar.gz: bac8d94fcf7f0f6454ef8778dffd06cec75a2728
5
+ SHA512:
6
+ metadata.gz: bea6f74889064beba3fb725e4f2580f82c3eb9e54aeec93f51d4c262df6dee60aabedf4a30169ed450074cf02beb805ef7c738bcc1c85af5ae2b343b867035e2
7
+ data.tar.gz: 4d7e12266ad95dabb4ea749618ef1216febc60ea118b6157455bf78beb9b130637d9fa3f9740baf6ef9f272580253d3330ff871167472835a8fee93aa44b3a2d
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2020 beslow
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,38 @@
1
+ [![Build Status](http://img.shields.io/travis/hashie/hashie.svg)](https://travis-ci.org/beslow/wechat-access_token)
2
+ # Wechat::AccessToken
3
+ 微信公众号 access_token获取
4
+
5
+ ## Usage
6
+ After Installation,
7
+ genearte wechat.yml and redis.yml and config them
8
+ ```bash
9
+ rails generate wechat_install_config
10
+ ```
11
+
12
+ then you can get access_token by
13
+ ```ruby
14
+ Wechat::AccessToken.current
15
+ ```
16
+
17
+ ## Installation
18
+ Add this line to your application's Gemfile:
19
+
20
+ ```ruby
21
+ gem 'wechat-access_token'
22
+ ```
23
+
24
+ And then execute:
25
+ ```bash
26
+ $ bundle
27
+ ```
28
+
29
+ Or install it yourself as:
30
+ ```bash
31
+ $ gem install wechat-access_token
32
+ ```
33
+
34
+ ## Contributing
35
+ Contribution directions go here.
36
+
37
+ ## License
38
+ 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,27 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'Wechat::AccessToken'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.md')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ require 'bundler/gem_tasks'
18
+
19
+ require 'rake/testtask'
20
+
21
+ Rake::TestTask.new(:test) do |t|
22
+ t.libs << 'test'
23
+ t.pattern = 'test/**/*_test.rb'
24
+ t.verbose = false
25
+ end
26
+
27
+ task default: :test
@@ -0,0 +1,16 @@
1
+ # encoding: UTF-8
2
+
3
+ defaults: &defaults
4
+ host: localhost
5
+ password:
6
+ port: 6379
7
+ db: 88
8
+
9
+ development:
10
+ <<: *defaults
11
+
12
+ test:
13
+ <<: *defaults
14
+
15
+ production:
16
+ <<: *defaults
@@ -0,0 +1,5 @@
1
+ # encoding: UTF-8
2
+
3
+ base:
4
+ appid: 123
5
+ appsecret: 456
@@ -0,0 +1,10 @@
1
+ class WechatInstallConfigGenerator < Rails::Generators::Base
2
+ source_root File.expand_path("../templates", __FILE__)
3
+
4
+ desc "This generator creates an wechat and redis config file at config/"
5
+
6
+ def copy_initializer_file
7
+ copy_file "redis.yml", "config/redis.yml"
8
+ copy_file "wechat.yml", "config/wechat.yml"
9
+ end
10
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :wechat_access_token do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,40 @@
1
+ require "rails"
2
+ require "hashie"
3
+ require 'redis'
4
+ require "rest-client"
5
+
6
+ require "wechat/access_token/railtie"
7
+ require "wechat/config"
8
+ module Wechat
9
+ module AccessToken
10
+ def self.key
11
+ 'wechat-access-token'
12
+ end
13
+
14
+ def self.current
15
+ Wechat.redis.get(key) || get_new
16
+ end
17
+
18
+ def self.request
19
+ url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=#{Wechat.config.base.appid}&secret=#{Wechat.config.base.appsecret}"
20
+ response = ::RestClient.get(url)
21
+ JSON.parse(response)
22
+ end
23
+
24
+ def self.get_new
25
+ response = request
26
+
27
+ code = response['errcode']
28
+
29
+ raise StandardError, response['errmsg'] if [-1, 40001, 40164, 40002, 40013].include?(code)
30
+
31
+ token = response['access_token']
32
+ if token.present?
33
+ Wechat.redis.set(key, token, ex: response['expires_in'].to_i - 5 * 60)
34
+ token
35
+ else
36
+ result
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,6 @@
1
+ module Wechat
2
+ module AccessToken
3
+ class Railtie < ::Rails::Railtie
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,5 @@
1
+ module Wechat
2
+ module AccessToken
3
+ VERSION = '0.1.1'
4
+ end
5
+ end
@@ -0,0 +1,49 @@
1
+ module Wechat
2
+ def self.config
3
+ @config ||= begin
4
+ path = ::Rails.root.join("config/wechat.yml")
5
+
6
+ raise StandardError, '请先在config文件夹下配置wechat.yml' unless File.exists?(path)
7
+
8
+ result = YAML::load(ERB.new(IO.read(path)).result) rescue {}
9
+
10
+ result = Hashie::Mash.new(result)
11
+
12
+ if result.base.nil? || result.base.appid.nil? || result.base.appsecret.nil?
13
+ raise StandardError, "wechat.yml文件配置错误"
14
+ end
15
+
16
+ result
17
+ end
18
+ end
19
+
20
+ def self.redis
21
+ @redis ||= begin
22
+ path = ::Rails.root.join("config/redis.yml")
23
+
24
+ raise StandardError, '请先在config文件夹下配置redis.yml' unless File.exists?(path)
25
+
26
+ result = YAML::load(ERB.new(IO.read(path)).result) #rescue {}
27
+
28
+ result = result[Rails.env]
29
+
30
+ raise StandardError, "redis.yml文件配置错误" if result.nil?
31
+
32
+ result = Hashie::Mash.new(result)
33
+
34
+ if result.host.nil? || result.port.nil? || result.wechat_db.nil?
35
+ raise StandardError, "redis.yml文件配置错误"
36
+ end
37
+
38
+ Redis.new(host: result.host, password: result.password, port: result.port, db: result.wechat_db)
39
+ end
40
+ end
41
+
42
+ def self.clear_redis
43
+ @redis = nil
44
+ end
45
+
46
+ def self.clear_config
47
+ @config = nil
48
+ end
49
+ end
metadata ADDED
@@ -0,0 +1,123 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: wechat-access_token
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - beslow
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-02-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 5.2.4
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 5.2.4.1
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: 5.2.4
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 5.2.4.1
33
+ - !ruby/object:Gem::Dependency
34
+ name: hashie
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '4.1'
40
+ type: :runtime
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '4.1'
47
+ - !ruby/object:Gem::Dependency
48
+ name: redis
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '4.1'
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: 4.1.3
57
+ type: :runtime
58
+ prerelease: false
59
+ version_requirements: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - "~>"
62
+ - !ruby/object:Gem::Version
63
+ version: '4.1'
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: 4.1.3
67
+ - !ruby/object:Gem::Dependency
68
+ name: rest-client
69
+ requirement: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - "~>"
72
+ - !ruby/object:Gem::Version
73
+ version: '2.1'
74
+ type: :runtime
75
+ prerelease: false
76
+ version_requirements: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - "~>"
79
+ - !ruby/object:Gem::Version
80
+ version: '2.1'
81
+ description: 微信公众号开发,通过简单配置,获取access_token。
82
+ email:
83
+ - 549174542@qq.com
84
+ executables: []
85
+ extensions: []
86
+ extra_rdoc_files: []
87
+ files:
88
+ - MIT-LICENSE
89
+ - README.md
90
+ - Rakefile
91
+ - lib/generators/wechat_install_config/templates/redis.yml
92
+ - lib/generators/wechat_install_config/templates/wechat.yml
93
+ - lib/generators/wechat_install_config/wechat_install_config_generator.rb
94
+ - lib/tasks/wechat/access_token_tasks.rake
95
+ - lib/wechat/access_token.rb
96
+ - lib/wechat/access_token/railtie.rb
97
+ - lib/wechat/access_token/version.rb
98
+ - lib/wechat/config.rb
99
+ homepage: https://github.com/beslow/wechat-access_token
100
+ licenses:
101
+ - MIT
102
+ metadata: {}
103
+ post_install_message:
104
+ rdoc_options: []
105
+ require_paths:
106
+ - lib
107
+ required_ruby_version: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ required_rubygems_version: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ requirements: []
118
+ rubyforge_project:
119
+ rubygems_version: 2.5.1
120
+ signing_key:
121
+ specification_version: 4
122
+ summary: 微信公众号开发,获取access_token
123
+ test_files: []