api_tools 0.0.2 → 0.0.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: 1d1f1a6697b965e58c477d6a010ec1cfd6447575
4
- data.tar.gz: 461feec02dba1372e9bd13d0ee5ba0130f60893d
3
+ metadata.gz: c3e56100008f5a35083b0043cc17b9db07c7bb44
4
+ data.tar.gz: 7c80f71133a6bb39c21c95128b8fa8e1e67ad130
5
5
  SHA512:
6
- metadata.gz: f12330f40ec97f66f6d9016b309066b1a0da4f064e51a58572aaced55d5a08b09438bdfabca3cd2a7bbe55b6477c78ff1b440e7932cf503b8a9b9875cf6381e7
7
- data.tar.gz: 424a5ae18fb1e0c5ad34358b32027c3f70688f58e78bcefd8c27947bd59b3fbc9684ee77337d151ebd29be0962ea1079aff6f89df57e09cd5fab13fc0b000ccb
6
+ metadata.gz: c6a58991777ef3410b2697121ebd9ee6e6cc2a6b00028b3f1275780b8cae0d3c6c37419657ca5078d076cc2ee73e3af2105fc30ba3618712bc1b7bf411464c27
7
+ data.tar.gz: ebd00d5c22311f1fc9bf3564ecc44c8643d482fa1c883109b4294b2b4ef1f548d84d63b26f97adc286d6792f27675a2736234b29623aeb2dc08c57a479f12a54
data/README.md CHANGED
@@ -7,4 +7,42 @@
7
7
 
8
8
 
9
9
  ## 使用
10
- gem 'api_tools', :git => 'git@github.com:jicheng1014/api_tools.git'
10
+
11
+ ### add to Gemfile
12
+ gem 'api_tools', :git => 'https://github.com/jicheng1014/api_tools.git'
13
+
14
+ ## 任意对象
15
+
16
+ 直接使用  DefaultRest.post(url, xxx)
17
+
18
+
19
+
20
+ ```ruby
21
+ class XXXService < DefaultRest
22
+ # override(option)
23
+ def default_options
24
+ # 默认的参数
25
+ {
26
+ timeout: 5,
27
+ retry_times: 5,
28
+ response_json: true,
29
+ params_to_json: true,
30
+ ensure_no_exception: false,
31
+ header: { content_type: :json, accept: :json },
32
+ other_base_execute_option: {},
33
+ exception_with_response: true
34
+ }
35
+ end
36
+
37
+ # override (must)
38
+ def basic_url
39
+ # 填写基础版本的url
40
+ end
41
+
42
+ #override (option)
43
+ def base_params
44
+ # 默认每次提交时候附带的默认参数
45
+ {}
46
+ end
47
+ end
48
+ ```
data/api_tools.gemspec CHANGED
@@ -32,5 +32,5 @@ Gem::Specification.new do |spec|
32
32
  spec.add_development_dependency "webmock"
33
33
 
34
34
  spec.add_dependency "oj", '> 2.0'
35
- spec.add_dependency "rest-client", '> 2.0'
35
+ spec.add_dependency "rest-client", '> 1.0'
36
36
  end
@@ -1,114 +1,10 @@
1
1
  require_relative '../vendors/hash'
2
+ require_relative './default_rest_module.rb'
2
3
  require 'json'
3
4
  require 'uri'
4
5
  require 'rest-client'
5
6
  require 'oj'
6
7
 
7
8
  class DefaultRest
8
- class << self
9
- %w[get delete head].each do |word|
10
- define_method(word) do |path, params = {}, options = {}|
11
- user_params = base_params.merge(params)
12
- user_options = default_options.deep_merge(options) # 这里注意一下,是深merge,hash 底下的子hash是merge
13
- request_dict = build_similar_get_request(word, path, user_params, user_options)
14
- basic_request(request_dict, user_options)
15
- end
16
- end
17
-
18
- %w[post patch put].each do |word|
19
- define_method(word) do |path, params = {}, options = {}|
20
- user_params = base_params.merge(params)
21
- user_options = default_options.deep_merge(options) # 这里注意一下,是深merge,hash 底下的子hash是merge
22
- request_dict = build_similar_post_request(word, path, user_params, user_options)
23
- basic_request(request_dict, user_options)
24
- end
25
- end
26
-
27
- def build_similar_get_request(word, path, user_params, user_options)
28
- # 生成类get 请求的URL
29
- path_params = URI.escape(user_params.collect { |k, v| "#{k}=#{v}" }.join('&'))
30
- tmp = path.include?('?') ? '&' : '?'
31
- path = path + tmp + path_params
32
- url = build_whole_url(path)
33
- {
34
- method: word,
35
- url: url,
36
- headers: user_options[:header],
37
- timeout: user_options[:timeout]
38
- }.merge(user_options[:other_base_execute_option])
39
- end
40
-
41
- def build_similar_post_request(word, path, user_params, user_options)
42
- url = build_whole_url(path)
43
- payload = user_options[:params_to_json] ? user_params.to_json : user_params
44
- {
45
- method: word,
46
- url: url,
47
- payload: payload,
48
- headers: user_options[:header],
49
- timeout: user_options[:timeout]
50
- }.merge(user_options[:other_base_execute_option])
51
- end
52
-
53
- def build_whole_url(path)
54
- web = basic_url
55
- return web if path.length.zero?
56
- return path if path.start_with?("http") # path 是一个绝对路径
57
- if web[-1] == "/"
58
- path = path[1..-1] if path[0] == "/"
59
- else
60
- path = "/#{path}" if path[0] != "/"
61
- end
62
- web + path
63
- end
64
-
65
- def basic_request(request_dict, user_options)
66
- exception = nil
67
-
68
- user_options[:retry_times].times do
69
- begin
70
- response = ::RestClient::Request.execute(request_dict)
71
- return ::Oj.load(response.body, symbol_keys: true) if user_options[:response_json]
72
- return response.body
73
- rescue ::RestClient::ExceptionWithResponse => ex
74
- raise ex if user_options[:exception_with_response]
75
- return {
76
- status: false,
77
- response_code: ex.response.code,
78
- response_body: ex.response.body,
79
- message: ex.message
80
- }
81
- rescue RestClient::Exception => e
82
- exception = e
83
- next
84
- end
85
- end
86
- raise exception unless user_options[:ensure_no_exception]
87
- {
88
- status: false,
89
- message: ex.message
90
- }
91
- end
92
-
93
- def default_options
94
- @default_options ||= {
95
- timeout: 5,
96
- retry_times: 5,
97
- response_json: true,
98
- params_to_json: true,
99
- ensure_no_exception: false,
100
- header: { content_type: :json, accept: :json },
101
- other_base_execute_option: {},
102
- exception_with_response: true
103
- }
104
- end
105
-
106
- def basic_url
107
- 'http://www.example.com' # 子类中复写
108
- end
109
-
110
- def base_params
111
- {} # 子类中复写
112
- end
113
- end
9
+ extend ApiTools::DefaultRestModule
114
10
  end
@@ -0,0 +1,102 @@
1
+ module ApiTools
2
+ module DefaultRestModule
3
+ %w[get delete head].each do |word|
4
+ define_method(word) do |path, params = {}, options = {}|
5
+ user_params = base_params.merge(params)
6
+ user_options = default_options.deep_merge(options) # 这里注意一下,是深merge,hash 底下的子hash是merge
7
+ request_dict = build_similar_get_request(word, path, user_params, user_options)
8
+ basic_request(request_dict, user_options)
9
+ end
10
+ end
11
+
12
+ %w[post patch put].each do |word|
13
+ define_method(word) do |path, params = {}, options = {}|
14
+ user_params = base_params.merge(params)
15
+ user_options = default_options.deep_merge(options) # 这里注意一下,是深merge,hash 底下的子hash是merge
16
+ request_dict = build_similar_post_request(word, path, user_params, user_options)
17
+ basic_request(request_dict, user_options)
18
+ end
19
+ end
20
+
21
+ def build_similar_get_request(word, path, user_params, user_options)
22
+ # 生成类get 请求的URL
23
+ path_params = URI.escape(user_params.collect { |k, v| "#{k}=#{v}" }.join('&'))
24
+ tmp = path.include?('?') ? '&' : '?'
25
+ path = path + tmp + path_params
26
+ url = build_whole_url(path)
27
+ {
28
+ method: word,
29
+ url: url,
30
+ headers: user_options[:header],
31
+ timeout: user_options[:timeout]
32
+ }.merge(user_options[:other_base_execute_option])
33
+ end
34
+
35
+ def build_similar_post_request(word, path, user_params, user_options)
36
+ url = build_whole_url(path)
37
+ payload = user_options[:params_to_json] ? user_params.to_json : user_params
38
+ {
39
+ method: word,
40
+ url: url,
41
+ payload: payload,
42
+ headers: user_options[:header],
43
+ timeout: user_options[:timeout]
44
+ }.merge(user_options[:other_base_execute_option])
45
+ end
46
+
47
+ def build_whole_url(path)
48
+ web = basic_url
49
+ return web if path.length.zero?
50
+ return path if path.start_with?("http") # path 是一个绝对路径
51
+ if web[-1] == "/"
52
+ path = path[1..-1] if path[0] == "/"
53
+ else
54
+ path = "/#{path}" if path[0] != "/"
55
+ end
56
+ web + path
57
+ end
58
+
59
+ def basic_request(request_dict, user_options)
60
+ exception = nil
61
+ user_options[:retry_times].times do
62
+ begin
63
+ response = ::RestClient::Request.execute(request_dict)
64
+ return ::Oj.load(response.body, symbol_keys: true) if user_options[:response_json]
65
+ return response.body
66
+ rescue RestClient::Exception => e
67
+ exception = e
68
+ next
69
+ end
70
+ end
71
+ raise exception unless user_options[:ensure_no_exception]
72
+ {
73
+ status: false,
74
+ message: ex.message,
75
+ response_code: ex&.response.code,
76
+ response_body: ex&.response.body
77
+ }
78
+ end
79
+
80
+ def default_options
81
+ @default_options ||= {
82
+ timeout: 5,
83
+ retry_times: 5,
84
+ response_json: true,
85
+ params_to_json: true,
86
+ ensure_no_exception: false,
87
+ header: { content_type: :json, accept: :json },
88
+ other_base_execute_option: {},
89
+ exception_with_response: true
90
+ }
91
+ end
92
+
93
+ def basic_url
94
+ 'http://www.example.com' # 子类中复写
95
+ end
96
+
97
+ def base_params
98
+ {} # 子类中复写
99
+ end
100
+ end
101
+
102
+ end
@@ -1,3 +1,3 @@
1
1
  module ApiTools
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.4"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: api_tools
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - atpking
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-06-23 00:00:00.000000000 Z
11
+ date: 2018-03-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -114,14 +114,14 @@ dependencies:
114
114
  requirements:
115
115
  - - ">"
116
116
  - !ruby/object:Gem::Version
117
- version: '2.0'
117
+ version: '1.0'
118
118
  type: :runtime
119
119
  prerelease: false
120
120
  version_requirements: !ruby/object:Gem::Requirement
121
121
  requirements:
122
122
  - - ">"
123
123
  - !ruby/object:Gem::Version
124
- version: '2.0'
124
+ version: '1.0'
125
125
  description: 常用工具都放这里了
126
126
  email:
127
127
  - atpking@gmail.com
@@ -142,6 +142,7 @@ files:
142
142
  - bin/setup
143
143
  - lib/api_tools.rb
144
144
  - lib/api_tools/default_rest.rb
145
+ - lib/api_tools/default_rest_module.rb
145
146
  - lib/api_tools/version.rb
146
147
  - lib/vendors/hash.rb
147
148
  homepage: http://jicheng1014.cnblogs.com
@@ -164,7 +165,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
164
165
  version: '0'
165
166
  requirements: []
166
167
  rubyforge_project:
167
- rubygems_version: 2.6.12
168
+ rubygems_version: 2.6.14
168
169
  signing_key:
169
170
  specification_version: 4
170
171
  summary: 常用工具集合