zhima 0.2.6 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4a1c5288c76c7dd21f7afcc4d9ac792a17638cff
4
- data.tar.gz: 36748ae8e31b45ecc09c20247ff36c7c4d00726a
3
+ metadata.gz: 5ae4098fe5acc3b45025e6cd8603676362d4c7c3
4
+ data.tar.gz: b64c2616fa053692d59d8e86a30c3d256e2167e4
5
5
  SHA512:
6
- metadata.gz: d9938d6e8a7ebadfcbe8e432650159c3e0658a38060286129e475812df805ebcf3c27ce78bb4c97d59f4216dc849285c50514d3731ff2e2343e9825dc103d4cf
7
- data.tar.gz: 125fe0b745c1f1c6d864e4dc61179e6bf41bd6affefa5325d1488878890f36020b3a604330c720490d897f6705d8b62420824d0520ae8dc6af131ef2dcad586d
6
+ metadata.gz: 7c0ed4896d352c4302ca8dbcf640f8fedbf4c2e4123c67d600eaa952cd292196917b1108554ae0d9da5f70141d58fde4084b3da53d758aa8ceabc43f11784f52
7
+ data.tar.gz: 2602e39b06060ba1d44970b8d9e50ebde5aa63b8e929f542045779ad4acef5e1a6bb7eb8c6c538479ae7ebb17672ec5ffd3c506032de6f97d1db62e72739fdf2
data/README.md CHANGED
@@ -21,8 +21,37 @@ Or install it yourself as:
21
21
  $ gem install zhima
22
22
 
23
23
  ## Usage
24
+ 配置(config/initializers/zhima.rb):
25
+ ```ruby
26
+ Zhima.configure do |config|
27
+ config.app_id = 'xxxxxxx' # 应用id
28
+ config.private_key = private_key # 自己的私钥
29
+ config.public_key = public_key # 芝麻给的公钥
30
+ end
31
+ ```
32
+
33
+ 芝麻信用分,调用方法如下(请自行查阅芝麻文档的业务参数组织params):
34
+ ```ruby
35
+ # 认证url 业务参数请参考 https://b.zmxy.com.cn/technology/openDoc.htm?id=67
36
+ Zhima::Score.auth_url(params) # 第二个参数可hash传入芝麻需要的系统参数,不传亦可(下同,省略)
37
+
38
+ # 获取芝麻分
39
+ # https://b.zmxy.com.cn/technology/openDoc.htm?relInfo=zhima.credit.score.get@1.0@1.4&relType=API_DOC&type=API_INFO_DOC&LEFT_MENU_MODEnull#Seq_1
40
+ Zhima::Score.get(params)
24
41
 
25
- TODO: Write usage instructions here
42
+ # auth_query
43
+ # https://b.zmxy.com.cn/technology/openDoc.htm?id=453
44
+ Zhima::Score.auth_query(params)
45
+
46
+ # 芝麻callback url中的params参数解密
47
+ Zhima::Score.param_decrypt(params_str)
48
+ ```
49
+
50
+ 反欺诈信息验证:
51
+ ```ruby
52
+ # 参数 https://b.zmxy.com.cn/technology/openDoc.htm?relInfo=zhima.credit.ivs.detail.get@1.0@1.2&relType=API_DOC&type=API_INFO_DOC&LEFT_MENU_MODEnull
53
+ Zhima::Ivs.get(params)
54
+ ```
26
55
 
27
56
  ## Development
28
57
 
@@ -32,7 +61,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
32
61
 
33
62
  ## Contributing
34
63
 
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/zhima. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
64
+ Bug reports and pull requests are welcome on GitHub at https://github.com/zhchsf/zhima. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
36
65
 
37
66
 
38
67
  ## License
data/lib/zhima/ivs.rb ADDED
@@ -0,0 +1,11 @@
1
+ module Zhima
2
+ # 反欺诈信息验证
3
+ class Ivs
4
+ METHOD = 'zhima.credit.ivs.detail.get'
5
+
6
+ # https://b.zmxy.com.cn/technology/openDoc.htm?relInfo=zhima.credit.ivs.detail.get@1.0@1.2&relType=API_DOC&type=API_INFO_DOC&LEFT_MENU_MODEnull
7
+ def self.get(params, sys_options = {})
8
+ Request.new(params, sys_options.merge(method: METHOD)).execute
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,36 @@
1
+ module Zhima
2
+ class Request
3
+ GATEWAY = 'https://zmopenapi.zmxy.com.cn/openapi.do'
4
+ SYSTEM_OPTIONS = {charset: 'UTF-8', version: '1.0', channel: 'app'}
5
+
6
+ attr_accessor :biz_params, :sys_options
7
+
8
+ def initialize(biz_params, sys_options)
9
+ @biz_params = biz_params
10
+ @sys_options = SYSTEM_OPTIONS.merge(sys_options)
11
+ end
12
+
13
+ def url
14
+ params_value, sign = Param.encrypt(biz_params)
15
+ opts = SYSTEM_OPTIONS.merge(sys_options)
16
+ .merge(params: params_value, sign: sign, app_id: Config.app_id)
17
+ query_str = Util.to_query(opts)
18
+ [GATEWAY, query_str].join('?')
19
+ end
20
+
21
+ def execute
22
+ response = RestClient.get url
23
+ parse_response(response.body)
24
+ end
25
+
26
+ private
27
+ # 芝麻返回的json数据解析,结果为真正的业务参数
28
+ # TODO verify sign
29
+ def parse_response(response_str)
30
+ response_hash = JSON.parse(response_str)
31
+ biz_response = response_hash["biz_response"]
32
+ biz_response = Param.decrypt(biz_response) if response_hash["encrypted"]
33
+ JSON.parse(biz_response)
34
+ end
35
+ end
36
+ end
data/lib/zhima/score.rb CHANGED
@@ -2,8 +2,6 @@ require "rest-client"
2
2
 
3
3
  module Zhima
4
4
  class Score
5
- GATEWAY = 'https://zmopenapi.zmxy.com.cn/openapi.do'
6
- SYSTEM_OPTIONS = {charset: 'UTF-8', version: '1.0', channel: 'app'}
7
5
  AUTHORIZE_METHOD = 'zhima.auth.info.authorize'
8
6
  SCORE_METHOD = 'zhima.credit.score.get'
9
7
  AUTH_QUERY_METHOD = 'zhima.auth.info.authquery'
@@ -11,22 +9,17 @@ module Zhima
11
9
  # params参数 请参考 https://b.zmxy.com.cn/technology/openDoc.htm?id=67
12
10
  # 系统参数 SYSTEM_OPTIONS,可自己传入,一般只需要配置channel参数(与auth_code不对应芝麻信用会报错)
13
11
  def self.auth_url(params, sys_options = {})
14
- url_by(params, sys_options.merge(method: AUTHORIZE_METHOD))
12
+ Request.new(params, sys_options.merge(method: AUTHORIZE_METHOD)).url
15
13
  end
16
14
 
17
15
  # https://b.zmxy.com.cn/technology/openDoc.htm?relInfo=zhima.credit.score.get@1.0@1.4&relType=API_DOC&type=API_INFO_DOC&LEFT_MENU_MODEnull#Seq_1
18
- # TODO verify sign
19
16
  def self.get(params, sys_options = {})
20
- score_url = url_by(params, sys_options.merge(method: SCORE_METHOD))
21
- response = RestClient.get score_url
22
- parse_response(response.body)
17
+ Request.new(params, sys_options.merge(method: SCORE_METHOD)).execute
23
18
  end
24
19
 
25
20
  # https://b.zmxy.com.cn/technology/openDoc.htm?id=453
26
21
  def self.auth_query(params, sys_options = {})
27
- query_url = url_by(params, sys_options.merge(method: AUTH_QUERY_METHOD))
28
- response = RestClient.get query_url
29
- parse_response(response.body)
22
+ Request.new(params, sys_options.merge(method: AUTH_QUERY_METHOD)).execute
30
23
  end
31
24
 
32
25
  # 芝麻callback url中的params参数解密方法
@@ -34,20 +27,5 @@ module Zhima
34
27
  decrypted_str = Param.decrypt(params_str)
35
28
  URI.decode_www_form(URI.decode URI.escape(decrypted_str)).to_h
36
29
  end
37
-
38
- def self.url_by(params, sys_options)
39
- params_value, sign = Param.encrypt(params)
40
- opts = SYSTEM_OPTIONS.merge(sys_options)
41
- .merge(params: params_value, sign: sign, app_id: Config.app_id)
42
- query_str = Util.to_query(opts)
43
- [GATEWAY, query_str].join('?')
44
- end
45
-
46
- def self.parse_response(response_str)
47
- response_hash = JSON.parse(response_str)
48
- biz_response = response_hash["biz_response"]
49
- biz_response = Param.decrypt(biz_response) if response_hash["encrypted"]
50
- JSON.parse(biz_response)
51
- end
52
30
  end
53
31
  end
data/lib/zhima/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Zhima
2
- VERSION = "0.2.6"
2
+ VERSION = "0.3.0"
3
3
  end
data/lib/zhima.rb CHANGED
@@ -11,8 +11,10 @@ module Zhima
11
11
  end
12
12
  end
13
13
 
14
+ require_relative "zhima/config"
14
15
  require_relative "zhima/score"
16
+ require_relative "zhima/ivs"
17
+ require_relative "zhima/request"
15
18
  require_relative "zhima/param"
16
19
  require_relative "zhima/sign"
17
20
  require_relative "zhima/util"
18
- require_relative "zhima/config"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zhima
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.6
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - sam
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-10-20 00:00:00.000000000 Z
11
+ date: 2016-10-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rest-client
@@ -85,7 +85,9 @@ files:
85
85
  - bin/setup
86
86
  - lib/zhima.rb
87
87
  - lib/zhima/config.rb
88
+ - lib/zhima/ivs.rb
88
89
  - lib/zhima/param.rb
90
+ - lib/zhima/request.rb
89
91
  - lib/zhima/score.rb
90
92
  - lib/zhima/sign.rb
91
93
  - lib/zhima/util.rb