json_requester 1.0.0 → 1.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +1 -0
- data/lib/json_requester.rb +26 -11
- metadata +6 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8352bed1c5d1f5cf27c68c6cd9655f396607dcbc727717d27513a0ecfd8eeccf
|
4
|
+
data.tar.gz: a75ba2ef8b8ba5dfc2c107fa0f528347cf2f60a16ffd809b1e93453a46485557
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: '0787a94609c76f42118122188beb92a0a14956438f50940f8aa51b1d0dbb4db54f63d8e1c0585cb00d64fcf7e96585ee1723fd2b93bfc74e1062cdfb9d1d9efc'
|
7
|
+
data.tar.gz: ec62f38838dcbc4a3404948b1c29cbc6eb4940dc5ce33f8dc959e9ca45a8e63c479205e9feac910638c10922ce4526a2a2b4aa6ec752796be1a3da3d9d012e72
|
data/README.md
CHANGED
data/lib/json_requester.rb
CHANGED
@@ -4,9 +4,9 @@ require 'json'
|
|
4
4
|
class JsonRequester
|
5
5
|
attr_reader :host, :conn
|
6
6
|
|
7
|
-
def initialize(host)
|
7
|
+
def initialize(host, multipart: false)
|
8
8
|
@host = host
|
9
|
-
@
|
9
|
+
@multipart = multipart
|
10
10
|
end
|
11
11
|
|
12
12
|
def http_send(http_method, path, params={}, headers={})
|
@@ -18,7 +18,8 @@ class JsonRequester
|
|
18
18
|
end
|
19
19
|
|
20
20
|
def normal_send(http_method, path, params={}, headers={})
|
21
|
-
|
21
|
+
conn = init_conn
|
22
|
+
res = conn.send(http_method) do |req|
|
22
23
|
req.url path
|
23
24
|
req.headers = headers if object_present?(headers)
|
24
25
|
req.params = params if object_present?(params)
|
@@ -28,8 +29,22 @@ class JsonRequester
|
|
28
29
|
error_response(e)
|
29
30
|
end
|
30
31
|
|
32
|
+
def json_send(http_method, path, params={}, headers={})
|
33
|
+
conn = init_conn
|
34
|
+
res = conn.send(http_method) do |req|
|
35
|
+
req.url path
|
36
|
+
req.headers = headers if object_present?(headers)
|
37
|
+
req.headers['Content-Type'] = 'application/json;charset=utf-8'
|
38
|
+
req.body = params.to_json if object_present?(params)
|
39
|
+
end
|
40
|
+
process_response(res)
|
41
|
+
rescue => e
|
42
|
+
error_response(e)
|
43
|
+
end
|
44
|
+
|
31
45
|
def form_send(http_method, path, params={}, headers={})
|
32
|
-
|
46
|
+
conn = init_conn
|
47
|
+
res = conn.send(http_method) do |req|
|
33
48
|
req.url path
|
34
49
|
req.headers = headers if object_present?(headers)
|
35
50
|
req.headers['Content-Type'] = 'application/x-www-form-urlencoded ;charset=utf-8'
|
@@ -40,22 +55,21 @@ class JsonRequester
|
|
40
55
|
error_response(e)
|
41
56
|
end
|
42
57
|
|
43
|
-
def
|
44
|
-
|
58
|
+
def multipart_form_send(http_method, path, params={}, headers={})
|
59
|
+
conn = init_conn
|
60
|
+
res = conn.send(http_method) do |req|
|
45
61
|
req.url path
|
46
62
|
req.headers = headers if object_present?(headers)
|
47
|
-
req.
|
48
|
-
req.body = params.to_json if object_present?(params)
|
63
|
+
req.body = params if object_present?(params)
|
49
64
|
end
|
50
65
|
process_response(res)
|
51
|
-
rescue => e
|
52
|
-
error_response(e)
|
53
66
|
end
|
54
67
|
|
55
68
|
private
|
56
69
|
|
57
70
|
def init_conn
|
58
71
|
Faraday.new(url: host) do |faraday|
|
72
|
+
faraday.request :multipart if @multipart # multipart form POST request
|
59
73
|
faraday.request :url_encoded # form-encode POST params
|
60
74
|
faraday.response :logger # log requests to $stdout
|
61
75
|
faraday.adapter Faraday.default_adapter # make requests with Net::HTTP
|
@@ -67,7 +81,7 @@ class JsonRequester
|
|
67
81
|
begin
|
68
82
|
body = JSON.parse(response.body)
|
69
83
|
body = body.is_a?(Hash) ? body : {'body' => body}
|
70
|
-
body['body_status'] = body.delete('status') unless body['status'].
|
84
|
+
body['body_status'] = body.delete('status') unless body['status'].nil?
|
71
85
|
rescue
|
72
86
|
body = {'body' => response.body}
|
73
87
|
end
|
@@ -81,4 +95,5 @@ class JsonRequester
|
|
81
95
|
def object_present?(object)
|
82
96
|
!(object.nil? || object.empty?)
|
83
97
|
end
|
98
|
+
|
84
99
|
end
|
metadata
CHANGED
@@ -1,29 +1,29 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: json_requester
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- JiaRou Lee
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-12-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: faraday
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - "
|
17
|
+
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: 0.
|
19
|
+
version: 1.0.0
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- - "
|
24
|
+
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: 0.
|
26
|
+
version: 1.0.0
|
27
27
|
description: wrapper of faraday
|
28
28
|
email: laura34963@kdanmobile.com
|
29
29
|
executables: []
|