rb-net_http-client 1.0.8 → 1.0.9

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
  SHA256:
3
- metadata.gz: 0dc48f10a3e4bcef77bfd022105c8d2c8eac6ad051d5a2cb7df74ffe6f73301c
4
- data.tar.gz: 29f716c9095d131543befdb9999febe5025168cef4ed6d543b1089302d6c3686
3
+ metadata.gz: 6d55326eb0acbf27e9b701970c6454d99ca776adb6cfd3b47bdc314fa9a487c2
4
+ data.tar.gz: 6da96962de6db3697ab09a34f0b9f5130a378a1ef5686b5ceb99952665b5b7bd
5
5
  SHA512:
6
- metadata.gz: cb9851ae05fc3a6b2e5ebe40b8928fbea27d27ae6924004c041a805d145b5cb84f5b3c81844be6e1742ec530219dfcd929e323561536c52eb1715a6a91cc0823
7
- data.tar.gz: ab449d0aa89b9e31633f4362258636a7e52b7b4cf8258c137bb73e8bc91b4185814be5ffed83fe1080f6b26b33594bd8f411c7ce5a6541fee1f6fb068b1ad7d1
6
+ metadata.gz: 598b5c8bbd716f0c57d901db69392593cb6ad2ec9469eb7d51748bb6db72ce425cd297367cca4bc1a0ad6004b38bad478cdf87c8e8d65aea6e7300866cccb566
7
+ data.tar.gz: 7fcf8f7272059c15be3a116e5dab90a4b64951696b44ef2df7e5e380f43d6afe11fee2340fbb036d828eb627ce3f3d34b3f4f763ed5a8575d02fe31194491310
data/lib/client/client.rb CHANGED
@@ -32,7 +32,12 @@ module NetHTTP
32
32
 
33
33
  def initialize(opts = {})
34
34
  send('logger=', opts[:logger])
35
- Core.schema_validation(opts, NetHTTP::Client::Schema, logger) unless opts[:enforce_schema_validation] == false
35
+ schema_results = Core.schema_validation(opts, NetHTTP::Client::Schema) unless opts[:enforce_schema_validation] == false
36
+ unless schema_results.nil?
37
+ logger.debug("NetHTTP::Client::SchemaError -> #{schema_results}")
38
+ raise NetHTTP::Client::SchemaError.new(schema_results.to_s)
39
+ end
40
+
36
41
  send('uri=', opts)
37
42
  send('client=', opts)
38
43
  end
data/lib/client/schema.rb CHANGED
@@ -47,5 +47,11 @@ module NetHTTP
47
47
  uri.empty?.then(url.empty?.then(port.empty?.then(scheme.filled?)))
48
48
  end
49
49
  end
50
+
51
+ class SchemaError < StandardError
52
+ def initialize(msg)
53
+ super(msg)
54
+ end
55
+ end
50
56
  end
51
57
  end
data/lib/core/core.rb CHANGED
@@ -15,23 +15,19 @@ module NetHTTP
15
15
  logger
16
16
  end
17
17
 
18
- def self.schema_validation(opts, schema, logger)
19
- begin
20
- results = schema.call(opts)
21
- return if results.success?
22
- begin
23
- schema_name = schema.name
24
- logger.debug("NetHTTP::Core::SchemaValidationError - #{schema_name} input validation failed due to => #{results.messages}")
25
- raise("NetHTTP::Core::SchemaValidationError - #{schema_name} input validation failed due to => #{results.messages}")
26
- rescue RuntimeError => error
27
- logger.debug("NetHTTP::Core::SchemaValidationError - Dry::Validation::Schema input validation failed due to => #{results.messages}")
28
- raise("NetHTTP::Core::SchemaValidationError - Dry::Validation::Schema input validation failed due to => #{results.messages}")
29
- end
30
- rescue RuntimeError => error
31
- logger.debug(error)
32
- logger.debug("NetHTTP::Core::SchemaValidationError - Dry::Validation::Schema input validation failed.")
33
- raise error
18
+ def self.schema_validation(opts, schema)
19
+ results = schema.call(opts)
20
+ if results.success?
21
+ return nil
22
+ else
23
+ return results.messages
34
24
  end
35
25
  end
36
26
  end
27
+
28
+ class SchemaError < StandardError
29
+ def initialize(msg)
30
+ super(msg)
31
+ end
32
+ end
37
33
  end
@@ -144,6 +144,23 @@ module NetHTTP
144
144
  opts[:object]
145
145
  end
146
146
 
147
+ # Recursive function to remove nil and empty values (including [] and {} from Array or Hash nested objects.
148
+ def self.scrub_obj(obj)
149
+ case obj
150
+ when Array
151
+ return obj.map { |item| scrub_obj(item) }
152
+ when Hash
153
+ new_hash = {}
154
+ obj.compact.each do |key, value|
155
+ new_hash[key] = scrub_obj(value) unless value.empty?
156
+ end
157
+
158
+ return new_hash
159
+ end
160
+
161
+ obj
162
+ end
163
+
147
164
  # Convert JSON doc to a Ruby Hash.
148
165
  def self.json_2_hash(json_doc, type = 'symbol', logger = nil)
149
166
  msg = "Invalid 'type' => #{type}. Use either 'string' or 'symbol' (default)."
@@ -135,7 +135,11 @@ module NetHTTP
135
135
  end
136
136
 
137
137
  def request_opts(opts)
138
- Core.schema_validation(opts, NetHTTP::Request::Schema, logger) unless opts[:enforce_schema_validation] == false
138
+ schema_results = Core.schema_validation(opts, NetHTTP::Request::Schema) unless opts[:enforce_schema_validation] == false
139
+ unless schema_results.nil?
140
+ logger.debug("NetHTTP::Request::SchemaError -> #{schema_results}")
141
+ raise NetHTTP::Request::SchemaError.new(schema_results.to_s)
142
+ end
139
143
 
140
144
  request_method = opts[:method] ||= 'post'
141
145
  request_uri = Core::Utilities.parse_uri(opts[:uri] || opts[:url] || uri)
@@ -20,5 +20,11 @@ module NetHTTP
20
20
  uri.empty?.then(url.empty?.then(path.filled?))
21
21
  end
22
22
  end
23
+
24
+ class SchemaError < StandardError
25
+ def initialize(msg)
26
+ super(msg)
27
+ end
28
+ end
23
29
  end
24
30
  end
data/lib/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module NetHTTP
4
- VERSION = '1.0.8'
4
+ VERSION = '1.0.9'
5
5
  end
@@ -19,7 +19,7 @@ describe 'NetHTTP::Client ext' do
19
19
  uri: 'https://jsonplaceholder.typicode.com/posts',
20
20
  proxy_uri: ''
21
21
  )
22
- }.to raise_error(RuntimeError, /NetHTTP::Core::SchemaValidationError/)
22
+ }.to raise_error(NetHTTP::Client::SchemaError)
23
23
  end
24
24
 
25
25
  it 'returns a valid NetHTTP client instance with proxy_uri -> nil' do
@@ -28,7 +28,7 @@ describe 'NetHTTP::Client ext' do
28
28
  uri: 'https://jsonplaceholder.typicode.com/posts',
29
29
  proxy_uri: nil
30
30
  )
31
- }.to raise_error(RuntimeError, /NetHTTP::Core::SchemaValidationError/)
31
+ }.to raise_error(NetHTTP::Client::SchemaError)
32
32
  end
33
33
 
34
34
  it 'returns a valid NetHTTP client instance with proxy_uri -> INVALID' do
@@ -37,7 +37,7 @@ describe 'NetHTTP::Client ext' do
37
37
  uri: 'https://jsonplaceholder.typicode.com/posts',
38
38
  proxy_uri: 12345
39
39
  )
40
- }.to raise_error(RuntimeError, /NetHTTP::Core::SchemaValidationError/)
40
+ }.to raise_error(NetHTTP::Client::SchemaError)
41
41
  end
42
42
 
43
43
  it 'returns a valid NetHTTP client instance' do
@@ -10,26 +10,24 @@ describe 'NetHTTP::Core' do
10
10
  {
11
11
  uri: 'https://www.google.com'
12
12
  },
13
- NetHTTP::Client::Schema,
14
- logger
13
+ NetHTTP::Client::Schema
15
14
  )
16
15
 
17
16
  expect(results).to eq(nil)
18
17
  end
19
18
 
20
- it 'failed schema_validation' do
21
- logger = NetHTTP::Core.get_logger
19
+ # it 'failed schema_validation' do
20
+ # logger = NetHTTP::Core.get_logger
22
21
 
23
- expect {
24
- NetHTTP::Core.schema_validation(
25
- {
26
- uri: nil
27
- },
28
- NetHTTP::Client::Schema,
29
- logger
30
- )
31
- }.to raise_error(RuntimeError)
32
- end
22
+ # expect {
23
+ # NetHTTP::Core.schema_validation(
24
+ # {
25
+ # uri: nil
26
+ # },
27
+ # NetHTTP::Client::Schema
28
+ # )
29
+ # }.to raise_error(NetHTTP::Client::SchemaError)
30
+ # end
33
31
 
34
32
  it 'failed schema_validation -> invalid schema' do
35
33
  logger = NetHTTP::Core.get_logger
@@ -39,8 +37,7 @@ describe 'NetHTTP::Core' do
39
37
  {
40
38
  uri: 'https://www.google.com'
41
39
  },
42
- 'Invalid::Schema',
43
- logger
40
+ 'Invalid::Schema'
44
41
  )
45
42
  }.to raise_error(NoMethodError, "undefined method `call' for \"Invalid::Schema\":String")
46
43
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rb-net_http-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.8
4
+ version: 1.0.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryan Bostian
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-04-11 00:00:00.000000000 Z
11
+ date: 2019-04-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport