simple-http-helper 0.0.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 +7 -0
- data/lib/helpers/simple_http_helper.rb +236 -0
- metadata +44 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: a4c5c31988ebdb707ce4b547d289c40ddb90ba05
|
4
|
+
data.tar.gz: 72ec3c6de6af4efaade98f989ffd1b21b66593eb
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: f12ed4091005e4226c65b4629f31a652c49ec872f8e20da767b2fc382c96ebe19e4834fb49afa9833597df3a26101f178ca8d2ad49189178b4685b2636b6888c
|
7
|
+
data.tar.gz: f552c417989d4e9de934062f283e102e8e5ef41c7a52eb5047f0473eef328c0fc6e5ed726c69126d993469173b4bd92019c4e9b91eddeed04c6acace5f6d1d7e
|
@@ -0,0 +1,236 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
require 'net/https'
|
3
|
+
require 'json'
|
4
|
+
|
5
|
+
module HelperModule
|
6
|
+
|
7
|
+
# TODO: add SimpleHttpsHelper to support https requests
|
8
|
+
class SimpleRequestHelper
|
9
|
+
attr_reader :host, :port, :path
|
10
|
+
attr_accessor :_debug
|
11
|
+
|
12
|
+
def initialize(host, port, path)
|
13
|
+
@host, @port, @path = host, port, path
|
14
|
+
end
|
15
|
+
|
16
|
+
def _request_prefix
|
17
|
+
raise NotImplementedError
|
18
|
+
end
|
19
|
+
|
20
|
+
def _create_request_sender(host, port)
|
21
|
+
raise NotImplementedError
|
22
|
+
end
|
23
|
+
|
24
|
+
def _get_base_uri(*args)
|
25
|
+
|
26
|
+
port_str = @port && ":#{@port}"
|
27
|
+
path_str = @path
|
28
|
+
args_str = args.join('/')
|
29
|
+
args_str = "/" + args_str if not args_str.empty?
|
30
|
+
|
31
|
+
URI(URI.escape("#{_request_prefix}://#{@host}#{port_str}/#{path_str}#{args_str}"))
|
32
|
+
end
|
33
|
+
|
34
|
+
def _send_request(content_klass, *args, &block)
|
35
|
+
uri = _get_base_uri(*args)
|
36
|
+
content = content_klass.new(uri)
|
37
|
+
|
38
|
+
block.call(content) if block_given?
|
39
|
+
|
40
|
+
if @_debug
|
41
|
+
puts content.get_request.uri
|
42
|
+
end
|
43
|
+
|
44
|
+
response = _create_request_sender(@host, @port).start do |http|
|
45
|
+
http.request(content.get_request)
|
46
|
+
end
|
47
|
+
|
48
|
+
return SimpleHttpResult.new(response)
|
49
|
+
end
|
50
|
+
|
51
|
+
def post(*args, &block)
|
52
|
+
return _send_request(PostContent, *args, &block)
|
53
|
+
end
|
54
|
+
|
55
|
+
def get(*args, &block)
|
56
|
+
return _send_request(GetContent, *args, &block)
|
57
|
+
end
|
58
|
+
|
59
|
+
def patch(*args, &block)
|
60
|
+
return _send_request(PatchContent, *args, &block)
|
61
|
+
end
|
62
|
+
|
63
|
+
def delete(*args, &block)
|
64
|
+
return _send_request(DeleteContent, *args, &block)
|
65
|
+
end
|
66
|
+
|
67
|
+
def loginsight_get(*args, &block)
|
68
|
+
return _send_request(LoginsightGetContent, *args, &block)
|
69
|
+
end
|
70
|
+
|
71
|
+
private :_get_base_uri, :_send_request
|
72
|
+
end
|
73
|
+
|
74
|
+
class SimpleHttpsHelper < SimpleRequestHelper
|
75
|
+
def _request_prefix
|
76
|
+
'https'
|
77
|
+
end
|
78
|
+
|
79
|
+
def _create_request_sender(host, port)
|
80
|
+
Net::HTTP.new(host, port).tap do |http|
|
81
|
+
http.use_ssl = true
|
82
|
+
#http.set_debug_output($stdout)
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
class SimpleHttpHelper < SimpleRequestHelper
|
88
|
+
def _request_prefix
|
89
|
+
'http'
|
90
|
+
end
|
91
|
+
|
92
|
+
def _create_request_sender(host, port)
|
93
|
+
Net::HTTP.new(host, port).tap do |http|
|
94
|
+
#http.set_debug_output($stdout)
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
class SimpleHttpContent
|
100
|
+
def initialize(uri)
|
101
|
+
@uri = uri
|
102
|
+
@headers = {}
|
103
|
+
end
|
104
|
+
|
105
|
+
def get_request
|
106
|
+
raise NotImplementedError
|
107
|
+
end
|
108
|
+
|
109
|
+
def add_parameters(params = {})
|
110
|
+
raise NotImplementedError
|
111
|
+
end
|
112
|
+
|
113
|
+
def get_request_klass
|
114
|
+
raise NotImplementedError
|
115
|
+
end
|
116
|
+
|
117
|
+
def add_headers(params = {})
|
118
|
+
@headers = params
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
class RichBodyContent < SimpleHttpContent
|
123
|
+
def get_request
|
124
|
+
_request = self.get_request_klass.new(@uri)
|
125
|
+
_request.body = @body if @body
|
126
|
+
_request.content_type = @content_type if @content_type
|
127
|
+
|
128
|
+
@headers.each_pair do |key, value|
|
129
|
+
_request.add_field(key, value)
|
130
|
+
end
|
131
|
+
|
132
|
+
_request
|
133
|
+
end
|
134
|
+
|
135
|
+
def add_parameters(params = {}, type = 'json')
|
136
|
+
if type == 'json'
|
137
|
+
@body = params.to_json
|
138
|
+
@content_type = 'application/json'
|
139
|
+
@type = type
|
140
|
+
else
|
141
|
+
raise NotImplementedError, "only support json, #{type} is not supported"
|
142
|
+
end
|
143
|
+
end
|
144
|
+
|
145
|
+
def add_url_parameters(params = {})
|
146
|
+
@uri.query = URI.encode_www_form(params)
|
147
|
+
end
|
148
|
+
end
|
149
|
+
|
150
|
+
class PostContent < RichBodyContent
|
151
|
+
def get_request_klass
|
152
|
+
Net::HTTP::Post
|
153
|
+
end
|
154
|
+
end
|
155
|
+
|
156
|
+
class PatchContent < RichBodyContent
|
157
|
+
def get_request_klass
|
158
|
+
Net::HTTP::Patch
|
159
|
+
end
|
160
|
+
end
|
161
|
+
|
162
|
+
class RichUrlContent < SimpleHttpContent
|
163
|
+
def get_request
|
164
|
+
_request = self.get_request_klass.new(@uri)
|
165
|
+
|
166
|
+
@headers.each_pair do |key, value|
|
167
|
+
_request.add_field(key, value)
|
168
|
+
end
|
169
|
+
|
170
|
+
_request
|
171
|
+
end
|
172
|
+
|
173
|
+
def add_parameters(params = {})
|
174
|
+
@uri.query = URI.encode_www_form(params)
|
175
|
+
end
|
176
|
+
end
|
177
|
+
|
178
|
+
class GetContent < RichUrlContent
|
179
|
+
def get_request_klass
|
180
|
+
Net::HTTP::Get
|
181
|
+
end
|
182
|
+
end
|
183
|
+
|
184
|
+
class DeleteContent < RichUrlContent
|
185
|
+
def get_request_klass
|
186
|
+
Net::HTTP::Delete
|
187
|
+
end
|
188
|
+
end
|
189
|
+
|
190
|
+
class LoginsightGetContent < GetContent
|
191
|
+
|
192
|
+
def add_loginsight_parameters(params = {})
|
193
|
+
uri_str = @uri.to_s
|
194
|
+
|
195
|
+
params.each_pair do |key, expression|
|
196
|
+
if expression.is_a? String
|
197
|
+
uri_str += "/#{key}/#{expression}"
|
198
|
+
else
|
199
|
+
expressions = expression
|
200
|
+
expressions.each do |expression|
|
201
|
+
uri_str += "/#{key}/#{expression}"
|
202
|
+
end
|
203
|
+
end
|
204
|
+
end
|
205
|
+
|
206
|
+
@uri = URI(URI.escape(uri_str))
|
207
|
+
end
|
208
|
+
end
|
209
|
+
|
210
|
+
|
211
|
+
class SimpleHttpResult
|
212
|
+
def initialize(response)
|
213
|
+
@response = response
|
214
|
+
end
|
215
|
+
|
216
|
+
def to_s
|
217
|
+
@response.to_s
|
218
|
+
end
|
219
|
+
|
220
|
+
def get_response
|
221
|
+
@response
|
222
|
+
end
|
223
|
+
|
224
|
+
def is_success?
|
225
|
+
@response.code.to_i == 200
|
226
|
+
end
|
227
|
+
|
228
|
+
def error_message
|
229
|
+
is_success? ? '' : @response.body
|
230
|
+
end
|
231
|
+
|
232
|
+
def get_content
|
233
|
+
is_success? ? JSON.parse(@response.body) : nil
|
234
|
+
end
|
235
|
+
end
|
236
|
+
end
|
metadata
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: simple-http-helper
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Yukai Jin
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-12-27 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: A simple http(s) request helper
|
14
|
+
email: fish1928@outlook.com
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- lib/helpers/simple_http_helper.rb
|
20
|
+
homepage: https://github.com/fish1928/SimpleHttpHelper
|
21
|
+
licenses:
|
22
|
+
- MIT
|
23
|
+
metadata: {}
|
24
|
+
post_install_message:
|
25
|
+
rdoc_options: []
|
26
|
+
require_paths:
|
27
|
+
- lib
|
28
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
29
|
+
requirements:
|
30
|
+
- - ">="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
34
|
+
requirements:
|
35
|
+
- - ">="
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
requirements: []
|
39
|
+
rubyforge_project:
|
40
|
+
rubygems_version: 2.6.12
|
41
|
+
signing_key:
|
42
|
+
specification_version: 4
|
43
|
+
summary: A simple http(s) request helper
|
44
|
+
test_files: []
|