rb-net_http-client 0.0.4 → 1.0.1

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
  SHA256:
3
- metadata.gz: 43106fa08999d370c0d5d28bdf9dc5ce1fecd7ecb32b72ee96d7b12ed63a7259
4
- data.tar.gz: 773fba38a078d87ab259dfa8c7a575d68e4cf2307dfed0d7affbacbb09dd2040
3
+ metadata.gz: 864953a6821c8556cd571a116d852aadb39810fbe41c672ce4a4d0a9d37ad0cc
4
+ data.tar.gz: 53fbde8821ae8c0610dc7ad7b1b798b92728df13fc05fd1bad0ca620dfb61761
5
5
  SHA512:
6
- metadata.gz: e7867815e0b13eefc9b15d35ab29af789879849f8d2d3eca4853cd273e5196764b2cd849a8ec0ff32ddb29b6b33712d76d1a5c80ee9db93ddb413e7ce3c58f22
7
- data.tar.gz: d278c746b537012f7767f429a1554059db30771ee9348dd7acbabd1620ff6a5c8f074a3771e1bd6ead4cd3e24583ce499334f52cda924e28603f8cd024f911f8
6
+ metadata.gz: fecbe20848e6f55335b16cbe3d5a8f418a1b5843a69aac09fa9f068eb67ccb08c407ca3226357c4595d9e2ccb60454b6e06357b6c7fba109d297098ffba47d7b
7
+ data.tar.gz: 7d5ffbc523f9c97bc3e241c7ca41f117f2ba51849d69b540c0e4ad5d5f8d30a221a4c09012812ab95d8c029c61ff030b8042eb2d46d175c028a1c638fb4ab2eb
data/lib/client/client.rb CHANGED
@@ -4,48 +4,81 @@ require 'net/https'
4
4
  require_relative 'ext'
5
5
  require_relative 'schema'
6
6
  require_relative '../core/core'
7
- require_relative '../request/ext'
8
- require_relative '../response/ext'
7
+ require_relative '../response/response'
8
+ require_relative '../request/request'
9
9
 
10
10
  module NetHTTP
11
- module_function
11
+ class Client
12
+ attr_reader :ca_file,
13
+ :cert,
14
+ :client,
15
+ :host,
16
+ :key,
17
+ :logger,
18
+ :path,
19
+ :pkcs12,
20
+ :port,
21
+ :proxy_uri,
22
+ :open_timeout,
23
+ :read_timeout,
24
+ :response,
25
+ :scheme,
26
+ :ssl_path,
27
+ :uri,
28
+ :use_proxy,
29
+ :use_ssl,
30
+ :verify_mode
31
+
32
+ def initialize(opts = {})
33
+ send('logger=', opts[:logger])
34
+ Core.schema_validation(opts, NetHTTP::Client::Schema, logger) unless opts[:enforce_schema_validation] == false
35
+ send('uri=', (opts[:uri] || opts[:url]))
36
+ send('client=', opts)
37
+ end
12
38
 
13
- def client(opts)
14
- logger = Core.assign_logger(opts[:logger])
39
+ def client=(opts = {})
40
+ @client = Net::HTTP.new(uri.host, uri.port)
15
41
 
16
- Core.schema_validation(opts, NetHTTP::Client::Schema, logger) unless opts[:enforce_schema_validation] == false
42
+ send('scheme=', uri.scheme)
43
+ send('host=', uri.host)
44
+ send('port=', uri.port)
45
+ send('path=', uri.path)
46
+ send('proxy_uri=', (opts[:proxy_uri] ||= opts[:proxy_url]))
47
+ send('use_proxy=')
48
+ send('open_timeout=', (opts[:open_timeout] ||= 60))
49
+ send('read_timeout=', (opts[:read_timeout] ||= 60))
50
+ send('use_ssl=', opts[:use_ssl], uri.scheme)
51
+ send('ssl_path=', opts[:ssl_path])
52
+ send('pkcs12_file=', opts[:pkcs12_file])
53
+ send('pkcs12_passphrase=', opts[:pkcs12_passphrase])
54
+ send('pkcs12=')
55
+ send('cert=')
56
+ send('key=')
57
+ send('ca_file=', opts[:ca_file])
58
+ send('verify_mode=', opts[:verify_mode])
17
59
 
18
- if !(opts[:uri] ||= opts[:url] ||= nil).nil?
19
- uri = Core.parse_uri((opts[:uri] ||= opts[:url]))
20
- else
21
- uri = Core.parse_uri(Core.construct_uri(opts[:scheme], opts[:host], opts[:port], opts[:path]))
60
+ @client.open_timeout = open_timeout
61
+ @client.read_timeout = read_timeout
62
+ @client.use_ssl = use_ssl
63
+ if use_ssl
64
+ @client.cert = cert
65
+ @client.key = key
66
+ @client.ca_file = ca_file
67
+ @client.verify_mode = verify_mode
68
+ end
22
69
  end
23
70
 
24
- # Instantiate client:
25
- client = Net::HTTP.new(uri.host, uri.port)
26
-
27
- # client config:
28
- client.logger = logger
29
- client.uri = uri
30
- client.host = uri.host
31
- client.port = uri.port
32
- client.path = uri.path
33
- client.use_proxy = Client.use_proxy(opts[:use_proxy])
34
- client.proxy_uri = Client.proxy_uri((opts[:proxy_uri] ||= opts[:proxy_url]), client.use_proxy)
35
- client.open_timeout = Client.open_timeout(opts[:open_timeout])
36
- client.read_timeout = Client.read_timeout(opts[:read_timeout])
37
-
38
- # SSL config:
39
- client.use_ssl = Client.use_ssl(opts[:use_ssl], uri.scheme)
40
-
41
- if client.use_ssl
42
- pkcs12 = Client.pkcs12(opts[:ssl_path], opts[:pkcs12_file], opts[:pkcs12_passphrase])
43
- client.cert = Client.cert(pkcs12)
44
- client.key = Client.key(pkcs12)
45
- client.ca_file = Client.ca_file(opts[:ssl_path], opts[:ca_file])
46
- client.verify_mode = Client.verify_mode(opts[:verify_mode])
71
+ private :logger,
72
+ :response
73
+
74
+ def logger=(logger = nil)
75
+ @logger = Core.get_logger(logger)
47
76
  end
77
+ end
78
+
79
+ module_function
48
80
 
49
- client
81
+ def client(opts = {})
82
+ Client.new(opts)
50
83
  end
51
84
  end
data/lib/client/ext.rb CHANGED
@@ -1,120 +1,137 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative '../core/core'
4
-
5
3
  module NetHTTP
6
- module Client
7
- module_function
8
-
9
- def proxy_uri(proxy_uri, use_proxy)
10
- return nil if use_proxy == false
4
+ class Client
5
+ def scheme=(scheme = nil)
6
+ @scheme = scheme
7
+ end
11
8
 
12
- Core.parse_uri(proxy_uri ||= ENV['http_proxy'] ||= ENV['HTTP_PROXY'] ||= ENV['https_proxy'] ||= ENV['HTTPS_PROXY'])
9
+ def host=(host = nil)
10
+ @host = host
13
11
  end
14
12
 
15
- def use_proxy(use_proxy)
16
- return false if use_proxy == false
13
+ def port=(port = nil)
14
+ @port = port
15
+ end
17
16
 
18
- true
17
+ def path=(path = nil)
18
+ @path = path
19
19
  end
20
20
 
21
- def use_ssl(use_ssl, scheme)
22
- if use_ssl.nil? || use_ssl.to_s.empty?
23
- return false if scheme == 'http'
24
- return true if scheme == 'https'
21
+ def use_proxy=
22
+ if proxy_uri.nil?
23
+ @use_proxy = false
24
+ else
25
+ @use_proxy = true
25
26
  end
26
- return false if use_ssl == false
27
-
28
- true
29
27
  end
30
28
 
31
- def open_timeout(open_timeout)
32
- (open_timeout ||= 60).to_s.to_i
29
+ def proxy_uri=(proxy_uri = nil)
30
+ proxy_uri = ENV['http_proxy'] ||= ENV['HTTP_PROXY'] ||= ENV['https_proxy'] ||= ENV['HTTPS_PROXY'] if proxy_uri.nil?
31
+ @proxy_uri = Core::Utilities.parse_uri(proxy_uri)
33
32
  end
34
33
 
35
- def read_timeout(read_timeout)
36
- (read_timeout ||= 60).to_s.to_i
34
+ def uri=(uri = nil)
35
+ if !uri.nil?
36
+ @uri = Core::Utilities.parse_uri(uri)
37
+ else
38
+ @uri = Core::Utilities.parse_uri(Core::Utilities.construct_uri(opts[:scheme], opts[:host], opts[:port], opts[:path]))
39
+ end
37
40
  end
38
41
 
39
- def pkcs12(ssl_path, pkcs12_file, pkcs12_passphrase)
40
- return nil if ssl_path.nil?
41
- return nil if ssl_path.to_s.empty?
42
- return nil if pkcs12_file.nil?
43
- return nil if pkcs12_file.to_s.empty?
44
- return nil if pkcs12_passphrase.nil?
45
- return nil if pkcs12_passphrase.to_s.empty?
46
-
47
- OpenSSL::PKCS12.new(File.binread(ssl_path + '/' + pkcs12_file), pkcs12_passphrase)
42
+ def ca_file=(ca_file)
43
+ if ca_file.nil? || ca_file.to_s.empty?
44
+ @ca_file = nil
45
+ elsif ssl_path.nil? || ssl_path.to_s.empty?
46
+ @ca_file = nil
47
+ else
48
+ @ca_file = File.binread(ssl_path + '/' + ca_file)
49
+ end
48
50
  end
49
51
 
50
- def cert(pkcs12)
51
- return nil if pkcs12.nil?
52
- return nil if pkcs12.to_s.empty?
53
-
54
- OpenSSL::X509::Certificate.new(pkcs12.certificate)
52
+ def cert=
53
+ if pkcs12.nil? || pkcs12.to_s.empty?
54
+ @cert = nil
55
+ else
56
+ @cert = OpenSSL::X509::Certificate.new(pkcs12.certificate)
57
+ end
55
58
  end
56
59
 
57
- def key(pkcs12)
58
- return nil if pkcs12.nil?
59
- return nil if pkcs12.to_s.empty?
60
-
61
- OpenSSL::PKey::RSA.new(pkcs12.key)
60
+ def key=
61
+ if pkcs12.nil? || pkcs12.to_s.empty?
62
+ @key = nil
63
+ else
64
+ @key = OpenSSL::PKey::RSA.new(pkcs12.key)
65
+ end
62
66
  end
63
67
 
64
- def ca_file(ssl_path, ca_file)
65
- return nil if ssl_path.nil?
66
- return nil if ssl_path.to_s.empty?
67
- return nil if ca_file.nil?
68
- return nil if ca_file.to_s.empty?
68
+ def open_timeout=(open_timeout)
69
+ @open_timeout = open_timeout.to_s.to_i
70
+ end
69
71
 
70
- File.binread(ssl_path + '/' + ca_file)
72
+ def pkcs12=
73
+ if ssl_path.nil? || ssl_path.to_s.empty?
74
+ @pkcs12 = nil
75
+ elsif pkcs12_file.nil? || pkcs12_file.to_s.empty?
76
+ @pkcs12 = nil
77
+ elsif pkcs12_passphrase.nil? || pkcs12_passphrase.to_s.empty?
78
+ @pkcs12 = nil
79
+ else
80
+ OpenSSL::PKCS12.new(File.binread(ssl_path + '/' + pkcs12_file), pkcs12_passphrase)
81
+ end
71
82
  end
72
83
 
73
- def verify_mode(verify_mode)
74
- return OpenSSL::SSL::VERIFY_NONE if verify_mode.nil?
75
- return OpenSSL::SSL::VERIFY_NONE if verify_mode.to_s.empty?
84
+ def pkcs12_file=(pkcs12_file)
85
+ if pkcs12_file.nil? || pkcs12_file.to_s.empty?
86
+ @pkcs12_file = nil
87
+ else
88
+ @pkcs12_file = pkcs12_file
89
+ end
90
+ end
76
91
 
77
- verify_mode
92
+ def pkcs12_passphrase=(pkcs12_passphrase)
93
+ if pkcs12_passphrase.nil? || pkcs12_passphrase.to_s.empty?
94
+ @pkcs12_passphrase = nil
95
+ else
96
+ @pkcs12_passphrase = pkcs12_passphrase
97
+ end
78
98
  end
79
99
 
80
- module Ext
81
- # TODO: Define public / private
82
- # Also look into module_function and how that impacts attr_reader / attr_writer
83
- attr_reader :host,
84
- :path,
85
- :logger,
86
- :uri,
87
- :url,
88
- :use_proxy,
89
- :use_ssl
100
+ def read_timeout=(read_timeout)
101
+ @read_timeout = read_timeout.to_s.to_i
102
+ end
90
103
 
91
- attr_writer :host,
92
- :port,
93
- :path,
94
- :logger,
95
- :uri,
96
- :url,
97
- :use_proxy,
98
- :use_ssl
104
+ def ssl_path=(ssl_path)
105
+ if ssl_path.nil? || ssl_path.to_s.empty?
106
+ @ssl_path = nil
107
+ else
108
+ @ssl_path = ssl_path
109
+ end
110
+ end
99
111
 
100
- def proxy_uri=(proxy_uri)
101
- if proxy_uri.class.ancestors.include? URI
102
- @proxy_address = URI(proxy_uri)
103
- @proxy_port = URI(proxy_uri).port
104
- @proxy_uri = URI(proxy_uri)
105
- else
106
- @proxy_address = nil
107
- @proxy_port = nil
108
- @proxy_uri = nil
112
+ def use_ssl=(use_ssl, scheme)
113
+ @use_ssl = true
114
+ case use_ssl
115
+ when false
116
+ @use_ssl = false
117
+ when true
118
+ @use_ssl = true
119
+ else
120
+ case scheme.downcase
121
+ when 'http'
122
+ @use_ssl = false
123
+ when 'https'
124
+ @use_ssl = true
109
125
  end
110
126
  end
111
127
  end
112
- end
113
- end
114
128
 
115
- module Net
116
- class HTTP
117
- include NetHTTP::Core
118
- include NetHTTP::Client::Ext
129
+ def verify_mode=(verify_mode)
130
+ if verify_mode.nil? || verify_mode.to_s.empty?
131
+ @verify_mode = OpenSSL::SSL::VERIFY_NONE
132
+ else
133
+ @verify_mode = verify_mode
134
+ end
135
+ end
119
136
  end
120
137
  end
data/lib/client/schema.rb CHANGED
@@ -3,7 +3,7 @@
3
3
  require 'dry-validation'
4
4
 
5
5
  module NetHTTP
6
- module Client
6
+ class Client
7
7
  Schema = Dry::Validation.Schema do
8
8
  configure do
9
9
  def true_or_false
data/lib/core/core.rb CHANGED
@@ -1,75 +1,26 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'logger'
4
- require 'nokogiri'
5
- require 'uri'
6
4
 
7
5
  module NetHTTP
8
- module Core
9
- def logger
10
- @logger || send('logger=', nil)
11
- end
12
-
13
- def logger=(logger = nil)
14
- @logger = assign_logger(logger)
15
- end
16
-
17
- module_function
18
-
19
- def assign_logger(logger = nil)
6
+ class Core
7
+ def self.get_logger(logger = nil)
20
8
  return logger if logger.class == Logger
21
9
 
22
10
  if logger.nil? || logger.to_s.empty?
23
11
  logger = Logger.new(STDOUT)
24
12
  logger.level = Logger::INFO
25
13
  end
26
- logger
27
- end
28
-
29
- def construct_uri(scheme, host, port, path)
30
- return nil if host.nil?
31
-
32
- uri_scheme = case scheme
33
- when 'http', 'https'
34
- "#{scheme}://"
35
- end
36
- uri_port = if port.nil? || port.to_s.empty?
37
- nil
38
- else
39
- ":#{port.to_i}"
40
- end
41
- "#{uri_scheme}#{host}#{uri_port}#{path}"
42
- end
43
-
44
- def parse_uri(uri)
45
- return if uri.nil?
46
- return if uri.to_s.empty?
47
-
48
- parsed_uri = URI(uri)
49
-
50
- scheme = parsed_uri.to_s.scan(%r{(https|http):\/\/}).flatten[0]
51
- port = parsed_uri.to_s.scan(%r{:(\d+)\/}).flatten[0] ||= parsed_uri.port
52
-
53
- return URI(parsed_uri) if scheme == 'http'
54
- return URI(parsed_uri) if scheme == 'https'
55
14
 
56
- case port
57
- when 80, '80'
58
- URI("http://#{parsed_uri.to_s.gsub("#{scheme}://", '')}")
59
- else
60
- URI("https://#{parsed_uri.to_s.gsub("#{scheme}://", '')}")
61
- end
15
+ logger
62
16
  end
63
17
 
64
- def schema_validation(opts, schema, logger = nil)
18
+ def self.schema_validation(opts, schema, logger)
65
19
  results = schema.call(opts)
66
20
  return if results.success?
67
21
 
68
- if logger.nil? || logger.to_s.empty?
69
- else
70
- logger.debug("NetHTTP::Core::Error - schema input validation failed due to => #{results.messages}")
71
- end
72
- raise 'NetHTTP::Core::Error - schema input validation failed.'
22
+ logger.debug("NetHTTP::Core::SchemaValidationError - #{schema} input validation failed due to => #{results.messages}")
23
+ raise "NetHTTP::Core::SchemaValidationError - #{schema} input validation failed."
73
24
  end
74
25
  end
75
26
  end
@@ -2,15 +2,49 @@
2
2
 
3
3
  require 'json'
4
4
  require 'nokogiri'
5
+ require 'uri'
5
6
  require 'yaml'
6
7
 
7
8
  module NetHTTP
8
- module Core
9
- module Utilities
10
- module_function
9
+ class Core
10
+ class Utilities
11
+ def self.construct_uri(uri = {})
12
+ return nil if uri[:host].nil?
13
+
14
+ scheme = case uri[:scheme]
15
+ when 'http', 'https'
16
+ "#{uri[:scheme]}://"
17
+ end
18
+ port = if uri[:port].nil? || uri[:port].to_s.empty?
19
+ nil
20
+ else
21
+ ":#{uri[:port].to_i}"
22
+ end
23
+ "#{scheme}#{host}#{port}#{path}"
24
+ end
25
+
26
+ def self.parse_uri(uri)
27
+ return if uri.nil?
28
+ return if uri.to_s.empty?
29
+
30
+ parsed_uri = URI(uri)
31
+
32
+ scheme = parsed_uri.to_s.scan(%r{(https|http):\/\/}).flatten[0]
33
+ port = parsed_uri.to_s.scan(%r{:(\d+)\/}).flatten[0] ||= parsed_uri.port
34
+
35
+ return URI(parsed_uri) if scheme == 'http'
36
+ return URI(parsed_uri) if scheme == 'https'
37
+
38
+ case port
39
+ when 80, '80'
40
+ URI("http://#{parsed_uri.to_s.gsub("#{scheme}://", '')}")
41
+ else
42
+ URI("https://#{parsed_uri.to_s.gsub("#{scheme}://", '')}")
43
+ end
44
+ end
11
45
 
12
46
  # CamelCase to snake_case
13
- def camel_2_snake(key, type = nil)
47
+ def self.camel_2_snake(key, type = nil)
14
48
  new_key = key.to_s
15
49
  .tr('::', '/')
16
50
  .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
@@ -29,7 +63,7 @@ module NetHTTP
29
63
  end
30
64
 
31
65
  # snake_case to CamelCase
32
- def snake_2_camel(key, type = nil)
66
+ def self.snake_2_camel(key, type = nil)
33
67
  new_key = key.to_s.split('_')
34
68
  new_key = new_key[0].downcase + new_key[1..-1].map(&:capitalize).join('')
35
69
 
@@ -46,36 +80,36 @@ module NetHTTP
46
80
  # convert hash / array / nested object keys to either snake_case or CamelCase
47
81
  # format -> 'snake' or 'camel'
48
82
  # type -> 'string' or 'symbol'
49
- def convert_hash_keys(request = {})
50
- return request[:object] if request[:format].nil? && request[:type].nil?
83
+ def self.convert_hash_keys(opts = {})
84
+ return opts[:object] if opts[:format].nil? && opts[:type].nil?
51
85
 
52
- case request[:object]
86
+ case opts[:object]
53
87
  when Hash
54
88
  new_hash = {}
55
- request[:object].each do |key, value|
89
+ opts[:object].each do |key, value|
56
90
  value = convert_hash_keys(
57
91
  object: value,
58
- format: request[:format],
59
- type: request[:type]
92
+ format: opts[:format],
93
+ type: opts[:type]
60
94
  )
61
- case request[:format].downcase
95
+ case opts[:format].downcase
62
96
  when 'snake'
63
- new_key = camel_2_snake(key, request[:type])
97
+ new_key = camel_2_snake(key, opts[:type])
64
98
  when 'camel'
65
- new_key = snake_2_camel(key, request[:type])
99
+ new_key = snake_2_camel(key, opts[:type])
66
100
  end
67
101
  new_hash[new_key] = value
68
102
  end
69
103
  return new_hash
70
104
  when Array
71
- return request[:object].map { |value| convert_hash_keys(object: value, format: request[:format], type: request[:type]) }
105
+ return opts[:object].map { |value| convert_hash_keys(object: value, format: opts[:format], type: opts[:type]) }
72
106
  end
73
107
 
74
- request[:object]
108
+ opts[:object]
75
109
  end
76
110
 
77
111
  # Convert JSON doc to a Ruby Hash.
78
- def json_2_hash(json_doc, type = 'symbol', logger = nil)
112
+ def self.json_2_hash(json_doc, type = 'symbol', logger = nil)
79
113
  msg = "Invalid 'type' => #{type}. Use either 'string' or 'symbol' (default)."
80
114
  unless ['string', 'symbol'].include?(type.downcase)
81
115
  if logger.nil? || logger.to_s.empty?
@@ -100,7 +134,7 @@ module NetHTTP
100
134
  end
101
135
 
102
136
  # Convert XML doc to a Ruby Hash.
103
- def xml_2_hash(xml_doc, type = 'symbol', logger = nil)
137
+ def self.xml_2_hash(xml_doc, type = 'symbol', logger = nil)
104
138
  msg = "Invalid 'type' => #{type}. Use either 'string' or 'symbol' (default)."
105
139
  unless ['string', 'symbol'].include?(type.downcase)
106
140
  if logger.nil? || logger.to_s.empty?
@@ -123,7 +157,7 @@ module NetHTTP
123
157
  end
124
158
  end
125
159
 
126
- def format_xml_doc(xml_doc)
160
+ def self.format_xml_doc(xml_doc)
127
161
  case xml_doc
128
162
  when String
129
163
  formatted_xml_doc = Nokogiri::XML(xml_doc) { |c| c.options = Nokogiri::XML::ParseOptions::STRICT }
@@ -138,7 +172,7 @@ module NetHTTP
138
172
  end
139
173
 
140
174
  # Convert YAML doc to a Ruby Hash.
141
- def yaml_2_hash(yaml_doc, type = 'symbol', logger = nil)
175
+ def self.yaml_2_hash(yaml_doc, type = 'symbol', logger = nil)
142
176
  msg = "Invalid 'type' => #{type}. Use either 'string' or 'symbol' (default)."
143
177
  unless ['string', 'symbol'].include?(type.downcase)
144
178
  if logger.nil? || logger.to_s.empty?
@@ -167,7 +201,7 @@ module NetHTTP
167
201
  end
168
202
  end
169
203
 
170
- def valid_json?(json_doc, logger = nil)
204
+ def self.valid_json?(json_doc, logger = nil)
171
205
  begin
172
206
  return false if json_doc.nil?
173
207
  return false if json_doc.empty?
@@ -187,7 +221,7 @@ module NetHTTP
187
221
  true
188
222
  end
189
223
 
190
- def valid_xml?(xml_doc, logger = nil)
224
+ def self.valid_xml?(xml_doc, logger = nil)
191
225
  begin
192
226
  return false if xml_doc.nil?
193
227
  return false if xml_doc.empty?
@@ -210,7 +244,7 @@ module NetHTTP
210
244
  true
211
245
  end
212
246
 
213
- def valid_html?(html_doc, logger = nil)
247
+ def self.valid_html?(html_doc, logger = nil)
214
248
  begin
215
249
  return false if html_doc.nil?
216
250
  return false if html_doc.empty?
@@ -0,0 +1,149 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'schema'
4
+ require_relative '../core/core'
5
+
6
+ module NetHTTP
7
+ class Client
8
+ def call_web_service(request_opts = {})
9
+ @response = case request_opts[:method].to_s.upcase
10
+ when 'DELETE'
11
+ delete(request_opts)
12
+ when 'GET'
13
+ get(request_opts)
14
+ when 'POST'
15
+ post(request_opts)
16
+ when 'POST_FORM', 'POST_FORM_DATA'
17
+ post_form(request_opts)
18
+ when 'PUT'
19
+ put(request_opts)
20
+ else
21
+ logger.debug("Request method => '#{request_opts[:method]}' not yet supported.")
22
+ raise "Request method => '#{request_opts[:method]}' not yet supported."
23
+ end
24
+
25
+ @response
26
+ end
27
+
28
+ def delete(opts = {})
29
+ raise 'Empty DELETE request options provided.' if opts.empty?
30
+
31
+ opts[:method] = 'delete'
32
+ request_opts = request_opts(opts)
33
+ logger.debug('Request Method => ' + request_opts[:method])
34
+ logger.debug('Request Host => ' + request_opts[:uri].scheme.to_s + request_opts[:uri].host.to_s)
35
+ logger.debug('Request Port => ' + request_opts[:uri].port.to_s)
36
+ logger.debug('Request Path => ' + request_opts[:path])
37
+ Response.new(
38
+ response: client.delete(
39
+ request_opts[:path]
40
+ ),
41
+ logger: logger
42
+ )
43
+ end
44
+
45
+ def get(opts = {})
46
+ raise 'Empty GET request options provided.' if opts.empty?
47
+
48
+ opts[:method] = 'get'
49
+ request_opts = request_opts(opts)
50
+ logger.debug('Request Method => ' + request_opts[:method])
51
+ logger.debug('Request Host => ' + request_opts[:uri].scheme.to_s + request_opts[:uri].host.to_s)
52
+ logger.debug('Request Port => ' + request_opts[:uri].port.to_s)
53
+ logger.debug('Request Path => ' + request_opts[:path])
54
+ logger.debug('Request Headers =>')
55
+ logger.debug(request_opts[:headers])
56
+ Response.new(
57
+ response: client.get(
58
+ request_opts[:path],
59
+ request_opts[:headers]
60
+ ),
61
+ logger: logger
62
+ )
63
+ end
64
+
65
+ def post(opts = {})
66
+ raise 'Empty POST request options provided.' if opts.empty?
67
+
68
+ opts[:method] = 'post'
69
+ request_opts = request_opts(opts)
70
+ logger.debug('Request Method => ' + request_opts[:method])
71
+ logger.debug('Request Host => ' + request_opts[:uri].scheme.to_s + request_opts[:uri].host.to_s)
72
+ logger.debug('Request Port => ' + request_opts[:uri].port.to_s)
73
+ logger.debug('Request Path => ' + request_opts[:path])
74
+ logger.debug('Request Headers =>')
75
+ logger.debug(request_opts[:headers])
76
+ logger.debug('Request Body =>')
77
+ logger.debug(request_opts[:body])
78
+ Response.new(
79
+ response: client.post(
80
+ request_opts[:path],
81
+ request_opts[:body],
82
+ request_opts[:headers]
83
+ ),
84
+ logger: logger
85
+ )
86
+ end
87
+
88
+ def post_form(opts = {})
89
+ raise 'Empty POST_FORM request options provided.' if opts.empty?
90
+
91
+ opts[:method] = 'post_form'
92
+ request_opts = request_opts(opts)
93
+ logger.debug('Request Method => ' + request_opts[:method])
94
+ logger.debug('Request Host => ' + request_opts[:uri].scheme.to_s + request_opts[:uri].host.to_s)
95
+ logger.debug('Request Port => ' + request_opts[:uri].port.to_s)
96
+ logger.debug('Request Path => ' + request_opts[:path])
97
+ logger.debug('Request Headers =>')
98
+ logger.debug(request_opts[:headers])
99
+ logger.debug('Request Body =>')
100
+ logger.debug(URI.encode_www_form(request_opts[:body]))
101
+ Response.new(
102
+ response: client.post(
103
+ uri,
104
+ URI.encode_www_form(request_opts[:body]),
105
+ request_opts[:headers]
106
+ ),
107
+ logger: logger
108
+ )
109
+ end
110
+
111
+ alias post_form_data post_form
112
+
113
+ def put(opts = {})
114
+ raise 'Empty PUT request options provided.' if opts.empty?
115
+
116
+ opts[:method] = 'put'
117
+ request_opts = request_opts(opts)
118
+ logger.debug('Request Method => ' + request_opts[:method])
119
+ logger.debug('Request Host => ' + request_opts[:uri].scheme.to_s + request_opts[:uri].host.to_s)
120
+ logger.debug('Request Port => ' + request_opts[:uri].port.to_s)
121
+ logger.debug('Request Path => ' + request_opts[:path])
122
+ logger.debug('Request Headers =>')
123
+ logger.debug(request_opts[:headers])
124
+ logger.debug('Request Body =>')
125
+ logger.debug(request_opts[:body])
126
+ Response.new(
127
+ response: client.put(
128
+ request_opts[:path],
129
+ request_opts[:body],
130
+ request_opts[:headers]
131
+ ),
132
+ logger: logger
133
+ )
134
+ end
135
+
136
+ def request_opts(opts = {})
137
+ Core.schema_validation(opts, NetHTTP::Request::Schema, logger) unless opts[:enforce_schema_validation] == false
138
+
139
+ request_opts = {}
140
+ request_opts[:method] = opts[:method] ||= 'post'
141
+ request_opts[:uri] = opts[:uri] ||= opts[:url] ||= uri
142
+ request_opts[:path] = opts[:path] ||= request_opts[:uri].path ||= path
143
+ request_opts[:path] = (request_opts[:path] + '?' + request_opts[:uri].query) unless request_opts[:uri].query.nil? || request_opts[:path].include?('?')
144
+ request_opts[:headers] = opts[:headers] ||= {}
145
+ request_opts[:body] = opts[:body] ||= nil
146
+ request_opts
147
+ end
148
+ end
149
+ end
@@ -3,13 +3,10 @@
3
3
  require 'dry-validation'
4
4
 
5
5
  module NetHTTP
6
- module Request
6
+ class Request
7
7
  Schema = Dry::Validation.Schema do
8
- optional(:req_method).filled(type?: String)
9
- optional(:method).filled(type?: String)
10
- optional(:req_headers).maybe(type?: Hash)
8
+ required(:method).filled(type?: String)
11
9
  optional(:headers).maybe(type?: Hash)
12
- optional(:req_body).maybe
13
10
  optional(:body).maybe
14
11
 
15
12
  rule(if_url_and_uri_are_nil_must_provide_path: [:uri, :url, :path]) do |uri, url, path|
@@ -0,0 +1,131 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'json'
4
+ require 'nokogiri'
5
+ require 'ostruct'
6
+ require_relative 'schema'
7
+ require_relative '../core/core'
8
+ require_relative '../core/utilities'
9
+
10
+ module NetHTTP
11
+ class Response
12
+ attr_reader :logger,
13
+ :response
14
+
15
+ def initialize(opts = {})
16
+ send('logger=', opts[:logger])
17
+ puts opts[:response].class
18
+ Core.schema_validation(opts, NetHTTP::Response::Schema, logger) unless opts[:enforce_schema_validation] == false
19
+ @response = opts[:response]
20
+ log_response
21
+ end
22
+
23
+ alias resp response
24
+
25
+ def body
26
+ response.body
27
+ end
28
+
29
+ alias resp_body body
30
+ alias response_body body
31
+
32
+ def body_hash
33
+ begin
34
+ return NetHTTP::Core::Utilities.json_2_hash(body, 'symbol', logger) if valid_json?
35
+ rescue JSON::ParserError => err
36
+ logger.debug(err)
37
+ end
38
+ begin
39
+ return NetHTTP::Core::Utilities.xml_2_hash(body, 'symbol', logger) if valid_xml?
40
+ rescue Nokogiri::XML::SyntaxError => err
41
+ logger.debug(err)
42
+ end
43
+ begin
44
+ # TODO: revisit this if necessary
45
+ return NetHTTP::Core::Utilities.xml_2_hash(body, 'symbol', logger) if valid_html?
46
+ rescue Nokogiri::XML::SyntaxError => err
47
+ logger.debug(err)
48
+ end
49
+
50
+ {}
51
+ end
52
+
53
+ alias resp_body_hash body_hash
54
+ alias response_body_hash body_hash
55
+
56
+ def body_os
57
+ JSON.parse(body_hash.to_json, object_class: OpenStruct)
58
+ end
59
+
60
+ alias resp_body_os body_os
61
+ alias response_body_os body_os
62
+ alias body_open_struct body_os
63
+ alias resp_body_open_struct body_os
64
+ alias response_body_open_struct body_os
65
+
66
+ def code
67
+ response.code
68
+ end
69
+
70
+ alias resp_code code
71
+ alias response_code code
72
+
73
+ def headers
74
+ response_headers = {}
75
+ response.to_hash.each do |key, value|
76
+ response_headers[key] = value.flatten[0].to_s
77
+ end
78
+ end
79
+
80
+ alias resp_headers headers
81
+ alias response_headers headers
82
+
83
+ def headers_hash
84
+ NetHTTP::Core::Utilities.convert_hash_keys(
85
+ object: headers,
86
+ format: 'snake',
87
+ type: 'symbol'
88
+ )
89
+ end
90
+
91
+ alias resp_headers_hash headers_hash
92
+ alias response_headers_hash headers_hash
93
+
94
+ def headers_os
95
+ JSON.parse(headers_hash.to_json, object_class: OpenStruct)
96
+ end
97
+
98
+ alias resp_headers_os headers_os
99
+ alias response_headers_os headers_os
100
+ alias headers_open_struct headers_os
101
+ alias resp_headers_struct headers_os
102
+ alias response_headers_open_struct headers_os
103
+
104
+ def valid_json?
105
+ NetHTTP::Core::Utilities.valid_json?(body, logger)
106
+ end
107
+
108
+ def valid_xml?
109
+ NetHTTP::Core::Utilities.valid_xml?(body, logger)
110
+ end
111
+
112
+ def valid_html?
113
+ NetHTTP::Core::Utilities.valid_html?(body, logger)
114
+ end
115
+
116
+ private :logger,
117
+ :response
118
+
119
+ def logger=(logger = nil)
120
+ @logger = Core.get_logger(logger)
121
+ end
122
+
123
+ def log_response
124
+ logger.debug('Response Code => ' + code)
125
+ logger.debug('Response Headers => ')
126
+ logger.debug(headers)
127
+ logger.debug('Response Body => ')
128
+ logger.debug(body)
129
+ end
130
+ end
131
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'dry-validation'
4
+
5
+ module NetHTTP
6
+ class Response
7
+ Schema = Dry::Validation.Schema do
8
+ required(:response).filled(type?: Net::HTTPResponse)
9
+ end
10
+ end
11
+ end
data/lib/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module NetHTTP
4
- VERSION = '0.0.4'
4
+ VERSION = '1.0.1'
5
5
  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: 0.0.4
4
+ version: 1.0.1
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-02-22 00:00:00.000000000 Z
11
+ date: 2019-02-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: dry-validation
@@ -206,9 +206,10 @@ files:
206
206
  - lib/core/core.rb
207
207
  - lib/core/utilities.rb
208
208
  - lib/rb-net_http-client.rb
209
- - lib/request/ext.rb
209
+ - lib/request/request.rb
210
210
  - lib/request/schema.rb
211
- - lib/response/ext.rb
211
+ - lib/response/response.rb
212
+ - lib/response/schema.rb
212
213
  - lib/version.rb
213
214
  - spec/integration/net_http/client/client_ext_spec.rb
214
215
  - spec/integration/net_http/client/client_spec.rb
@@ -244,8 +245,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
244
245
  - !ruby/object:Gem::Version
245
246
  version: '0'
246
247
  requirements: []
247
- rubyforge_project:
248
- rubygems_version: 2.7.7
248
+ rubygems_version: 3.0.2
249
249
  signing_key:
250
250
  specification_version: 4
251
251
  summary: A Simple Ruby HTTP Client
data/lib/request/ext.rb DELETED
@@ -1,87 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative '../core/core'
4
- require_relative 'schema'
5
-
6
- module NetHTTP
7
- module Request
8
- module Ext
9
- def call_web_service(opts = {})
10
- logger = Core.assign_logger(self.logger ||= opts[:logger])
11
-
12
- Core.schema_validation(opts, NetHTTP::Request::Schema, logger) unless opts[:enforce_schema_validation] == false
13
-
14
- method = (opts[:method] ||= opts[:req_method] ||= opts[:request_method] ||= 'post').to_s.upcase
15
- uri = Core.parse_uri(opts[:uri] ||= opts[:url] ||= self.uri)
16
- path = opts[:path] ||= opts[:req_path] ||= opts[:request_path] ||= opts[:path] ||= uri.path ||= self.path
17
- path = (path + '?' + uri.query) unless uri.query.nil? || path.include?('?')
18
- headers = opts[:headers] ||= opts[:req_headers] ||= opts[:request_headers] ||= {}
19
- body = opts[:body] ||= opts[:req_body] ||= opts[:request_body] ||= nil
20
-
21
- resp = case method
22
- when 'DELETE'
23
- logger.debug('Request Method => ' + method)
24
- logger.debug('Request Host => ' + uri.host.to_s)
25
- logger.debug('Request Path => ' + path)
26
- delete(path)
27
- when 'GET'
28
- logger.debug('Request Method => ' + method)
29
- logger.debug('Request Host => ' + uri.host.to_s)
30
- logger.debug('Request Path => ' + path)
31
- logger.debug('Request Headers =>')
32
- logger.debug(headers)
33
- get(path, headers)
34
- when 'POST'
35
- logger.debug('Request Method => ' + method)
36
- logger.debug('Request Host => ' + uri.host.to_s)
37
- logger.debug('Request Path => ' + path)
38
- logger.debug('Request Headers =>')
39
- logger.debug(headers)
40
- logger.debug('Request Body =>')
41
- logger.debug(body)
42
- post(path, body, headers)
43
- when 'POST_FORM', 'POST_FORM_DATA'
44
- logger.debug('Request Method => ' + method)
45
- logger.debug('Request Host => ' + uri.host.to_s)
46
- logger.debug('Request Path => ' + path)
47
- logger.debug('Request Headers =>')
48
- logger.debug(headers)
49
- logger.debug('Request Body =>')
50
- logger.debug(URI.encode_www_form(body))
51
- post(uri, URI.encode_www_form(body), headers)
52
- when 'PUT'
53
- logger.debug('Request Method => ' + method)
54
- logger.debug('Request Host => ' + uri.host.to_s)
55
- logger.debug('Request Path => ' + path)
56
- logger.debug('Request Headers =>')
57
- logger.debug(headers)
58
- logger.debug('Request Body =>')
59
- logger.debug(body)
60
- put(path, body, headers)
61
- else
62
- logger.debug("Request method => '#{method}' not yet supported.")
63
- raise "Request method => '#{method}' not yet supported."
64
- end
65
- logger.debug('Response Code => ' + resp.code)
66
- logger.debug('Response Headers => ')
67
- logger.debug(resp.headers)
68
- logger.debug('Response Body => ')
69
- logger.debug(resp.body)
70
- resp
71
- end
72
-
73
- alias call_api call_web_service
74
- alias call_service call_web_service
75
- alias execute_request call_web_service
76
- alias send_request call_web_service
77
- alias submit_request call_web_service
78
- end
79
- end
80
- end
81
-
82
- module Net
83
- class HTTP
84
- include NetHTTP::Core
85
- include NetHTTP::Request::Ext
86
- end
87
- end
data/lib/response/ext.rb DELETED
@@ -1,111 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'json'
4
- require 'nokogiri'
5
- require 'ostruct'
6
- require_relative '../core/core'
7
- require_relative '../core/utilities'
8
-
9
- module NetHTTP
10
- module Response
11
- module Ext
12
- def resp_code
13
- code
14
- end
15
-
16
- alias response_code resp_code
17
-
18
- def headers
19
- resp_headers = {}
20
- headers = to_hash
21
- headers.each do |key, value|
22
- resp_headers[key] = value.flatten[0].to_s
23
- end
24
- end
25
-
26
- alias resp_headers headers
27
- alias response_headers headers
28
-
29
- def headers_hash
30
- NetHTTP::Core::Utilities.convert_hash_keys(
31
- object: headers,
32
- format: 'snake',
33
- type: 'symbol'
34
- )
35
- end
36
-
37
- alias resp_headers_hash headers_hash
38
- alias response_headers_hash headers_hash
39
-
40
- def headers_os
41
- JSON.parse(headers_hash.to_json, object_class: OpenStruct)
42
- end
43
-
44
- alias resp_headers_os headers_os
45
- alias response_headers_os headers_os
46
- alias headers_open_struct headers_os
47
- alias resp_headers_struct headers_os
48
- alias response_headers_open_struct headers_os
49
-
50
- def resp_body
51
- body
52
- end
53
-
54
- alias response_body resp_body
55
-
56
- def body_hash
57
- begin
58
- return NetHTTP::Core::Utilities.json_2_hash(body, 'symbol', logger) if valid_json?
59
- rescue JSON::ParserError => err
60
- logger.debug(err)
61
- end
62
- begin
63
- return NetHTTP::Core::Utilities.xml_2_hash(body, 'symbol', logger) if valid_xml?
64
- rescue Nokogiri::XML::SyntaxError => err
65
- logger.debug(err)
66
- end
67
- begin
68
- # TODO: update this to work if necessary
69
- # probably makes more sense to parse HTML using nokogiri
70
- return NetHTTP::Core::Utilities.xml_2_hash(body, 'symbol', logger) if valid_html?
71
- rescue Nokogiri::XML::SyntaxError => err
72
- logger.debug(err)
73
- end
74
-
75
- {}
76
- end
77
-
78
- alias resp_body_hash body_hash
79
- alias response_body_hash body_hash
80
-
81
- def body_os
82
- JSON.parse(body_hash.to_json, object_class: OpenStruct)
83
- end
84
-
85
- alias resp_body_os body_os
86
- alias response_body_os body_os
87
- alias body_open_struct body_os
88
- alias resp_body_open_struct body_os
89
- alias response_body_open_struct body_os
90
-
91
- def valid_json?
92
- NetHTTP::Core::Utilities.valid_json?(body, logger)
93
- end
94
-
95
- def valid_xml?
96
- NetHTTP::Core::Utilities.valid_xml?(body, logger)
97
- end
98
-
99
- def valid_html?
100
- NetHTTP::Core::Utilities.valid_html?(body, logger)
101
- end
102
- end
103
- end
104
- end
105
-
106
- module Net
107
- class HTTPResponse
108
- include NetHTTP::Core
109
- include NetHTTP::Response::Ext
110
- end
111
- end