json_requester 2.0.6 → 2.0.7
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 +4 -4
- data/lib/json_requester.rb +14 -14
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 6b5c16b22f8ebb2f6dfb24fcc7c90a4989c69028d83d763b7220796abbc635b1
|
|
4
|
+
data.tar.gz: 5d4eb12d1807eb72ca2284b6207c0320c702738ba74553ed8c0bd049a6ffa6f7
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: d2b407242fc322ff2a23401b02bde6e5046da77d40296ffa2c72f5d8a61c6055fb7f8f680f7be3057b7b819bdae14a7297dd672000ce333b72fcc7a3143a1009
|
|
7
|
+
data.tar.gz: 517b6d56cc8dfaa17b569b8288d6a94c254e7ed0a0f636fdcd7af2dbcc97f3ac681102070ac82d0f6d979b462f157292ed3d8b2c50cb62222d05a43ff58728ec
|
data/lib/json_requester.rb
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require
|
|
4
|
-
require
|
|
5
|
-
require
|
|
3
|
+
require "faraday"
|
|
4
|
+
require "faraday/multipart"
|
|
5
|
+
require "json"
|
|
6
6
|
|
|
7
7
|
class JsonRequester
|
|
8
8
|
attr_reader :host, :conn
|
|
9
9
|
|
|
10
|
-
def initialize(host, multipart: false, ssl_verify: true, timeout: 60, user_agent:
|
|
10
|
+
def initialize(host, multipart: false, ssl_verify: true, timeout: 60, user_agent: "")
|
|
11
11
|
@host = host
|
|
12
12
|
@multipart = multipart
|
|
13
13
|
@ssl_verify = ssl_verify
|
|
@@ -15,7 +15,7 @@ class JsonRequester
|
|
|
15
15
|
@user_agent = user_agent.strip.to_s
|
|
16
16
|
end
|
|
17
17
|
|
|
18
|
-
def http_send(http_method, path, params = {}, headers = {}, sort_params: true, need_response_header: false, content_type_charset:
|
|
18
|
+
def http_send(http_method, path, params = {}, headers = {}, sort_params: true, need_response_header: false, content_type_charset: "utf-8")
|
|
19
19
|
puts "send #{http_method} request to #{@host} with\npath: #{path}\nparams: #{params}\nheaders: #{headers}"
|
|
20
20
|
if http_method == :get
|
|
21
21
|
normal_send(http_method, path, params, headers, sort_params: sort_params, need_response_header: need_response_header)
|
|
@@ -36,12 +36,12 @@ class JsonRequester
|
|
|
36
36
|
error_response(e)
|
|
37
37
|
end
|
|
38
38
|
|
|
39
|
-
def json_send(http_method, path, params = {}, headers = {}, sort_params: true, need_response_header: false, content_type_charset:
|
|
39
|
+
def json_send(http_method, path, params = {}, headers = {}, sort_params: true, need_response_header: false, content_type_charset: "utf-8")
|
|
40
40
|
conn = init_conn(sort_params: sort_params)
|
|
41
41
|
res = conn.send(http_method) do |req|
|
|
42
42
|
req.url path
|
|
43
43
|
req.headers = headers if object_present?(headers)
|
|
44
|
-
req.headers[
|
|
44
|
+
req.headers["Content-Type"] = object_present?(content_type_charset) ? "application/json;charset=#{content_type_charset}" : "application/json"
|
|
45
45
|
req.body = params.to_json if object_present?(params)
|
|
46
46
|
end
|
|
47
47
|
process_response(res, need_response_header: need_response_header)
|
|
@@ -54,7 +54,7 @@ class JsonRequester
|
|
|
54
54
|
res = conn.send(http_method) do |req|
|
|
55
55
|
req.url path
|
|
56
56
|
req.headers = headers if object_present?(headers)
|
|
57
|
-
req.headers[
|
|
57
|
+
req.headers["Content-Type"] = "application/x-www-form-urlencoded;charset=utf-8"
|
|
58
58
|
req.body = URI.encode_www_form(params) if object_present?(params)
|
|
59
59
|
end
|
|
60
60
|
process_response(res, need_response_header: need_response_header)
|
|
@@ -93,21 +93,21 @@ class JsonRequester
|
|
|
93
93
|
end
|
|
94
94
|
|
|
95
95
|
def process_response(response, need_response_header: false)
|
|
96
|
-
result = {
|
|
96
|
+
result = { "status" => response.status }
|
|
97
97
|
begin
|
|
98
98
|
body = JSON.parse(response.body)
|
|
99
|
-
body = {
|
|
100
|
-
body[
|
|
99
|
+
body = { "body" => body } unless body.is_a?(Hash)
|
|
100
|
+
body["body_status"] = body.delete("status") unless body["status"].nil?
|
|
101
101
|
rescue StandardError
|
|
102
|
-
body = {
|
|
102
|
+
body = { "body" => response.body }
|
|
103
103
|
end
|
|
104
104
|
result.merge!(body)
|
|
105
|
-
result[
|
|
105
|
+
result["headers"] = response.headers.to_h if need_response_header
|
|
106
106
|
result
|
|
107
107
|
end
|
|
108
108
|
|
|
109
109
|
def error_response(err)
|
|
110
|
-
{
|
|
110
|
+
{ "status" => 500, "message" => "#{err.class.name}: #{err.message}" }
|
|
111
111
|
end
|
|
112
112
|
|
|
113
113
|
def object_present?(object)
|