http_requests 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/connection.rb +92 -0
- data/lib/http_requests.rb +1 -0
- data/lib/single_connection.rb +26 -0
- metadata +48 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: d8aea7ee8220161a65e6c9c0ca08f6c4f9224d5a
|
4
|
+
data.tar.gz: b6c916f09ff3e03d1204d9aa222ec9db94332444
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: efcda325be9b37daeb0a8193874a0b499c1e24d6b91c921350dc89bac2e035377c6104acead697caaf2c422e5b4f1f687de08e67263c7abeaa28ab30f8313b30
|
7
|
+
data.tar.gz: acfd54141e2e2c38447c598258398746ee13eeaddd0bcc7425fcd53b575f907827e8b87343004111351d393f4cb2ccd2b1b65f487731f06be65c1a765d0e1cfa
|
data/lib/connection.rb
ADDED
@@ -0,0 +1,92 @@
|
|
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
|
@@ -0,0 +1 @@
|
|
1
|
+
%w{connection single_connection}.each { |file| require_relative file }
|
@@ -0,0 +1,26 @@
|
|
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
|
metadata
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: http_requests
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Wanderson Silva
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-02-06 00:00:00.000000000 Z
|
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.
|
16
|
+
email: wanderson.olivs@gmail.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- lib/connection.rb
|
22
|
+
- lib/http_requests.rb
|
23
|
+
- lib/single_connection.rb
|
24
|
+
homepage: https://github.com/silvawand/wand-ruby-gem-http-requests
|
25
|
+
licenses:
|
26
|
+
- MIT
|
27
|
+
metadata: {}
|
28
|
+
post_install_message:
|
29
|
+
rdoc_options: []
|
30
|
+
require_paths:
|
31
|
+
- lib
|
32
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
33
|
+
requirements:
|
34
|
+
- - ">="
|
35
|
+
- !ruby/object:Gem::Version
|
36
|
+
version: '0'
|
37
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
42
|
+
requirements: []
|
43
|
+
rubyforge_project:
|
44
|
+
rubygems_version: 2.5.2.1
|
45
|
+
signing_key:
|
46
|
+
specification_version: 4
|
47
|
+
summary: A simple lib that provides easy methods to make HTTP and HTTP requests.
|
48
|
+
test_files: []
|