weixin_js_sdk 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +14 -0
- data/CHANGELOG.md +7 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +62 -0
- data/Rakefile +2 -0
- data/lib/weixin_js_sdk.rb +11 -0
- data/lib/weixin_js_sdk/access_token.rb +24 -0
- data/lib/weixin_js_sdk/errors.rb +26 -0
- data/lib/weixin_js_sdk/signature.rb +27 -0
- data/lib/weixin_js_sdk/ticket.rb +22 -0
- data/lib/weixin_js_sdk/util.rb +31 -0
- data/lib/weixin_js_sdk/version.rb +3 -0
- data/weixin_js_sdk.gemspec +25 -0
- metadata +115 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 5a7c77eb5bf4e109a1ceb2cdca6e42f737978da3
|
4
|
+
data.tar.gz: c3b3100872590b03b809bd0901161e9c69d71a97
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 0b383adf3263554e16055152360c9002979d4a6c8ab6ab8e1486554ed55056410e436ad77159d5d63fc89ee8404d6ba104f424051df03701f0a4f92afc2ec897
|
7
|
+
data.tar.gz: 0ed647af36e5e34ca666742d06e800c8eff3683048ff4b9ec7212f452712526079ddbe97cbc78086f827df6c5d0c841d8556ff51de5d5d23ed669007bedb237a
|
data/.gitignore
ADDED
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 Jun Lin
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
# WeixinJsSdk
|
2
|
+
|
3
|
+
[![Code Climate](http://img.shields.io/codeclimate/github/linjunpop/weixin_js_sdk.svg)](https://codeclimate.com/github/linjunpop/weixin_js_sdk)
|
4
|
+
[![Gem Version](http://img.shields.io/gem/v/weixin_js_sdk.svg)](https://rubygems.org/gems/weixin_js_sdk)
|
5
|
+
[![License](http://img.shields.io/:license-mit-blue.svg)](http://linjunpop.mit-license.org)
|
6
|
+
|
7
|
+
a Weixin [JS-SDK](http://mp.weixin.qq.com/wiki/7/aaa137b55fb2e0456bf8dd9148dd613f.html) toolkit.
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add this line to your application's Gemfile:
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
gem 'weixin_js_sdk'
|
15
|
+
```
|
16
|
+
|
17
|
+
And then execute:
|
18
|
+
|
19
|
+
$ bundle
|
20
|
+
|
21
|
+
Or install it yourself as:
|
22
|
+
|
23
|
+
$ gem install weixin_js_sdk
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
* Fetch Access Token
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
access_token = WeixinJsSDK::AccessToken.new(
|
31
|
+
app_id: ENV['WEIXIN_APP_ID'],
|
32
|
+
app_secret: ENV['WEIXIN_APP_SECRET']
|
33
|
+
).fetch
|
34
|
+
#=> xxx
|
35
|
+
```
|
36
|
+
|
37
|
+
* Fetch Ticket
|
38
|
+
|
39
|
+
```ruby
|
40
|
+
ticket = WeixinJsSDK::Ticket.new(access_token: access_token).fetch
|
41
|
+
#=> xxx
|
42
|
+
```
|
43
|
+
|
44
|
+
* Generate Signature
|
45
|
+
|
46
|
+
```ruby
|
47
|
+
signature = WeixinJsSDK::Signature.new(
|
48
|
+
ticket: ticket,
|
49
|
+
nonce_str: 'Wm3WZYTPz0wzccnW',
|
50
|
+
timestamp: '1414587457',
|
51
|
+
url: 'http://mp.weixin.qq.com'
|
52
|
+
).sign
|
53
|
+
#=> f4d90daf4b3bca3078ab155816175ba34c443a7b
|
54
|
+
```
|
55
|
+
|
56
|
+
## Contributing
|
57
|
+
|
58
|
+
1. Fork it ( https://github.com/[my-github-username]/weixin_js_sdk/fork )
|
59
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
60
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
61
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
62
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
require "weixin_js_sdk/version"
|
2
|
+
|
3
|
+
require "weixin_js_sdk/util"
|
4
|
+
require "weixin_js_sdk/errors"
|
5
|
+
require "weixin_js_sdk/access_token"
|
6
|
+
require "weixin_js_sdk/ticket"
|
7
|
+
require "weixin_js_sdk/signature"
|
8
|
+
|
9
|
+
module WeixinJsSDK
|
10
|
+
# Your code goes here...
|
11
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module WeixinJsSDK
|
2
|
+
class AccessToken
|
3
|
+
URI_TEMPLATE = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%{app_id}&secret=%{app_secret}'.freeze
|
4
|
+
|
5
|
+
def initialize(app_id:, app_secret:)
|
6
|
+
@app_id = app_id
|
7
|
+
@app_secret = app_secret
|
8
|
+
end
|
9
|
+
|
10
|
+
def fetch
|
11
|
+
url = URI_TEMPLATE % {
|
12
|
+
app_id: @app_id,
|
13
|
+
app_secret: @app_secret
|
14
|
+
}
|
15
|
+
|
16
|
+
json = Util.get_json(url)
|
17
|
+
|
18
|
+
access_token = json['access_token']
|
19
|
+
expires_in = json['expires_in']
|
20
|
+
|
21
|
+
access_token
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module WeixinJsSDK
|
2
|
+
class Errors
|
3
|
+
Standard = Class.new(StandardError)
|
4
|
+
|
5
|
+
class InvalidRequest < Standard
|
6
|
+
DOC_URI = 'http://mp.weixin.qq.com/wiki/17/fa4e1434e57290788bde25603fa2fcbd.html'.freeze
|
7
|
+
|
8
|
+
def initialize(error_code:, message:)
|
9
|
+
@error_code = error_code
|
10
|
+
@message = message
|
11
|
+
end
|
12
|
+
|
13
|
+
def message
|
14
|
+
<<-EOS
|
15
|
+
|
16
|
+
Code: #{@error_code}
|
17
|
+
Message: #{@message}
|
18
|
+
|
19
|
+
Please read the doc: #{DOC_URI}
|
20
|
+
EOS
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
class ServerHTTPError < Standard; end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'digest/sha1'
|
2
|
+
|
3
|
+
module WeixinJsSDK
|
4
|
+
class Signature
|
5
|
+
TEMPLATE = 'jsapi_ticket=%{jsapi_ticket}&noncestr=%{nonce_str}×tamp=%{timestamp}&url=%{url}'.freeze
|
6
|
+
|
7
|
+
def initialize(ticket:, nonce_str:, timestamp:, url:)
|
8
|
+
@ticket = ticket
|
9
|
+
@nonce_str = nonce_str
|
10
|
+
@timestamp = timestamp
|
11
|
+
@url = url
|
12
|
+
end
|
13
|
+
|
14
|
+
def sign
|
15
|
+
str = TEMPLATE % {
|
16
|
+
jsapi_ticket: @ticket,
|
17
|
+
nonce_str: @nonce_str,
|
18
|
+
timestamp: @timestamp,
|
19
|
+
url: @url
|
20
|
+
}
|
21
|
+
|
22
|
+
puts str
|
23
|
+
|
24
|
+
Digest::SHA1.hexdigest(str)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module WeixinJsSDK
|
2
|
+
class Ticket
|
3
|
+
URI_TEMPLATE = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=%{access_token}&type=jsapi'.freeze
|
4
|
+
|
5
|
+
def initialize(access_token:)
|
6
|
+
@access_token = access_token
|
7
|
+
end
|
8
|
+
|
9
|
+
def fetch
|
10
|
+
url = URI_TEMPLATE % {
|
11
|
+
access_token: @access_token
|
12
|
+
}
|
13
|
+
|
14
|
+
json = Util.get_json(url)
|
15
|
+
|
16
|
+
ticket = json['ticket']
|
17
|
+
expires_in = json['expires_in']
|
18
|
+
|
19
|
+
ticket
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'json'
|
2
|
+
|
3
|
+
module WeixinJsSDK
|
4
|
+
class Util
|
5
|
+
def self.get_json(uri_str)
|
6
|
+
uri = URI(uri_str)
|
7
|
+
request = Net::HTTP::Get.new(uri)
|
8
|
+
|
9
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
10
|
+
http.use_ssl = true
|
11
|
+
|
12
|
+
response = http.request(request)
|
13
|
+
|
14
|
+
case response
|
15
|
+
when Net::HTTPSuccess then
|
16
|
+
json = JSON.parse(response.body)
|
17
|
+
|
18
|
+
if json['errcode'] && (json['errcode'] != 0)
|
19
|
+
raise Errors::InvalidRequest.new(
|
20
|
+
error_code: json['errcode'],
|
21
|
+
message: json['errmsg']
|
22
|
+
), caller
|
23
|
+
else
|
24
|
+
json
|
25
|
+
end
|
26
|
+
else
|
27
|
+
raise Errors::ServerHTTPError, response.value, caller
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'weixin_js_sdk/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "weixin_js_sdk"
|
8
|
+
spec.version = WeixinJsSdk::VERSION
|
9
|
+
spec.authors = ["Jun Lin"]
|
10
|
+
spec.email = ["linjunpop@gmail.com"]
|
11
|
+
spec.summary = %q{A Weixin JS-SDK toolkit.}
|
12
|
+
spec.description = %q{A Weixin JS-SDK toolkit.}
|
13
|
+
spec.homepage = "https://github.com/linjunpop/weixin_js_sdk"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
22
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
23
|
+
spec.add_development_dependency "pry"
|
24
|
+
spec.add_development_dependency "httplog"
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,115 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: weixin_js_sdk
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jun Lin
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-01-27 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.7'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.7'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: pry
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: httplog
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description: A Weixin JS-SDK toolkit.
|
70
|
+
email:
|
71
|
+
- linjunpop@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- CHANGELOG.md
|
78
|
+
- Gemfile
|
79
|
+
- LICENSE.txt
|
80
|
+
- README.md
|
81
|
+
- Rakefile
|
82
|
+
- lib/weixin_js_sdk.rb
|
83
|
+
- lib/weixin_js_sdk/access_token.rb
|
84
|
+
- lib/weixin_js_sdk/errors.rb
|
85
|
+
- lib/weixin_js_sdk/signature.rb
|
86
|
+
- lib/weixin_js_sdk/ticket.rb
|
87
|
+
- lib/weixin_js_sdk/util.rb
|
88
|
+
- lib/weixin_js_sdk/version.rb
|
89
|
+
- weixin_js_sdk.gemspec
|
90
|
+
homepage: https://github.com/linjunpop/weixin_js_sdk
|
91
|
+
licenses:
|
92
|
+
- MIT
|
93
|
+
metadata: {}
|
94
|
+
post_install_message:
|
95
|
+
rdoc_options: []
|
96
|
+
require_paths:
|
97
|
+
- lib
|
98
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
99
|
+
requirements:
|
100
|
+
- - ">="
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: '0'
|
103
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
104
|
+
requirements:
|
105
|
+
- - ">="
|
106
|
+
- !ruby/object:Gem::Version
|
107
|
+
version: '0'
|
108
|
+
requirements: []
|
109
|
+
rubyforge_project:
|
110
|
+
rubygems_version: 2.2.2
|
111
|
+
signing_key:
|
112
|
+
specification_version: 4
|
113
|
+
summary: A Weixin JS-SDK toolkit.
|
114
|
+
test_files: []
|
115
|
+
has_rdoc:
|