json_requester 1.0.9 → 1.0.11
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 +19 -14
- 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: 9f5093495b6cc5e5ea2778c405bac7f712130a0d5a4e4b4dbd5d307606c20b98
|
4
|
+
data.tar.gz: a01404be960fc093d0637ab4890981d42a67161fea04f26151984bf661a6e48c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 95a2aedf37245b98f0167a7e099c5dbf650796dab009025b3075022cb5d8fe9c56ff5759b1075e7d272fcad51a43a68a33e999558d1f0e3932d099eb14bb9781
|
7
|
+
data.tar.gz: 6d32b1f828a49cb7f9fd98a6c1ea487feb74472e1cd64ec6256c4fccb4969a9389144c15d26957de2de838b162d41af0c1c4f01845a6dce4981b79eba737555a
|
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
@@ -4,23 +4,24 @@ require 'json'
|
|
4
4
|
class JsonRequester
|
5
5
|
attr_reader :host, :conn
|
6
6
|
|
7
|
-
def initialize(host, multipart: false, ssl_verify: true)
|
7
|
+
def initialize(host, multipart: false, ssl_verify: true, timeout: 60)
|
8
8
|
@host = host
|
9
9
|
@multipart = multipart
|
10
10
|
@ssl_verify = ssl_verify
|
11
|
+
@timeout = timeout
|
11
12
|
end
|
12
13
|
|
13
|
-
def http_send(http_method, path, params={}, headers={})
|
14
|
+
def http_send(http_method, path, params={}, headers={}, sort_params: true)
|
14
15
|
puts "send #{http_method} reqeust to #{@host} with\npath: #{path}\nparams: #{params}\nheaders: #{headers}"
|
15
16
|
if http_method == :get
|
16
|
-
normal_send(http_method, path, params, headers)
|
17
|
+
normal_send(http_method, path, params, headers, sort_params: sort_params)
|
17
18
|
else
|
18
|
-
json_send(http_method, path, params, headers)
|
19
|
+
json_send(http_method, path, params, headers, sort_params: sort_params)
|
19
20
|
end
|
20
21
|
end
|
21
22
|
|
22
|
-
def normal_send(http_method, path, params={}, headers={})
|
23
|
-
conn = init_conn
|
23
|
+
def normal_send(http_method, path, params={}, headers={}, sort_params: true)
|
24
|
+
conn = init_conn(sort_params: sort_params)
|
24
25
|
res = conn.send(http_method) do |req|
|
25
26
|
req.url path
|
26
27
|
req.headers = headers if object_present?(headers)
|
@@ -31,8 +32,8 @@ class JsonRequester
|
|
31
32
|
error_response(e)
|
32
33
|
end
|
33
34
|
|
34
|
-
def json_send(http_method, path, params={}, headers={})
|
35
|
-
conn = init_conn
|
35
|
+
def json_send(http_method, path, params={}, headers={}, sort_params: true)
|
36
|
+
conn = init_conn(sort_params: sort_params)
|
36
37
|
res = conn.send(http_method) do |req|
|
37
38
|
req.url path
|
38
39
|
req.headers = headers if object_present?(headers)
|
@@ -44,12 +45,12 @@ class JsonRequester
|
|
44
45
|
error_response(e)
|
45
46
|
end
|
46
47
|
|
47
|
-
def form_send(http_method, path, params={}, headers={})
|
48
|
-
conn = init_conn
|
48
|
+
def form_send(http_method, path, params={}, headers={}, sort_params: true)
|
49
|
+
conn = init_conn(sort_params: sort_params)
|
49
50
|
res = conn.send(http_method) do |req|
|
50
51
|
req.url path
|
51
52
|
req.headers = headers if object_present?(headers)
|
52
|
-
req.headers['Content-Type'] = 'application/x-www-form-urlencoded
|
53
|
+
req.headers['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8'
|
53
54
|
req.body = URI.encode_www_form(params) if object_present?(params)
|
54
55
|
end
|
55
56
|
process_response(res)
|
@@ -57,8 +58,8 @@ class JsonRequester
|
|
57
58
|
error_response(e)
|
58
59
|
end
|
59
60
|
|
60
|
-
def multipart_form_send(http_method, path, params={}, headers={})
|
61
|
-
conn = init_conn
|
61
|
+
def multipart_form_send(http_method, path, params={}, headers={}, sort_params: true)
|
62
|
+
conn = init_conn(sort_params: sort_params)
|
62
63
|
res = conn.send(http_method) do |req|
|
63
64
|
req.url path
|
64
65
|
req.headers = headers if object_present?(headers)
|
@@ -69,12 +70,16 @@ class JsonRequester
|
|
69
70
|
|
70
71
|
private
|
71
72
|
|
72
|
-
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
|
+
|
73
77
|
Faraday.new(url: host, ssl: { verify: @ssl_verify }) do |faraday|
|
74
78
|
faraday.request :multipart if @multipart # multipart form POST request
|
75
79
|
faraday.request :url_encoded # form-encode POST params
|
76
80
|
faraday.response :logger # log requests to $stdout
|
77
81
|
faraday.adapter Faraday.default_adapter # make requests with Net::HTTP
|
82
|
+
faraday.options.timeout = @timeout
|
78
83
|
end
|
79
84
|
end
|
80
85
|
|
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.11
|
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-08-17 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: []
|