weixin_authorize 1.0.1 → 1.1.0

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
  SHA1:
3
- metadata.gz: df74fa58c1890b6aa3ac5d5684b3542d368d521a
4
- data.tar.gz: c296a7894161afd1bdda7240945da3529e1982cc
3
+ metadata.gz: 46bd1a765537e643f77dd3b7a112160131f031e5
4
+ data.tar.gz: 40e96c73ebc3123b461986e673e504dc530e42c6
5
5
  SHA512:
6
- metadata.gz: ba04e33192c97ce67831fe80dce94c472405aaaa670e0e70ee946dbd086a533ece2f202e852919d1c96d5233a6c31bfd0632025883a9bb57b81c1b29d695df33
7
- data.tar.gz: b6c34f7484b0839220ea0353b63749796e5950bf9eb5455d1fbecc7399f83803e5af5dc51e46f7deb7adf19826024b913fa653730b11e2b9a55e858829bfb102
6
+ metadata.gz: 72fe71c1509bcdaa2d8d5f58f325f03df41f1d996c6ce008a6bbafeb38ebe8b4261f2745fc0832cd7f49cffa4607168a14af5e5e9edea30f31cca43f414963a9
7
+ data.tar.gz: 67f3c7642241afd83736d5702c93e3f5be1d4dbccaa13c2a78b59f985de0629f09f723ef70404952ee4b6a8dcadfe0853869637be519dcdc496943983ec16aa9
data/README.md CHANGED
@@ -6,6 +6,10 @@ Add this line to your application's Gemfile:
6
6
 
7
7
  `gem 'weixin_authorize'`
8
8
 
9
+ Or
10
+
11
+ `gem 'weixin_authorize', git: "https://github.com/lanrion/weixin_authorize.git"`
12
+
9
13
  And then execute:
10
14
 
11
15
  `$ bundle`
@@ -68,7 +72,7 @@ $client ||= WeixinAuthorize::Client.new(ENV["APPID"], ENV["APPSECRET"])
68
72
 
69
73
  * [自定义菜单创建接口](http://mp.weixin.qq.com/wiki/index.php?title=自定义菜单创建接口)
70
74
 
71
- `response = $client.create_menu(menu)`
75
+ `response = $client.create_menu(menu) # Hash or Json`
72
76
 
73
77
  * [自定义菜单查询接口](http://mp.weixin.qq.com/wiki/index.php?title=自定义菜单查询接口)
74
78
 
@@ -104,7 +108,18 @@ $client ||= WeixinAuthorize::Client.new(ENV["APPID"], ENV["APPSECRET"])
104
108
 
105
109
  `$client.send_news_custom(to_user, *articles)`
106
110
 
107
- ##
111
+ ## How to test
112
+
113
+ Go to https://github.com/lanrion/weixin_authorize/issues/2, apply a weixin sandbox test account and follow this account, then add them to your `~/.bash_profile`
114
+
115
+ ```
116
+ export APPID="your test account weixin app_id"
117
+ export APPSECRET="your test account weixin appsecret"
118
+ export OPENID="your weixin openid"
119
+ ```
120
+ Last, you have to **open a new terminal tag (Reload bash_profile)** , and run `rspec .`
121
+
122
+ ## 多用户微信营销平台的对接
108
123
 
109
124
  > 对于多用户微信营销平台的对接,需要把每次的expired_at, access_token保存在Redis中,每次使用,则可以从Redis中获取expired_at和access_token, 即 `@client = WeixinAuthorize::Client.new(appid, appsecret, expired_at, access_token)`, 获取access_token,则仍然是:`@client.get_access_token`来获取.
110
125
 
@@ -14,7 +14,7 @@ module WeixinAuthorize
14
14
  # }
15
15
  def send_text_custom(to_user, content)
16
16
  message = default_options(to_user).merge({text: {content: content}})
17
- http_post(custom_base_url, MultiJson.dump(message))
17
+ http_post(custom_base_url, message)
18
18
  end
19
19
 
20
20
  # 发送图片消息
@@ -27,8 +27,8 @@ module WeixinAuthorize
27
27
  # }
28
28
  # }
29
29
  def send_image_custom(to_user, media_id)
30
- message = default_options(to_user).merge({msgtype: image, image: {media_id: media_id}})
31
- http_post(custom_base_url, MultiJson.dump(message))
30
+ message = default_options(to_user, "image").merge({image: {media_id: media_id}})
31
+ http_post(custom_base_url, message)
32
32
  end
33
33
 
34
34
  # 发送语音消息
@@ -41,8 +41,8 @@ module WeixinAuthorize
41
41
  # }
42
42
  # }
43
43
  def send_voice_custom(to_user, media_id)
44
- message = default_options(to_user).merge({msgtype: voice, voice: {media_id: media_id}})
45
- http_post(custom_base_url, MultiJson.dump(message))
44
+ message = default_options(to_user, "voice").merge({voice: {media_id: media_id}})
45
+ http_post(custom_base_url, message)
46
46
  end
47
47
 
48
48
  # 发送视频消息
@@ -56,8 +56,8 @@ module WeixinAuthorize
56
56
  # }
57
57
  def send_video_custom(to_user, media_id, options={})
58
58
  video_options = {media_id: media_id}.merge(options)
59
- message = default_options(to_user).merge({msgtype: video, video: video_options})
60
- http_post(custom_base_url, MultiJson.dump(message))
59
+ message = default_options(to_user, "video").merge({video: video_options})
60
+ http_post(custom_base_url, message)
61
61
  end
62
62
 
63
63
  # 发送音乐消息
@@ -74,10 +74,12 @@ module WeixinAuthorize
74
74
  # }
75
75
  # }
76
76
  def send_music_custom(to_user, media_id, musicurl, hqmusicurl, options={})
77
- music_options = { thumb_media_id: media_id, musicurl: musicurl,
78
- hqmusicurl: hqmusicurl}.merge(options)
79
- message = default_options(to_user).merge({msgtype: music, music: music_options})
80
- http_post(custom_base_url, MultiJson.dump(message))
77
+ music_options = { thumb_media_id: media_id,
78
+ musicurl: musicurl,
79
+ hqmusicurl: hqmusicurl
80
+ }.merge(options)
81
+ message = default_options(to_user, "music").merge({music: music_options})
82
+ http_post(custom_base_url, message)
81
83
  end
82
84
 
83
85
  # 发送图文消息
@@ -101,9 +103,9 @@ module WeixinAuthorize
101
103
  # ]
102
104
  # }
103
105
  # }
104
- def send_news_custom(to_user, *articles)
105
- message = default_options(to_user).merge({msgtype: "news", news: {articles: articles}})
106
- http_post(custom_base_url, MultiJson.dump(message))
106
+ def send_news_custom(to_user, articles=[])
107
+ message = default_options(to_user, "news").merge({news: {articles: articles}})
108
+ http_post(custom_base_url, message)
107
109
  end
108
110
 
109
111
  private
@@ -113,8 +115,8 @@ module WeixinAuthorize
113
115
  "/message/custom/send"
114
116
  end
115
117
 
116
- def default_options(to_user)
117
- {touser: to_user, msgtype: "text"}
118
+ def default_options(to_user, msgtype="text")
119
+ {touser: to_user, msgtype: msgtype}
118
120
  end
119
121
 
120
122
  end
@@ -7,8 +7,7 @@ module WeixinAuthorize
7
7
  # https://api.weixin.qq.com/cgi-bin/groups/create?access_token=ACCESS_TOKEN
8
8
  def create_group(group_name)
9
9
  create_url = "#{group_base_url}/create"
10
- group = MultiJson.dump({group: {name: group_name}})
11
- http_post(create_url, group)
10
+ http_post(create_url, {group: {name: group_name}})
12
11
  end
13
12
 
14
13
  # 查询所有分组
@@ -22,24 +21,21 @@ module WeixinAuthorize
22
21
  # https://api.weixin.qq.com/cgi-bin/groups/getid?access_token=ACCESS_TOKEN
23
22
  def get_group_for(openid)
24
23
  group_url = "#{group_base_url}/getid"
25
- openid = MultiJson.dump({openid: openid})
26
- http_post(group_url, openid)
24
+ http_post(group_url, {openid: openid})
27
25
  end
28
26
 
29
27
  # 修改分组名
30
28
  # https://api.weixin.qq.com/cgi-bin/groups/update?access_token=ACCESS_TOKEN
31
29
  def update_group_name(group_id, new_group_name)
32
30
  group_url = "#{group_base_url}/update"
33
- group = MultiJson.dump({group: {id: openid, name: new_group_name}})
34
- http_post(group_url, group)
31
+ http_post(group_url, {group: {id: group_id, name: new_group_name}})
35
32
  end
36
33
 
37
34
  # 移动用户分组
38
35
  # https://api.weixin.qq.com/cgi-bin/groups/members/update?access_token=ACCESS_TOKEN
39
36
  def update_group_for_openid(openid, to_groupid)
40
37
  group_url = "#{group_base_url}/members/update"
41
- group = MultiJson.dump({openid: openid, to_groupid: to_groupid})
42
- http_post(group_url, group)
38
+ http_post(group_url, {openid: openid, to_groupid: to_groupid})
43
39
  end
44
40
 
45
41
  private
@@ -20,6 +20,7 @@ module WeixinAuthorize
20
20
  # 自定义菜单创建接口
21
21
  # https://api.weixin.qq.com/cgi-bin/menu/create?access_token=ACCESS_TOKEN
22
22
  def create_menu(menu)
23
+ menu = MultiJson.load(menu) if menu.is_a?(String)
23
24
  create_menu_url = "#{menu_base_url}/create"
24
25
  http_post(create_menu_url, menu)
25
26
  end
@@ -15,9 +15,9 @@ module WeixinAuthorize
15
15
  # @client = WeixinAuthorize::Client.new(appid, appsecret, expired_at, access_token)
16
16
  # 获取access_token,则仍然是:@client.get_access_token 来获取
17
17
  def initialize(app_id="", app_secret="", expired_at=nil, access_token=nil)
18
- @app_id = app_id
19
- @app_secret = app_secret
20
- @expired_at = (expired_at.to_i || Time.now.to_i)
18
+ @app_id = app_id
19
+ @app_secret = app_secret
20
+ @expired_at = (expired_at.to_i || Time.now.to_i)
21
21
  @access_token = access_token
22
22
  yield self if block_given?
23
23
  end
@@ -63,7 +63,8 @@ module WeixinAuthorize
63
63
  # Refactor
64
64
  def http_post(url, options={}, endpoint="plain")
65
65
  post_api_url = endpoint_url(endpoint) + url + "?access_token=#{get_access_token}"
66
- JSON.parse(RestClient.post(post_api_url ,options))
66
+ options = MultiJson.dump(options) # to json
67
+ JSON.parse(RestClient.post(post_api_url, options))
67
68
  end
68
69
 
69
70
  def endpoint_url(endpoint)
@@ -1,3 +1,3 @@
1
1
  module WeixinAuthorize
2
- VERSION = "1.0.1"
2
+ VERSION = "1.1.0"
3
3
  end
@@ -1,8 +1,50 @@
1
1
  require "spec_helper"
2
2
 
3
3
  describe WeixinAuthorize::Api::Custom do
4
- it "can send a text Custom message" do
5
- response = $client.send_text_custom(ENV["OPENID"], "test send Custom Message")
6
- puts response
4
+ let(:text_message) do
5
+ "text Custom message"
7
6
  end
7
+
8
+ it "#send_text_custom" do
9
+ response = $client.send_text_custom(ENV["OPENID"], text_message)
10
+ expect(response["errcode"]).to eq(0)
11
+ end
12
+
13
+ it "#send_news_custom" do
14
+ articles = [{
15
+ "title" => "Happy Day",
16
+ "description" => "Is Really A Happy Day",
17
+ "url" => "http://www.baidu.com",
18
+ "picurl" => "http://www.baidu.com/img/bdlogo.gif"
19
+ },
20
+ {
21
+ "title" => "Happy Day",
22
+ "description" => "Is Really A Happy Day",
23
+ "url" => "http://www.baidu.com",
24
+ "picurl"=> "http://www.baidu.com/img/bdlogo.gif"
25
+ }]
26
+ response = $client.send_news_custom(ENV["OPENID"], articles)
27
+ expect(response["errcode"]).to eq(0)
28
+ end
29
+
30
+ it "#send_image_custom" do
31
+ pending("The test must have a media_id")
32
+ this_should_not_get_executed
33
+ end
34
+
35
+ it "#send_video_custom" do
36
+ pending("The test must have a media_id")
37
+ this_should_not_get_executed
38
+ end
39
+
40
+ it "#send_music_custom" do
41
+ pending("The test must have a media_id")
42
+ this_should_not_get_executed
43
+ end
44
+
45
+ it "#send_voice_custom" do
46
+ pending("The test must have a media_id")
47
+ this_should_not_get_executed
48
+ end
49
+
8
50
  end
@@ -1,12 +1,45 @@
1
1
  require "spec_helper"
2
2
  describe WeixinAuthorize::Api::Groups do
3
+
4
+ let(:group_name) do
5
+ "test group_name"
6
+ end
7
+
8
+ let(:group_name_2) do
9
+ "test group_name_2"
10
+ end
11
+
3
12
  it "create a group" do
4
- response = $client.create_group("test")
5
- puts response
13
+ response = $client.create_group(group_name)
14
+ expect(response["group"]["name"]).to eq(group_name)
6
15
  end
7
16
 
8
17
  it "get groups" do
9
18
  groups = $client.groups
10
- puts groups
19
+ expect(groups["groups"][-1]["name"]).to eq(group_name)
20
+ end
21
+
22
+ it "#get_group_for ENV['OPENID']" do
23
+ group = $client.get_group_for(ENV["OPENID"])
24
+ expect(group.keys).to eq(["groupid"])
25
+ end
26
+
27
+ it "#update_group_name" do
28
+ response = $client.create_group(group_name)
29
+ expect(response["group"]["name"]).to eq(group_name)
30
+ response = $client.update_group_name(response["group"]["id"], group_name_2)
31
+ expect(response["errcode"]).to eq(0)
32
+ groups = $client.groups
33
+ expect(groups["groups"][-1]["name"]).to eq(group_name_2)
11
34
  end
35
+
36
+ it "#update_group_for_openid" do
37
+ groups = $client.groups
38
+ last_group_id = groups["groups"][-1]["id"]
39
+ $client.update_group_for_openid(ENV["OPENID"], last_group_id)
40
+ group = $client.get_group_for(ENV["OPENID"])
41
+ expect(group["groupid"]).to eq(last_group_id)
42
+ $client.update_group_for_openid(ENV["OPENID"], 0)
43
+ end
44
+
12
45
  end
@@ -5,10 +5,8 @@ describe WeixinAuthorize::Api::Menu do
5
5
 
6
6
  it "can create a menu" do
7
7
  menu = '{"button":[{"type":"click","name":"今日歌曲","key":"V1001_TODAY_MUSIC"},{"type":"click","name":"歌手简介","key":"V1001_TODAY_SINGER"},{"name":"菜单","sub_button":[{"type":"view","name":"搜索","url":"http://www.soso.com/"},{"type":"view","name":"视频","url":"http://v.qq.com/"},{"type":"click","name":"赞一下我们","key":"V1001_GOOD"}]}]}'
8
- response = $client.create_menu(menu)
9
- puts response
8
+ response = $client.create_menu(MultiJson.load(menu)) # or Json string
10
9
  expect(response["errcode"]).to eq(0)
11
-
12
10
  end
13
11
 
14
12
  it "can get a weixin Menu info" do
@@ -3,11 +3,11 @@ require "spec_helper"
3
3
  describe WeixinAuthorize::Api::User do
4
4
  it "can get a weixin User info" do
5
5
  user_info = $client.user(ENV["OPENID"])
6
- puts user_info
6
+ expect(user_info["openid"]).to eq(ENV["OPENID"])
7
7
  end
8
8
 
9
9
  it "can get followers infos" do
10
10
  followers = $client.followers
11
- puts followers
11
+ expect(followers.keys).to eq(["total", "count", "data", "next_openid"])
12
12
  end
13
13
  end
data/spec/spec_helper.rb CHANGED
@@ -15,6 +15,9 @@
15
15
  # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
16
16
  require "rspec"
17
17
  require "weixin_authorize"
18
+ require "multi_json"
19
+
20
+ require "pry-rails"
18
21
 
19
22
  # $client ||= WeixinAuthorize.configure do |config|
20
23
  # config.app_id = ENV["APPID"]
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
10
10
  spec.email = ["huaitao-deng@foxmail.com"]
11
11
  spec.description = %q{weixin api authorize access_token}
12
12
  spec.summary = %q{weixin api authorize access_token}
13
- spec.homepage = ""
13
+ spec.homepage = "https://github.com/lanrion/weixin_authorize"
14
14
  spec.license = "MIT"
15
15
 
16
16
  spec.files = `git ls-files`.split($/)
@@ -19,7 +19,8 @@ Gem::Specification.new do |spec|
19
19
  spec.require_paths = ["lib"]
20
20
 
21
21
  spec.add_dependency "rest-client", ">= 1.6.7"
22
- spec.add_runtime_dependency 'multi_json' , "~> 1.7.2"
22
+ spec.add_dependency "multi_json", "~> 1.9.0"
23
+
23
24
  spec.add_development_dependency "bundler", "~> 1.3"
24
25
  spec.add_development_dependency "rake"
25
26
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: weixin_authorize
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - lanrion
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-03-16 00:00:00.000000000 Z
11
+ date: 2014-03-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rest-client
@@ -30,14 +30,14 @@ dependencies:
30
30
  requirements:
31
31
  - - ~>
32
32
  - !ruby/object:Gem::Version
33
- version: 1.7.2
33
+ version: 1.9.0
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - ~>
39
39
  - !ruby/object:Gem::Version
40
- version: 1.7.2
40
+ version: 1.9.0
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: bundler
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -107,7 +107,7 @@ files:
107
107
  - spec/api/user_spec.rb
108
108
  - spec/spec_helper.rb
109
109
  - weixin_authorize.gemspec
110
- homepage: ''
110
+ homepage: https://github.com/lanrion/weixin_authorize
111
111
  licenses:
112
112
  - MIT
113
113
  metadata: {}