json_requester 1.0.10 → 1.0.12
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +28 -8
- data/lib/json_requester.rb +24 -19
- metadata +22 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: '0845954f6576fcb489054c9dba9649432baf657c49e83e77bfcc0e5c70cc0e89'
|
4
|
+
data.tar.gz: cbe77bc53e67caedf438644d5f5302fa5bbf78761f79a96762b3b401c126b035
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: aa8352783d500877db5023c7d9f77d0cd5488e9af3418c25fba9e7cbb863e4e247bb40e534919e42ea15137636de0ced368c54d2148883e2c7139fe1e6386948
|
7
|
+
data.tar.gz: e0da8dcf1a2faaefedee3724d46fa56945db6d0495674a776870b813719c81299b8f5f10dad3aef0f2083995a609ba9e90d1994cf58477bf23d00e1ff1ce1772
|
data/README.md
CHANGED
@@ -4,15 +4,35 @@
|
|
4
4
|
|
5
5
|
### Install
|
6
6
|
|
7
|
-
|
7
|
+
```bash
|
8
|
+
$ gem install json_requester
|
9
|
+
```
|
8
10
|
|
9
11
|
### How to Use
|
10
12
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
```ruby
|
14
|
+
host = 'http://httpbingo.org'
|
15
|
+
# `timeout` at Faraday gem default is 60 secs.
|
16
|
+
requester = JsonRequester.new(host, timeout: 120)
|
17
|
+
|
18
|
+
http_method = :get # :get / :post / :put / :delete
|
19
|
+
path = ''
|
20
|
+
headers = { 'Authorization' => 'Bearer token' }
|
16
21
|
|
17
|
-
|
18
|
-
|
22
|
+
# The `:get` method will use query params;
|
23
|
+
# Other HTTP methods will use JSON body to request.
|
24
|
+
params = {
|
25
|
+
key_1: 'value_1',
|
26
|
+
key_2: 'value_2'
|
27
|
+
}
|
28
|
+
|
29
|
+
# Request by using JSON body or query params, use the `http_send` method.
|
30
|
+
# other methods: `form_send`, `multipart_form_send`
|
31
|
+
# `sort_params` at Faraday gem default is true.
|
32
|
+
res = requester.http_send(http_method, path, params, headers, sort_params: true)
|
33
|
+
|
34
|
+
# http response code
|
35
|
+
puts res['status'] # 200, 404, .. etc
|
36
|
+
# the response JSON body
|
37
|
+
puts res['body'] # { foo: 'bar' }
|
38
|
+
```
|
data/lib/json_requester.rb
CHANGED
@@ -11,66 +11,69 @@ class JsonRequester
|
|
11
11
|
@timeout = timeout
|
12
12
|
end
|
13
13
|
|
14
|
-
def http_send(http_method, path, params={}, headers={})
|
14
|
+
def http_send(http_method, path, params={}, headers={}, sort_params: true, need_response_header: false)
|
15
15
|
puts "send #{http_method} reqeust to #{@host} with\npath: #{path}\nparams: #{params}\nheaders: #{headers}"
|
16
16
|
if http_method == :get
|
17
|
-
normal_send(http_method, path, params, headers)
|
17
|
+
normal_send(http_method, path, params, headers, sort_params: sort_params, need_response_header: need_response_header)
|
18
18
|
else
|
19
|
-
json_send(http_method, path, params, headers)
|
19
|
+
json_send(http_method, path, params, headers, sort_params: sort_params, need_response_header: need_response_header)
|
20
20
|
end
|
21
21
|
end
|
22
22
|
|
23
|
-
def normal_send(http_method, path, params={}, headers={})
|
24
|
-
conn = init_conn
|
23
|
+
def normal_send(http_method, path, params={}, headers={}, sort_params: true, need_response_header: false)
|
24
|
+
conn = init_conn(sort_params: sort_params)
|
25
25
|
res = conn.send(http_method) do |req|
|
26
26
|
req.url path
|
27
27
|
req.headers = headers if object_present?(headers)
|
28
28
|
req.params = params if object_present?(params)
|
29
29
|
end
|
30
|
-
process_response(res)
|
30
|
+
process_response(res, need_response_header: need_response_header)
|
31
31
|
rescue => e
|
32
32
|
error_response(e)
|
33
33
|
end
|
34
34
|
|
35
|
-
def json_send(http_method, path, params={}, headers={})
|
36
|
-
conn = init_conn
|
35
|
+
def json_send(http_method, path, params={}, headers={}, sort_params: true, need_response_header: false)
|
36
|
+
conn = init_conn(sort_params: sort_params)
|
37
37
|
res = conn.send(http_method) do |req|
|
38
38
|
req.url path
|
39
39
|
req.headers = headers if object_present?(headers)
|
40
40
|
req.headers['Content-Type'] = 'application/json;charset=utf-8'
|
41
41
|
req.body = params.to_json if object_present?(params)
|
42
42
|
end
|
43
|
-
process_response(res)
|
43
|
+
process_response(res, need_response_header: need_response_header)
|
44
44
|
rescue => e
|
45
45
|
error_response(e)
|
46
46
|
end
|
47
47
|
|
48
|
-
def form_send(http_method, path, params={}, headers={})
|
49
|
-
conn = init_conn
|
48
|
+
def form_send(http_method, path, params={}, headers={}, sort_params: true, need_response_header: false)
|
49
|
+
conn = init_conn(sort_params: sort_params)
|
50
50
|
res = conn.send(http_method) do |req|
|
51
51
|
req.url path
|
52
52
|
req.headers = headers if object_present?(headers)
|
53
|
-
req.headers['Content-Type'] = 'application/x-www-form-urlencoded
|
53
|
+
req.headers['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8'
|
54
54
|
req.body = URI.encode_www_form(params) if object_present?(params)
|
55
55
|
end
|
56
|
-
process_response(res)
|
56
|
+
process_response(res, need_response_header: need_response_header)
|
57
57
|
rescue => e
|
58
58
|
error_response(e)
|
59
59
|
end
|
60
60
|
|
61
|
-
def multipart_form_send(http_method, path, params={}, headers={})
|
62
|
-
conn = init_conn
|
61
|
+
def multipart_form_send(http_method, path, params={}, headers={}, sort_params: true, need_response_header: false)
|
62
|
+
conn = init_conn(sort_params: sort_params)
|
63
63
|
res = conn.send(http_method) do |req|
|
64
64
|
req.url path
|
65
65
|
req.headers = headers if object_present?(headers)
|
66
66
|
req.body = params if object_present?(params)
|
67
67
|
end
|
68
|
-
process_response(res)
|
68
|
+
process_response(res, need_response_header: need_response_header)
|
69
69
|
end
|
70
70
|
|
71
71
|
private
|
72
72
|
|
73
|
-
def init_conn
|
73
|
+
def init_conn(sort_params: true)
|
74
|
+
# https://lostisland.github.io/faraday/#/customization/index?id=order-of-parameters
|
75
|
+
Faraday::NestedParamsEncoder.sort_params = sort_params # faraday default is true
|
76
|
+
|
74
77
|
Faraday.new(url: host, ssl: { verify: @ssl_verify }) do |faraday|
|
75
78
|
faraday.request :multipart if @multipart # multipart form POST request
|
76
79
|
faraday.request :url_encoded # form-encode POST params
|
@@ -80,7 +83,7 @@ class JsonRequester
|
|
80
83
|
end
|
81
84
|
end
|
82
85
|
|
83
|
-
def process_response(response)
|
86
|
+
def process_response(response, need_response_header: false)
|
84
87
|
result = {'status' => response.status}
|
85
88
|
begin
|
86
89
|
body = JSON.parse(response.body)
|
@@ -89,7 +92,9 @@ class JsonRequester
|
|
89
92
|
rescue
|
90
93
|
body = {'body' => response.body}
|
91
94
|
end
|
92
|
-
result.merge(body)
|
95
|
+
result.merge!(body)
|
96
|
+
result['headers'] = response.headers.to_h if need_response_header
|
97
|
+
result
|
93
98
|
end
|
94
99
|
|
95
100
|
def error_response(err)
|
metadata
CHANGED
@@ -1,19 +1,22 @@
|
|
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.12
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- JiaRou Lee
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-
|
11
|
+
date: 2023-10-26 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
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.0'
|
17
20
|
- - ">="
|
18
21
|
- !ruby/object:Gem::Version
|
19
22
|
version: 1.0.0
|
@@ -21,9 +24,26 @@ dependencies:
|
|
21
24
|
prerelease: false
|
22
25
|
version_requirements: !ruby/object:Gem::Requirement
|
23
26
|
requirements:
|
27
|
+
- - "~>"
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '1.0'
|
24
30
|
- - ">="
|
25
31
|
- !ruby/object:Gem::Version
|
26
32
|
version: 1.0.0
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: pry
|
35
|
+
requirement: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - "~>"
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: 0.14.1
|
40
|
+
type: :development
|
41
|
+
prerelease: false
|
42
|
+
version_requirements: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - "~>"
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: 0.14.1
|
27
47
|
description: wrapper of faraday
|
28
48
|
email: laura34963@kdanmobile.com
|
29
49
|
executables: []
|