http_requests 1.0.0 → 1.1.1

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
- SHA1:
3
- metadata.gz: d8aea7ee8220161a65e6c9c0ca08f6c4f9224d5a
4
- data.tar.gz: b6c916f09ff3e03d1204d9aa222ec9db94332444
2
+ SHA256:
3
+ metadata.gz: 4e4092264fc60ea83603b9e29a97b4cb82802807f9c0a0f67b20e3fa149024ea
4
+ data.tar.gz: a4def03105929eff2a63bd3c9b6f8248f8a2b419e56f20f97d70de842de651b4
5
5
  SHA512:
6
- metadata.gz: efcda325be9b37daeb0a8193874a0b499c1e24d6b91c921350dc89bac2e035377c6104acead697caaf2c422e5b4f1f687de08e67263c7abeaa28ab30f8313b30
7
- data.tar.gz: acfd54141e2e2c38447c598258398746ee13eeaddd0bcc7425fcd53b575f907827e8b87343004111351d393f4cb2ccd2b1b65f487731f06be65c1a765d0e1cfa
6
+ metadata.gz: 505d9ae0e566a07fe6bc0f66199f183ce93215aa12e5decb5921de4aa2c9657ac2578c54a2dafcd2b3064c508548cf0a75389e49137e4f4771df376d873c3c0f
7
+ data.tar.gz: 82fed4e66e7c6864fd002fdbf18364091c118a3d63a31f36a6b3b96a61d7ab68e9ca1bd9338ed7cb67db581933d862a7033787133a803320169a38069617b24d
@@ -0,0 +1,94 @@
1
+ require 'net/https'
2
+ require 'cgi'
3
+
4
+ # Connection allows the creation of an HTTP/HTTPS connection
5
+ # that allows the issues of HTTP requests.
6
+ class HTTPConn
7
+ attr_accessor :settings
8
+ attr_reader :url
9
+
10
+ def initialize(url, settings = {})
11
+ @started = false
12
+ @http = nil
13
+ @url = url
14
+
15
+ self.settings = {
16
+ header: {},
17
+ ssl: false,
18
+ cert: "",
19
+ read_timeout: 60
20
+ }
21
+ self.settings.merge!(settings)
22
+ end
23
+
24
+ def get(options = {})
25
+ default = {
26
+ end_point: "",
27
+ header: {},
28
+ query_str: {}
29
+ }
30
+ options = default.merge(options)
31
+ url = query_str("#{@url}#{options[:end_point]}", options[:query_str])
32
+
33
+ uri = URI(url)
34
+ http = @started ? @http : get_http(uri)
35
+ request = Net::HTTP::Get.new(uri.request_uri,
36
+ settings[:header].merge(options[:header]))
37
+ http.request(request)
38
+ end
39
+
40
+ def post(options = {})
41
+ default = {
42
+ end_point: "",
43
+ header: {},
44
+ body: nil
45
+ }
46
+ options = default.merge(options)
47
+ uri = URI("#{@url}#{options[:end_point]}")
48
+
49
+ http = @started ? @http : get_http(uri)
50
+ request = Net::HTTP::Post.new(uri.request_uri,
51
+ settings[:header].merge(options[:header]))
52
+ http.request(request, options[:body])
53
+ end
54
+
55
+ def start(uri = nil)
56
+ @started = true
57
+ @http = uri.nil? ? get_http(URI(@url)) : get_http(URI(uri))
58
+ @http.start
59
+ yield
60
+ @http.finish # Close connection after usage in 'yield'.
61
+ @started = false
62
+ end
63
+
64
+ private
65
+
66
+ def query_str(url, data)
67
+ unless data.empty?
68
+ first = data.keys[0]
69
+ url = "#{url}?#{first}=#{data[first]}"
70
+
71
+ data.each_key do |key|
72
+ next if key == first
73
+ url = "#{url}&#{key}=#{data[key]}"
74
+ end
75
+ end
76
+ url
77
+ end
78
+
79
+ def get_http(uri)
80
+ http = Net::HTTP.new(uri.host, uri.port)
81
+ http.read_timeout = settings[:read_timeout]
82
+
83
+ if settings[:ssl]
84
+ http.use_ssl = true
85
+ if settings[:cert].empty?
86
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
87
+ else
88
+ http.verify_mode = OpenSSL::SSL::VERIFY_PEER
89
+ http.cs_file = settings[:cert]
90
+ end
91
+ end
92
+ http
93
+ end
94
+ end
@@ -1 +1 @@
1
- %w{connection single_connection}.each { |file| require_relative file }
1
+ %w[http_conn single_conn].each { |file| require_relative file }
@@ -0,0 +1,27 @@
1
+ require_relative 'http_conn'
2
+
3
+ # SingleConn
4
+ module SingleConn
5
+ extend self
6
+
7
+ def init(url, settings = {})
8
+ @conn = HTTPConn.new(url, settings)
9
+ end
10
+
11
+ def conn_settings
12
+ @conn.settings
13
+ end
14
+
15
+ def get(options = {})
16
+ @conn.get(options)
17
+ end
18
+
19
+ def post(options = {})
20
+ @conn.post(options)
21
+ end
22
+
23
+ def start(uri = nil)
24
+ uri = uri.nil? ? URI(@conn.url) : URI(uri)
25
+ @conn.start(uri) { yield }
26
+ end
27
+ end
metadata CHANGED
@@ -1,26 +1,25 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: http_requests
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Wanderson Silva
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-02-06 00:00:00.000000000 Z
11
+ date: 2018-03-16 00:00:00.000000000 Z
12
12
  dependencies: []
13
- description: |-
14
- http_requests is a lib that provides an easy way to create an HTTP or HTTPS
15
- connection and issue requests to it.
13
+ description: http_requests is a lib that provides an easy way to create an HTTP/HTTPS
14
+ connection and issue requests to it.
16
15
  email: wanderson.olivs@gmail.com
17
16
  executables: []
18
17
  extensions: []
19
18
  extra_rdoc_files: []
20
19
  files:
21
- - lib/connection.rb
20
+ - lib/http_conn.rb
22
21
  - lib/http_requests.rb
23
- - lib/single_connection.rb
22
+ - lib/single_conn.rb
24
23
  homepage: https://github.com/silvawand/wand-ruby-gem-http-requests
25
24
  licenses:
26
25
  - MIT
@@ -33,7 +32,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
33
32
  requirements:
34
33
  - - ">="
35
34
  - !ruby/object:Gem::Version
36
- version: '0'
35
+ version: 1.9.0
37
36
  required_rubygems_version: !ruby/object:Gem::Requirement
38
37
  requirements:
39
38
  - - ">="
@@ -41,7 +40,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
41
40
  version: '0'
42
41
  requirements: []
43
42
  rubyforge_project:
44
- rubygems_version: 2.5.2.1
43
+ rubygems_version: 2.7.5
45
44
  signing_key:
46
45
  specification_version: 4
47
46
  summary: A simple lib that provides easy methods to make HTTP and HTTP requests.
@@ -1,92 +0,0 @@
1
- require 'net/https'
2
- require 'cgi'
3
-
4
- class Connection
5
- attr_accessor :settings
6
- attr_reader :url
7
-
8
- def initialize(url, settings = {})
9
- @started = false
10
- @http = nil
11
- @url = url
12
-
13
- @settings = {
14
- :header => {},
15
- :ssl => false,
16
- :cert => "",
17
- :read_timeout => 60
18
- }
19
- @settings.merge!(settings)
20
- end
21
-
22
- def get(options = {})
23
- default = {
24
- :end_point => "",
25
- :header => {},
26
- :query_str => {}
27
- }
28
- options = default.merge(options)
29
-
30
- url = "#{@url}#{options[:end_point]}"
31
-
32
- unless options[:query_str].empty?
33
- url = "#{url}?"
34
- first = options[:query_str].keys[0]
35
- url = "#{url}#{first}=#{options[:query_str][first]}"
36
-
37
- options[:query_str].each_key do |key|
38
- next if key == first
39
- url = "#{url}&#{key}=#{options[:query_str][key]}"
40
- end
41
- end
42
-
43
- uri = URI(url)
44
-
45
- http = @started ? @http : get_http(uri)
46
- request = Net::HTTP::Get.new(uri.request_uri, @settings[:header].merge(options[:header]))
47
- http.request(request)
48
- end
49
-
50
- def post(options = {})
51
- default = {
52
- :end_point => "",
53
- :header => {},
54
- :body => nil
55
- }
56
- options = default.merge(options)
57
- uri = URI("#{@url}#{options[:end_point]}")
58
-
59
- http = @started ? @http : get_http(uri)
60
- request = Net::HTTP::Post.new(uri.request_uri, @settings[:header].merge(options[:header]))
61
-
62
- http.request(request, options[:body])
63
- end
64
-
65
- def start(uri = nil)
66
- @started = true
67
- @http = uri.nil? ? get_http(URI(@url)) : get_http(URI(uri))
68
- @http.start
69
- yield
70
- @http.finish # Close connection after usage
71
- @started = false
72
- end
73
-
74
- private
75
-
76
- def get_http(uri)
77
- http = Net::HTTP.new(uri.host, uri.port)
78
- http.read_timeout = @settings[:read_timeout]
79
-
80
- if @settings[:ssl]
81
- http.use_ssl = true
82
- if @settings[:cert].empty?
83
- http.verify_mode = OpenSSL::SSL::VERIFY_NONE
84
- else
85
- http.verify_mode = OpenSSL::SSL::VERIFY_PEER
86
- http.cs_file = @settings[:cert]
87
- end
88
- end
89
-
90
- return http
91
- end
92
- end
@@ -1,26 +0,0 @@
1
- require_relative 'connection'
2
-
3
- module SingleConnection
4
- extend self
5
-
6
- def init(url, settings = {})
7
- @connection = Connection.new(url, settings)
8
- end
9
-
10
- def get_conn_settings
11
- @connection.settings
12
- end
13
-
14
- def get(options = {})
15
- @connection.get(options)
16
- end
17
-
18
- def post(options = {})
19
- @connection.post(options)
20
- end
21
-
22
- def start(uri = nil)
23
- arg = uri.nil? ? URI(@connection.url) : URI(uri)
24
- @connection.start(arg) { |connection| yield }
25
- end
26
- end