wx_ext 0.1.3 → 0.1.4

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: 9bc728f6d9fa05657b9ea77a14be415b1da612e9
4
- data.tar.gz: 3c78fc17004bb1f828c9bae82faf59e989a3d635
3
+ metadata.gz: b09d3e31cfa73aba2eb5b058c17163dc97a25ffa
4
+ data.tar.gz: 11d8bb8534f855c0154099411f46d43181ed1cd2
5
5
  SHA512:
6
- metadata.gz: a9cae7ea78c641aadab551b1399ac880d994d941dcb0ffbc125e976614340d7036a5c6272cd1c571677fbc692b1d78ef6dfb42eeb1fe712c42baf55b7b78a548
7
- data.tar.gz: 0b9e47830b57aa31733e98915c9ea1d7ff4e1953b578229a6112cbcb647493841f892a810313f24ed5904905f7f0a714e858e4e6946265b92b58170dc25adb21
6
+ metadata.gz: 7b458d4545f22d2f7679e0640887c367483446765c688a5745faebf50cb4e089faf5b7765facef32251d087dae2683b5c1c21e5850be7144aedf00e124de81c5
7
+ data.tar.gz: 936ae911d89043aa9ab3765e238ead59b58b7004867b6a58f6c9f60c944f2780f9550167560b74416899a7ea496108ef39c639528f3b164e031ff4082800abb7
data/README.md CHANGED
@@ -7,7 +7,7 @@
7
7
  Add this line to your application's Gemfile:
8
8
 
9
9
  ```ruby
10
- gem 'wx_ext', '~> 0.1.3'
10
+ gem 'wx_ext'
11
11
  ```
12
12
 
13
13
  And then execute:
@@ -0,0 +1,153 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'digest'
4
+ require 'rest_client'
5
+ require 'json'
6
+ require 'wx_ext/helper'
7
+
8
+ module WxExt
9
+ module Api
10
+
11
+ # 微信扩展接口, 模拟登陆微信公众平台
12
+ # @author FuShengYang
13
+ module Base
14
+ class << self
15
+ # Get the access_token for other apis.
16
+ # http://mp.weixin.qq.com/wiki/11/0e4b294685f817b95cbed85ba5e82b8f.html
17
+ #
18
+ # @param [String] appid
19
+ # @param [String] secret
20
+ # @param [String] grant_type, default is 'client_credential'
21
+ # @return [Hash] {"access_token":"ACCESS_TOKEN","expires_in":7200}
22
+ def get_access_token(appid, secret, grant_type = 'client_credential')
23
+ url = 'https://api.weixin.qq.com/cgi-bin/token'\
24
+ "?grant_type=#{grant_type}&appid=#{appid}&secret=#{secret}"
25
+ Helper.http_get(url, { accept: :json })
26
+ end
27
+
28
+ # { "ip_list":["127.0.0.1","127.0.0.1"] }
29
+ def get_weixin_ips(access_token)
30
+ url = 'https://api.weixin.qq.com/cgi-bin/getcallbackip'\
31
+ "?access_token=#{access_token}"
32
+ Helper.http_get(url, { accept: :json })
33
+ end
34
+
35
+ def code_msg
36
+ {
37
+ -1 => '系统繁忙,此时请开发者稍候再试',
38
+ 0 => '请求成功',
39
+ 40001 => '获取access_token时AppSecret错误,或者access_token无效。请开发者认真比对AppSecret的正确性,或查看是否正在为恰当的公众号调用接口',
40
+ 40002 => '不合法的凭证类型',
41
+ 40003 => '不合法的OpenID,请开发者确认OpenID(该用户)是否已关注公众号,或是否是其他公众号的OpenID',
42
+ 40004 => '不合法的媒体文件类型',
43
+ 40005 => '不合法的文件类型',
44
+ 40006 => '不合法的文件大小',
45
+ 40007 => '不合法的媒体文件id',
46
+ 40008 => '不合法的消息类型',
47
+ 40009 => '不合法的图片文件大小',
48
+ 40010 => '不合法的语音文件大小',
49
+ 40011 => '不合法的视频文件大小',
50
+ 40012 => '不合法的缩略图文件大小',
51
+ 40013 => '不合法的AppID,请开发者检查AppID的正确性,避免异常字符,注意大小写',
52
+ 40014 => '不合法的access_token,请开发者认真比对access_token的有效性(如是否过期),或查看是否正在为恰当的公众号调用接口',
53
+ 40015 => '不合法的菜单类型',
54
+ 40016 => '不合法的按钮个数',
55
+ 40017 => '不合法的按钮个数',
56
+ 40018 => '不合法的按钮名字长度',
57
+ 40019 => '不合法的按钮KEY长度',
58
+ 40020 => '不合法的按钮URL长度',
59
+ 40021 => '不合法的菜单版本号',
60
+ 40022 => '不合法的子菜单级数',
61
+ 40023 => '不合法的子菜单按钮个数',
62
+ 40024 => '不合法的子菜单按钮类型',
63
+ 40025 => '不合法的子菜单按钮名字长度',
64
+ 40026 => '不合法的子菜单按钮KEY长度',
65
+ 40027 => '不合法的子菜单按钮URL长度',
66
+ 40028 => '不合法的自定义菜单使用用户',
67
+ 40029 => '不合法的oauth_code',
68
+ 40030 => '不合法的refresh_token',
69
+ 40031 => '不合法的openid列表',
70
+ 40032 => '不合法的openid列表长度',
71
+ 40033 => '不合法的请求字符,不能包含\uxxxx格式的字符',
72
+ 40035 => '不合法的参数',
73
+ 40038 => '不合法的请求格式',
74
+ 40039 => '不合法的URL长度',
75
+ 40050 => '不合法的分组id',
76
+ 40051 => '分组名字不合法',
77
+ 41001 => '缺少access_token参数',
78
+ 41002 => '缺少appid参数',
79
+ 41003 => '缺少refresh_token参数',
80
+ 41004 => '缺少secret参数',
81
+ 41005 => '缺少多媒体文件数据',
82
+ 41006 => '缺少media_id参数',
83
+ 41007 => '缺少子菜单数据',
84
+ 41008 => '缺少oauth code',
85
+ 41009 => '缺少openid',
86
+ 42001 => 'access_token超时,请检查access_token的有效期,请参考基础支持-获取access_token中,对access_token的详细机制说明',
87
+ 42002 => 'refresh_token超时',
88
+ 42003 => 'oauth_code超时',
89
+ 43001 => '需要GET请求',
90
+ 43002 => '需要POST请求',
91
+ 43003 => '需要HTTPS请求',
92
+ 43004 => '需要接收者关注',
93
+ 43005 => '需要好友关系',
94
+ 44001 => '多媒体文件',
95
+ 44002 => 'POST的数据包',
96
+ 44003 => '图文消息内容',
97
+ 44004 => '文本消息内容',
98
+ 45001 => '多媒体文件大小超过',
99
+ 45002 => '消息内容超过',
100
+ 45003 => '标题字段超过',
101
+ 45004 => '描述字段超过',
102
+ 45005 => '链接字段超过',
103
+ 45006 => '图片链接字段超过',
104
+ 45007 => '语音播放时间超过',
105
+ 45008 => '图文消息超过',
106
+ 45009 => '接口调用超过',
107
+ 45010 => '创建菜单个数超过',
108
+ 45015 => '回复时间超过',
109
+ 45016 => '系统分组,不允许修改',
110
+ 45017 => '分组名字过长',
111
+ 45018 => '分组数量超过上限',
112
+ 46001 => '不存在媒体数据',
113
+ 46002 => '不存在的菜单版本',
114
+ 46003 => '不存在的菜单数据',
115
+ 46004 => '不存在的用户',
116
+ 47001 => '解析JSON/XML内容错误',
117
+ 48001 => 'api功能未授权,请确认公众号已获得该接口,可以在公众平台官网-开发者中心页中查看接口权限',
118
+ 50001 => '用户未授权该api',
119
+ 61451 => '参数错误(invalid parameter)',
120
+ 61452 => '无效客服账号(invalid kf_account)',
121
+ 61453 => '客服帐号已存在(kf_account exsited)',
122
+ 61454 => '客服帐号名长度超过(仅允许10个英文字符,不包括@及@后的公众号的微信号)(invalid kf_acount length)',
123
+ 61455 => '客服帐号名包含非法字符(仅允许英文+数字)(illegal character in kf_account)',
124
+ 61456 => '客服帐号个数超过(10个客服账号)(kf_account count exceeded)',
125
+ 61457 => '无效头像文件类型(invalid file type)',
126
+ 61450 => '系统错误(system error)',
127
+ 61500 => '日期格式错误',
128
+ 61501 => '日期范围错误'
129
+ }
130
+ end
131
+
132
+ # Upload media to weixin.
133
+ # http://mp.weixin.qq.com/wiki/10/78b15308b053286e2a66b33f0f0f5fb6.html
134
+ #
135
+ # @param [String] access_token
136
+ # @param [String] type, 媒体文件类型,分别有图片(image)、语音(voice)、视频(video)和缩略图(thumb)
137
+ # @param [String] media, form-data中媒体文件标识,有filename、filelength、content-type等信息
138
+ # @return [Hash] {"type":"TYPE","media_id":"MEDIA_ID","created_at":123456789}
139
+ def upload_media(access_token, type, media)
140
+ url = 'http://file.api.weixin.qq.com/cgi-bin/media/upload'\
141
+ "?access_token=#{access_token}&type=#{type}"
142
+ Helper.http_post(url, {file: media})
143
+ end
144
+
145
+ def download_media(access_token, media_id)
146
+ url = 'http://file.api.weixin.qq.com/cgi-bin/media/get'\
147
+ "?access_token=#{access_token}&media_id=#{media_id}"
148
+ RestClient.get url
149
+ end
150
+ end
151
+ end
152
+ end
153
+ end
@@ -0,0 +1,50 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'digest'
4
+ require 'rest_client'
5
+ require 'json'
6
+
7
+ module WxExt
8
+ module Api
9
+
10
+ # 微信扩展接口, 模拟登陆微信公众平台
11
+ # @author FuShengYang
12
+ module Js
13
+ class << self
14
+ def get_jsapi_ticket(access_token)
15
+ url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=#{access_token}&type=jsapi"
16
+ Helper.http_get(url, { accept: :json })
17
+ end
18
+
19
+ def get_jsapi_config(access_token, url, app_id)
20
+ config_hash = {}
21
+ jsapi_ticket_hash = get_jsapi_ticket(access_token)
22
+ timestamp = set_timestamp
23
+ noncestr = set_noncestr
24
+ if jsapi_ticket_hash['errcode'] == 0
25
+ jsapi_ticket = jsapi_ticket_hash['ticket']
26
+ str = "jsapi_ticket=#{jsapi_ticket}&noncestr=#{noncestr}&timestamp=#{timestamp}&url=#{url}"
27
+ signature = Digest::SHA1.hexdigest(str)
28
+ config_hash = {
29
+ app_id: app_id,
30
+ timestamp: timestamp,
31
+ noncestr: noncestr,
32
+ signature: signature
33
+ }
34
+ end
35
+ config_hash
36
+ end
37
+
38
+ private
39
+
40
+ def set_noncestr
41
+ [*'a'..'z',*'0'..'9',*'A'..'Z'].sample(16).join
42
+ end
43
+
44
+ def set_timestamp
45
+ Time.now.to_i.to_s
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,31 @@
1
+ # encoding: UTF-8
2
+
3
+ module WxExt
4
+ module Api
5
+
6
+ # 微信扩展接口, 模拟登陆微信公众平台
7
+ # @author FuShengYang
8
+ module Menu
9
+ class << self
10
+
11
+ def create_menu(access_token, menu_hash)
12
+ url = 'https://api.weixin.qq.com/cgi-bin/menu/create'\
13
+ "?access_token=#{access_token}"
14
+ Helper.http_post(url, menu_hash)
15
+ end
16
+
17
+ def menus(access_token)
18
+ url = 'https://api.weixin.qq.com/cgi-bin/menu/get'\
19
+ "?access_token=#{access_token}"
20
+ Helper.http_get(url, { accept: :json })
21
+ end
22
+
23
+ def del_menu(access_token)
24
+ url = 'https://api.weixin.qq.com/cgi-bin/menu/delete'\
25
+ "?access_token=#{access_token}"
26
+ Helper.http_get(url, { accept: :json })
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,20 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'digest'
4
+
5
+ module WxExt
6
+ module Api
7
+
8
+ # 微信扩展接口, 模拟登陆微信公众平台
9
+ # @author FuShengYang
10
+ module Msg
11
+ class << self
12
+
13
+ def check_signature(signature, timestamp, nonce, token)
14
+ array = [token, timestamp, nonce].sort
15
+ signature == Digest::SHA1.hexdigest(array.join) ? true : false
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,46 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'rest_client'
4
+
5
+ module WxExt
6
+ module Api
7
+
8
+ # 微信扩展接口, 模拟登陆微信公众平台
9
+ # @author FuShengYang
10
+ module User
11
+ module Group
12
+ class << self
13
+ def create_group(access_token, group_name)
14
+ url = 'https://api.weixin.qq.com/cgi-bin/groups/create'\
15
+ "?access_token=#{access_token}"
16
+ Helper.http_post(url, { group: { name: group_name } }.to_json)
17
+ end
18
+
19
+ def groups(access_token)
20
+ url = 'https://api.weixin.qq.com/cgi-bin/groups/get'\
21
+ "?access_token=#{access_token}"
22
+ Helper.http_get(url, { accept: :json })
23
+ end
24
+
25
+ def user_of_group(access_token, openid)
26
+ url = 'https://api.weixin.qq.com/cgi-bin/groups/getid'\
27
+ "?access_token=#{access_token}"
28
+ Helper.http_post(url, { openid: openid })
29
+ end
30
+
31
+ def update_group_name(access_token, id, name)
32
+ url = 'https://api.weixin.qq.com/cgi-bin/groups/update'\
33
+ "?access_token=#{access_token}"
34
+ Helper.http_post(url, { group: { id: id, name: name } })
35
+ end
36
+
37
+ def mv_user_group(access_token, openid, to_groupid)
38
+ url = 'https://api.weixin.qq.com/cgi-bin/groups/members/update'\
39
+ "?access_token=#{access_token}"
40
+ Helper.http_post(url, { openid: openid, to_groupid: to_groupid })
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,47 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'digest'
4
+ require 'rest_client'
5
+ require 'json'
6
+ require 'wx_ext/api/user/group'
7
+
8
+ module WxExt
9
+ module Api
10
+
11
+ # 微信扩展接口, 模拟登陆微信公众平台
12
+ # @author FuShengYang
13
+ module User
14
+ class << self
15
+ def change_remark(access_token, openid, remark) end
16
+
17
+ def get_user_base_info(access_token, openid, lang) end
18
+
19
+ def user_list(access_token, next_openid) end
20
+
21
+ def get_oauth2_token_with_code(app_id, app_secret, code, grant_type='authorization_code')
22
+ url = 'https://api.weixin.qq.com/sns/oauth2/access_token'\
23
+ "?appid=#{app_id}&secret=#{app_secret}&code=CODE&grant_type=#{grant_type}"
24
+ Helper.http_get(url, { accept: :json })
25
+ end
26
+
27
+ def refresh_oauth2_token(app_id, refresh_token, grant_type='refresh_token')
28
+ url = 'https://api.weixin.qq.com/sns/oauth2/refresh_token'\
29
+ "?appid=#{app_id}&grant_type=#{grant_type}&refresh_token=#{refresh_token}"
30
+ Helper.http_get(url, { accept: :json })
31
+ end
32
+
33
+ def get_user_info_with_snsapi_userinfo(oauth2_token, openid, lang)
34
+ url = 'https://api.weixin.qq.com/sns/userinfo'\
35
+ "?access_token=#{oauth2_token}&#{openid}=OPENID&lang=#{lang}"
36
+ Helper.http_get(url, { accept: :json })
37
+ end
38
+
39
+ def check_oauth2_token(openid, oauth2_token)
40
+ url = 'https://api.weixin.qq.com/sns/auth'\
41
+ "?access_token=#{oauth2_token}&openid=#{openid}"
42
+ Helper.http_get(url, { accept: :json })
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
data/lib/wx_ext/api.rb ADDED
@@ -0,0 +1,11 @@
1
+ # encoding: UTF-8
2
+
3
+ #require 'wx_ext/api/qrcode'
4
+ #require 'wx_ext/api/media'
5
+ #require 'wx_ext/api/oauth'
6
+
7
+ require 'wx_ext/api/base'
8
+ require 'wx_ext/api/js'
9
+ require 'wx_ext/api/msg'
10
+ require 'wx_ext/api/user'
11
+ require 'wx_ext/api/menu'
@@ -0,0 +1,22 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'rest_client'
4
+ require 'json'
5
+
6
+ module WxExt
7
+
8
+ # @author FuShengYang
9
+ module Helper
10
+ class << self
11
+ def http_get(url, headers = {})
12
+ res = RestClient.get url, headers
13
+ JSON.parse res
14
+ end
15
+
16
+ def http_post(url, params, headers = {})
17
+ res = RestClient.post url, params, headers
18
+ JSON.parse res
19
+ end
20
+ end
21
+ end
22
+ end
@@ -1,9 +1,13 @@
1
1
  # encoding: UTF-8
2
+
2
3
  require 'nokogiri'
3
4
  require 'rest_client'
4
5
  require 'json'
5
6
  require 'open-uri'
7
+
6
8
  module WxExt
9
+ # 微信扩展接口, 从搜狗微信抓取微信文章
10
+ # @author FuShengYang
7
11
  class SougouWeixin
8
12
  def self.spider_posts_from_sougou(openid, page_index = 1, date_last = (Time.now - 3600 * 24 * 10).strftime("%Y-%m-%d"))
9
13
  json_url = "http://weixin.sogou.com/gzhjs?&openid=#{openid}&page=#{page_index}"
@@ -1,4 +1,4 @@
1
1
  # encoding: UTF-8
2
2
  module WxExt
3
- VERSION = '0.1.3'
3
+ VERSION = '0.1.4'
4
4
  end