lanxin_open 0.0.5 → 0.0.6

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,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- ZTFmODQyN2ExNzIwZWQyZTEwZjE3NDM1YmYzNjgxYTUyYmExNGZjOA==
4
+ YTgyYzE2OGYyMjkzZTEzZGRiNzczZTMzMzdhYTIxOTdlNzg1NTZhZg==
5
5
  data.tar.gz: !binary |-
6
- ZDc3ZWI4YmU4MDE2Yjg5NDM4YmFjMTQwNGExOTg4ZjM0Mjc0OGMxNw==
6
+ Yzc5M2ZmNWQ5MjdkZGY5YmIzNWNmODZlYWZmYjY3ODdlMDYwODdjOQ==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- MTlkYjU5NzAzMGE5ODQxNTIxZmMwMmQwNjIzNzFkM2NiMjJlODY2NjhkYzQ1
10
- ZDk0NjM5MTEzMDBlYzI4NDM1Y2ZjYjdjYWJkYjhiNjFhODJlM2E4MWJlYzZm
11
- MDA5OGI4YzRiOGMxM2JmMGUwM2Y2Mzc4OTg1MWIxZTk4OGFmYjI=
9
+ N2YzODUwZGJlM2RlYmM4ZTZmYjYyMmRjOGRlZGEwNzBiMzRhYTRlNzk1N2Ri
10
+ MGZlYTljOTk5NzFhZmQ0YTI1OThlYWY1ZGRhOGZlZTA5M2U5ZWZlYzg5YzNl
11
+ NDZjYmVjNDY5M2RkYTA2YzhjOTMyZDFmZmI0N2ViOGFjNmM1OWE=
12
12
  data.tar.gz: !binary |-
13
- YzU0ZDg3MDA1YzUxNjI4NDY3Y2FjYzY0ZmI1ZWU2ODZjOGE0YmNjMTgyMjI3
14
- NmE5MTM1NmI0ZmIxOWY0OTczMDI5Y2M5ZGE2NTAxN2EzNDg0ZWEwYjM3NDZj
15
- Y2YxZTIyMGQyNTdmYjM5ZjE4N2IxYTI5ZGIzN2ZhZjlkOGYyMTA=
13
+ YjhlM2M3ZTQ2ZmRhYjQ0NWFiNTEzMzI2ODg5MDAzZjJhODBlYjg3ZWQxOWU0
14
+ YTI5YWU0ZDBlNWEzYzA0NTM2YWM3NmM0MjA1NTlhNDE0YmVmMzZmNzhiYjgz
15
+ OTJlOWJkYzc5Y2NkNzY1Y2Q2N2RkMDAwOGVlYmUzZWJmY2ZkYWI=
@@ -0,0 +1,64 @@
1
+ require 'addressable/uri'
2
+ require 'net/https'
3
+ require 'uri'
4
+
5
+ module LanxinOpen
6
+
7
+ class NetUtil
8
+ def self.get_req(url)
9
+ return nil if not url
10
+ #Rails.logger.info("get_req request url:#{url}")
11
+ uri = URI(url)
12
+
13
+ https = Net::HTTP.new(uri.host, uri.port)
14
+ #just for now.FIXME
15
+ if uri.scheme == 'https'
16
+ https.use_ssl = true
17
+ https.verify_mode = OpenSSL::SSL::VERIFY_NONE
18
+ end
19
+ response = https.get(uri.request_uri)
20
+ end
21
+
22
+ def self.dict2urlparam(params)
23
+ return if not params
24
+ uri = Addressable::URI.new
25
+ uri.query_values = params
26
+ uri.query
27
+ end
28
+
29
+ def self.force_encode(params,encoding_name)
30
+ params.each do |k,v|
31
+ if v.class == String #and v.encoding.name == "UTF-8"
32
+ begin
33
+ v_gbk = v.encode(encoding_name)
34
+ params[k] = v_gbk
35
+ rescue
36
+ end
37
+ end
38
+ end
39
+ return params
40
+ end
41
+
42
+ def self.post_req(url,params)
43
+ return if not url
44
+ uri = URI.parse(url)
45
+
46
+ need_uri_encode = false
47
+ if need_uri_encode
48
+ params.each do |k,v|
49
+ params[k] = URI.encode(v)
50
+ end
51
+ end
52
+
53
+ https = Net::HTTP.new(uri.host,uri.port)
54
+ if uri.scheme == 'https'
55
+ https.use_ssl = true
56
+ https.verify_mode = OpenSSL::SSL::VERIFY_NONE
57
+ end
58
+ req = Net::HTTP::Post.new(uri.path)
59
+ req.set_form_data(params)
60
+ response = https.request(req)
61
+ end
62
+ end
63
+
64
+ end
@@ -0,0 +1,211 @@
1
+ require "lanxin_open/net_util"
2
+
3
+ module LanxinOpen
4
+ module Platform
5
+ attr_accessor :skey
6
+ attr_accessor :host,:port,:use_new_json
7
+
8
+ def dump_config
9
+ puts "test_config #{host}, #{port}, #{use_new_json}"
10
+ end
11
+
12
+ def host_with_port
13
+ if port and port.length > 0
14
+ "#{host}:#{port}"
15
+ else
16
+ host
17
+ end
18
+ end
19
+
20
+ def lx_url(path)
21
+ unless path.start_with?("/")
22
+ path = "/" + path
23
+ end
24
+ "#{host_with_port}#{path}"
25
+ end
26
+
27
+ def content_utf8(content,source_charset="gbk")
28
+ return content unless source_charset
29
+ begin
30
+ # converter = Iconv.new 'UTF-8//IGNORE', source_charset
31
+ converter = Iconv.new 'UTF-8', source_charset
32
+ utf8_str = converter.iconv content
33
+ rescue Exception => e
34
+ return content
35
+ end
36
+ end
37
+
38
+ def encode_resbody(body)
39
+ source_charsets = ["gbk","gb2312"]
40
+ i = 0
41
+ begin
42
+ gbk = body.encode("utf-8", source_charsets[i])
43
+ return gbk
44
+ rescue Exception => e
45
+ if i < source_charsets.length
46
+ i +=1
47
+ retry
48
+ end
49
+ end
50
+ gbk = content_utf8(body,"gbk")
51
+ end
52
+
53
+ def fetch_skey(token,devkey)
54
+ req_url = lx_url("/opc/ishow")
55
+ params = {
56
+ "p_id" => "131",
57
+ "t_id" => "18",
58
+ "d_id" => "1",
59
+ "url" => "/logintoskey.html",
60
+ "token" => token,
61
+ "devkey" => devkey
62
+ }
63
+ # body = encode_resbody(NetUtil.post_req(req_url,params).body)
64
+ begin
65
+ body = encode_resbody(NetUtil.post_req(req_url,params).body)
66
+ puts body
67
+ ret = JSON.parse(body.to_s)
68
+ if ret["state"] == "OK"
69
+ @skey= ret["sessionKey"]
70
+ end
71
+ rescue
72
+ end
73
+ end
74
+
75
+ #new interface use UTF-8 charset
76
+ def member_get(org_id,open_id)
77
+ req_url = lx_url("cgi-bin/member/get")
78
+ params = {
79
+ "access_token" => skey,
80
+ "orgId" => org_id,
81
+ "mobile" => open_id,
82
+ }
83
+ body = NetUtil.post_req(req_url,params).body
84
+ end
85
+
86
+ #new interface use UTF-8 charset
87
+ # query_type:
88
+ # 0:get struct node; 1: get user node; -1:get all node
89
+ def org_get(org_id,struct_id,query_type)
90
+ req_url = lx_url("cgi-bin/org/struct/parent/get")
91
+ params = {
92
+ "access_token" => skey,
93
+ "orgId" => org_id,
94
+ "structId" => struct_id,
95
+ "orgStructType" => query_type
96
+ }
97
+ body = NetUtil.post_req(req_url,params).body
98
+ end
99
+
100
+ end
101
+
102
+ class OpenPlatformV1
103
+ include Platform
104
+
105
+ def initialize(args)
106
+ @host = args[:host] || LanxinOpen.host
107
+ @port = args[:port] || LanxinOpen.port
108
+ @use_new_json = args[:use_new_json] || LanxinOpen.use_new_json
109
+ end
110
+
111
+ def kehu_msg(fieldvalue,from_user)
112
+ req_url = lx_url("/opc/ishow")
113
+ params = {
114
+ "p_id" => "131",
115
+ "t_id" => "5",
116
+ "d_id" => "0",
117
+ "url" => "/customermessage/${docid}.shtml",
118
+ "skey" => skey,
119
+ "_fieldvalue_msgcontent" => fieldvalue.to_json
120
+ }
121
+ body = encode_resbody(NetUtil.post_req(req_url,params).body)
122
+ end
123
+
124
+ def send_text_msg(txt_msg,open_id,from_user)
125
+ text_json = {"content" => txt_msg}
126
+ base_json = {
127
+ "ToUserName" => open_id,
128
+ "FromUserName" => from_user,
129
+ "msgtype" => "text",
130
+ "text" => text_json.to_json
131
+ }
132
+ base_json["text"] = text_json if use_new_json
133
+ body = kehu_msg(base_json,from_user)
134
+ end
135
+
136
+ def send_link_msg(url,open_id,from_user)
137
+ base_json = {
138
+ "ToUserName" => open_id,
139
+ "FromUserName" => from_user,
140
+ "msgtype" => "link",
141
+ "url" => url
142
+ }
143
+ body = kehu_msg(base_json,from_user)
144
+ end
145
+
146
+ def send_pictext_msg(url,open_id,title,from_user)
147
+ news_json = {"url" => url}
148
+ base_json = {
149
+ "ToUserName" => open_id,
150
+ "FromUserName" => from_user,
151
+ "msgtype" => "news",
152
+ "title" => title,
153
+ "news" => news_json
154
+ }
155
+ # base_json["news"] = news_json if use_new_json
156
+ body = kehu_msg(base_json,from_user)
157
+ end
158
+
159
+ def send_mail_msg(url,open_id,title,from_user)
160
+ news_json = {"url" => url}
161
+ base_json = {
162
+ "ToUserName" => open_id,
163
+ "FromUserName" => from_user,
164
+ "msgtype" => "mail",
165
+ "news" => news_json,
166
+ "title" => title
167
+ }
168
+ body = kehu_msg(base_json,from_user)
169
+ end
170
+
171
+ def create_menu(menu_json)
172
+ params = {
173
+ "p_id" => 131,
174
+ "t_id" => 15,
175
+ "d_id" => 1,
176
+ "url" => "/insert_menu.shtml",
177
+ "menucontent" => menu_json.to_json,
178
+ "skey" => skey
179
+ }
180
+ req_url = lx_url("/opc/ishow")
181
+ body = encode_resbody(NetUtil.post_req(req_url,params).body)
182
+ end
183
+
184
+ def show_menu(publicno)
185
+ params = {
186
+ "p_id" => 131,
187
+ "t_id" => 16,
188
+ "d_id" => 1,
189
+ "url" => "/query_menu.shtml",
190
+ "publicno" => publicno,
191
+ "skey" => skey
192
+ }
193
+ req_url = lx_url("/opc/ishow")
194
+ body = encode_resbody(NetUtil.post_req(req_url,params).body)
195
+ end
196
+
197
+ def del_menu
198
+ req_url = lx_url("/opc/ishow")
199
+ params = {
200
+ "p_id" => 131,
201
+ "t_id" => 17,
202
+ "d_id" => 1,
203
+ "url" => "/delmenu.shtml",
204
+ "skey" => skey
205
+ }
206
+ body = encode_resbody(NetUtil.post_req(req_url,params).body)
207
+ end
208
+
209
+ end # End of OpenPlatformV1 class
210
+
211
+ end
@@ -1,13 +1,16 @@
1
1
  module SigVerify
2
2
 
3
- def sig_verify(params)
4
- time = params["timestamp"].to_i
5
- time = time.to_i / 1000 if params["timestamp"].length >= 13
3
+ def sig_verify(time_str,nonce,token,signature)
4
+ time = time_str.to_i
5
+ time = time / 1000 if time_str.length >= 13
6
6
  return false if (Time.now - Time.at(time)) > 120
7
7
 
8
- plain = [Rails.application.config.data_token,params["timestamp"],params["nonce"]].sort().join()
8
+ signature == sig_gen(time_str,nonce,token)
9
+ end
10
+
11
+ def sig_gen(time_str,nonce,token)
12
+ plain = [token,time_str,nonce].sort().join()
9
13
  sig = Digest::SHA1.hexdigest(plain)
10
- sig == params["signature"]
11
14
  end
12
15
 
13
16
  end
@@ -1,3 +1,3 @@
1
1
  module LanxinOpen
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
data/lib/lanxin_open.rb CHANGED
@@ -1,9 +1,7 @@
1
1
  require "lanxin_open/version"
2
2
  require "lanxin_open/sig_verify"
3
- require 'uri'
3
+ require "lanxin_open/openplatform"
4
4
  require "json"
5
- require 'addressable/uri'
6
- require 'net/https'
7
5
 
8
6
  module LanxinOpen
9
7
  extend self
@@ -62,270 +60,6 @@ module LanxinOpen
62
60
  end
63
61
  end
64
62
 
65
- class NetUtil
66
- def self.get_req(url)
67
- return nil if not url
68
- #Rails.logger.info("get_req request url:#{url}")
69
- uri = URI(url)
70
-
71
- https = Net::HTTP.new(uri.host, uri.port)
72
- #just for now.FIXME
73
- if uri.scheme == 'https'
74
- https.use_ssl = true
75
- https.verify_mode = OpenSSL::SSL::VERIFY_NONE
76
- end
77
- response = https.get(uri.request_uri)
78
- end
79
-
80
- def self.dict2urlparam(params)
81
- return if not params
82
- uri = Addressable::URI.new
83
- uri.query_values = params
84
- uri.query
85
- end
86
-
87
- def self.force_encode(params,encoding_name)
88
- params.each do |k,v|
89
- if v.class == String #and v.encoding.name == "UTF-8"
90
- begin
91
- v_gbk = v.encode(encoding_name)
92
- params[k] = v_gbk
93
- rescue
94
- end
95
- end
96
- end
97
- return params
98
- end
99
-
100
- def self.post_req(url,params)
101
- return if not url
102
- uri = URI.parse(url)
103
-
104
- need_uri_encode = false
105
- if need_uri_encode
106
- params.each do |k,v|
107
- params[k] = URI.encode(v)
108
- end
109
- end
110
-
111
- https = Net::HTTP.new(uri.host,uri.port)
112
- if uri.scheme == 'https'
113
- https.use_ssl = true
114
- https.verify_mode = OpenSSL::SSL::VERIFY_NONE
115
- end
116
- req = Net::HTTP::Post.new(uri.path)
117
- req.set_form_data(params)
118
- response = https.request(req)
119
- end
120
- end
121
-
122
- module Platform
123
- attr_accessor :skey
124
- attr_accessor :host,:port,:use_new_json
125
-
126
- def dump_config
127
- puts "test_config #{host}, #{port}, #{use_new_json}"
128
- end
129
-
130
- def host_with_port
131
- if port and port.length > 0
132
- "#{host}:#{port}"
133
- else
134
- host
135
- end
136
- end
137
-
138
- def lx_url(path)
139
- unless path.start_with?("/")
140
- path = "/" + path
141
- end
142
- "#{host_with_port}#{path}"
143
- end
144
-
145
- def content_utf8(content,source_charset="gbk")
146
- return content unless source_charset
147
- begin
148
- # converter = Iconv.new 'UTF-8//IGNORE', source_charset
149
- converter = Iconv.new 'UTF-8', source_charset
150
- utf8_str = converter.iconv content
151
- rescue Exception => e
152
- return content
153
- end
154
- end
155
-
156
- def encode_resbody(body)
157
- source_charsets = ["gbk","gb2312"]
158
- i = 0
159
- begin
160
- gbk = body.encode("utf-8", source_charsets[i])
161
- return gbk
162
- rescue Exception => e
163
- if i < source_charsets.length
164
- i +=1
165
- retry
166
- end
167
- end
168
- gbk = content_utf8(body,"gbk")
169
- end
170
-
171
- def fetch_skey(token,devkey)
172
- req_url = lx_url("/opc/ishow")
173
- params = {
174
- "p_id" => "131",
175
- "t_id" => "18",
176
- "d_id" => "1",
177
- "url" => "/logintoskey.html",
178
- "token" => token,
179
- "devkey" => devkey
180
- }
181
- # body = encode_resbody(NetUtil.post_req(req_url,params).body)
182
- begin
183
- body = encode_resbody(NetUtil.post_req(req_url,params).body)
184
- puts body
185
- ret = JSON.parse(body.to_s)
186
- if ret["state"] == "OK"
187
- @skey= ret["sessionKey"]
188
- end
189
- rescue
190
- end
191
- end
192
-
193
- #new interface use UTF-8 charset
194
- def member_get(org_id,open_id)
195
- req_url = lx_url("cgi-bin/member/get")
196
- params = {
197
- "access_token" => skey,
198
- "orgId" => org_id,
199
- "mobile" => open_id,
200
- }
201
- body = NetUtil.post_req(req_url,params).body
202
- end
203
-
204
- #new interface use UTF-8 charset
205
- # query_type:
206
- # 0:get struct node; 1: get user node; -1:get all node
207
- def org_get(org_id,struct_id,query_type)
208
- req_url = lx_url("cgi-bin/org/struct/parent/get")
209
- params = {
210
- "access_token" => skey,
211
- "orgId" => org_id,
212
- "structId" => struct_id,
213
- "orgStructType" => query_type
214
- }
215
- body = NetUtil.post_req(req_url,params).body
216
- end
217
-
218
- end
219
-
220
- class OpenPlatformV1
221
- include Platform
222
-
223
- def initialize(args)
224
- @host = args[:host] || LanxinOpen.host
225
- @port = args[:port] || LanxinOpen.port
226
- @use_new_json = args[:use_new_json] || LanxinOpen.use_new_json
227
- end
228
-
229
- def kehu_msg(fieldvalue,from_user)
230
- req_url = lx_url("/opc/ishow")
231
- params = {
232
- "p_id" => "131",
233
- "t_id" => "5",
234
- "d_id" => "0",
235
- "url" => "/customermessage/${docid}.shtml",
236
- "skey" => skey,
237
- "_fieldvalue_msgcontent" => fieldvalue.to_json
238
- }
239
- body = encode_resbody(NetUtil.post_req(req_url,params).body)
240
- end
241
-
242
- def send_text_msg(txt_msg,open_id,from_user)
243
- text_json = {"content" => txt_msg}
244
- base_json = {
245
- "ToUserName" => open_id,
246
- "FromUserName" => from_user,
247
- "msgtype" => "text",
248
- "text" => text_json.to_json
249
- }
250
- base_json["text"] = text_json if use_new_json
251
- body = kehu_msg(base_json,from_user)
252
- end
253
-
254
- def send_link_msg(url,open_id,from_user)
255
- base_json = {
256
- "ToUserName" => open_id,
257
- "FromUserName" => from_user,
258
- "msgtype" => "link",
259
- "url" => url
260
- }
261
- body = kehu_msg(base_json,from_user)
262
- end
263
-
264
- def send_pictext_msg(url,open_id,title,from_user)
265
- news_json = {"url" => url}
266
- base_json = {
267
- "ToUserName" => open_id,
268
- "FromUserName" => from_user,
269
- "msgtype" => "news",
270
- "title" => title,
271
- "news" => news_json
272
- }
273
- # base_json["news"] = news_json if use_new_json
274
- body = kehu_msg(base_json,from_user)
275
- end
276
-
277
- def send_mail_msg(url,open_id,title,from_user)
278
- news_json = {"url" => url}
279
- base_json = {
280
- "ToUserName" => open_id,
281
- "FromUserName" => from_user,
282
- "msgtype" => "mail",
283
- "news" => news_json,
284
- "title" => title
285
- }
286
- body = kehu_msg(base_json,from_user)
287
- end
288
-
289
- def create_menu(menu_json)
290
- params = {
291
- "p_id" => 131,
292
- "t_id" => 15,
293
- "d_id" => 1,
294
- "url" => "/insert_menu.shtml",
295
- "menucontent" => menu_json.to_json,
296
- "skey" => skey
297
- }
298
- req_url = lx_url("/opc/ishow")
299
- body = encode_resbody(NetUtil.post_req(req_url,params).body)
300
- end
301
-
302
- def show_menu(publicno)
303
- params = {
304
- "p_id" => 131,
305
- "t_id" => 16,
306
- "d_id" => 1,
307
- "url" => "/query_menu.shtml",
308
- "publicno" => publicno,
309
- "skey" => skey
310
- }
311
- req_url = lx_url("/opc/ishow")
312
- body = encode_resbody(NetUtil.post_req(req_url,params).body)
313
- end
314
-
315
- def del_menu
316
- req_url = lx_url("/opc/ishow")
317
- params = {
318
- "p_id" => 131,
319
- "t_id" => 17,
320
- "d_id" => 1,
321
- "url" => "/delmenu.shtml",
322
- "skey" => skey
323
- }
324
- body = encode_resbody(NetUtil.post_req(req_url,params).body)
325
- end
326
-
327
- end # End of OpenPlatformV1 class
328
-
329
63
  end
330
64
 
331
65
  LanxinOpen.config do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lanxin_open
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - wangdean
@@ -56,6 +56,8 @@ files:
56
56
  - Rakefile
57
57
  - lanxin_open.gemspec
58
58
  - lib/lanxin_open.rb
59
+ - lib/lanxin_open/net_util.rb
60
+ - lib/lanxin_open/openplatform.rb
59
61
  - lib/lanxin_open/sig_verify.rb
60
62
  - lib/lanxin_open/version.rb
61
63
  homepage: ''