json_requester 1.0.11 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +2 -1
- data/lib/json_requester.rb +18 -14
- metadata +12 -12
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cc7d06fc0826a6e7c0de5aa79a3f0f3bfa8825939594a46bd0f94d00fff2b4e2
|
4
|
+
data.tar.gz: 251126b9edcdd2c645f7510f388d6ab5f80b4954695c309c99bb402758a6ec10
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d2eddf1750ef801739e6dae0108a085954b3e0d41276f171a365ba5df5bb99f84ce666467fdcb2c5824c9a6c6be9a35976e219a3e2c95cd848e00e5bae5cd92b
|
7
|
+
data.tar.gz: 7d2d352b07533fd5906ddf8ea08687d8e320cd1dad193f46abe576bc0b4b304981956e8f87ef84fe32794d958554531dd2cbfffbbd9b676e4cebf84b7f121384
|
data/README.md
CHANGED
@@ -13,7 +13,8 @@ $ gem install json_requester
|
|
13
13
|
```ruby
|
14
14
|
host = 'http://httpbingo.org'
|
15
15
|
# `timeout` at Faraday gem default is 60 secs.
|
16
|
-
|
16
|
+
# `user_agent` at Faraday gem default is like "Faraday v1.10.0", it would be deep_merge at Faraday default setting.
|
17
|
+
requester = JsonRequester.new(host, timeout: 120, user_agent: 'My Agent 1.2')
|
17
18
|
|
18
19
|
http_method = :get # :get / :post / :put / :delete
|
19
20
|
path = ''
|
data/lib/json_requester.rb
CHANGED
@@ -4,35 +4,36 @@ require 'json'
|
|
4
4
|
class JsonRequester
|
5
5
|
attr_reader :host, :conn
|
6
6
|
|
7
|
-
def initialize(host, multipart: false, ssl_verify: true, timeout: 60)
|
7
|
+
def initialize(host, multipart: false, ssl_verify: true, timeout: 60, user_agent: '')
|
8
8
|
@host = host
|
9
9
|
@multipart = multipart
|
10
10
|
@ssl_verify = ssl_verify
|
11
11
|
@timeout = timeout
|
12
|
+
@user_agent = user_agent.strip.to_s
|
12
13
|
end
|
13
14
|
|
14
|
-
def http_send(http_method, path, params={}, headers={}, sort_params: true)
|
15
|
+
def http_send(http_method, path, params={}, headers={}, sort_params: true, need_response_header: false)
|
15
16
|
puts "send #{http_method} reqeust to #{@host} with\npath: #{path}\nparams: #{params}\nheaders: #{headers}"
|
16
17
|
if http_method == :get
|
17
|
-
normal_send(http_method, path, params, headers, sort_params: sort_params)
|
18
|
+
normal_send(http_method, path, params, headers, sort_params: sort_params, need_response_header: need_response_header)
|
18
19
|
else
|
19
|
-
json_send(http_method, path, params, headers, sort_params: sort_params)
|
20
|
+
json_send(http_method, path, params, headers, sort_params: sort_params, need_response_header: need_response_header)
|
20
21
|
end
|
21
22
|
end
|
22
23
|
|
23
|
-
def normal_send(http_method, path, params={}, headers={}, sort_params: true)
|
24
|
+
def normal_send(http_method, path, params={}, headers={}, sort_params: true, need_response_header: false)
|
24
25
|
conn = init_conn(sort_params: sort_params)
|
25
26
|
res = conn.send(http_method) do |req|
|
26
27
|
req.url path
|
27
28
|
req.headers = headers if object_present?(headers)
|
28
29
|
req.params = params if object_present?(params)
|
29
30
|
end
|
30
|
-
process_response(res)
|
31
|
+
process_response(res, need_response_header: need_response_header)
|
31
32
|
rescue => e
|
32
33
|
error_response(e)
|
33
34
|
end
|
34
35
|
|
35
|
-
def json_send(http_method, path, params={}, headers={}, sort_params: true)
|
36
|
+
def json_send(http_method, path, params={}, headers={}, sort_params: true, need_response_header: false)
|
36
37
|
conn = init_conn(sort_params: sort_params)
|
37
38
|
res = conn.send(http_method) do |req|
|
38
39
|
req.url path
|
@@ -40,12 +41,12 @@ class JsonRequester
|
|
40
41
|
req.headers['Content-Type'] = 'application/json;charset=utf-8'
|
41
42
|
req.body = params.to_json if object_present?(params)
|
42
43
|
end
|
43
|
-
process_response(res)
|
44
|
+
process_response(res, need_response_header: need_response_header)
|
44
45
|
rescue => e
|
45
46
|
error_response(e)
|
46
47
|
end
|
47
48
|
|
48
|
-
def form_send(http_method, path, params={}, headers={}, sort_params: true)
|
49
|
+
def form_send(http_method, path, params={}, headers={}, sort_params: true, need_response_header: false)
|
49
50
|
conn = init_conn(sort_params: sort_params)
|
50
51
|
res = conn.send(http_method) do |req|
|
51
52
|
req.url path
|
@@ -53,19 +54,19 @@ class JsonRequester
|
|
53
54
|
req.headers['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8'
|
54
55
|
req.body = URI.encode_www_form(params) if object_present?(params)
|
55
56
|
end
|
56
|
-
process_response(res)
|
57
|
+
process_response(res, need_response_header: need_response_header)
|
57
58
|
rescue => e
|
58
59
|
error_response(e)
|
59
60
|
end
|
60
61
|
|
61
|
-
def multipart_form_send(http_method, path, params={}, headers={}, sort_params: true)
|
62
|
+
def multipart_form_send(http_method, path, params={}, headers={}, sort_params: true, need_response_header: false)
|
62
63
|
conn = init_conn(sort_params: sort_params)
|
63
64
|
res = conn.send(http_method) do |req|
|
64
65
|
req.url path
|
65
66
|
req.headers = headers if object_present?(headers)
|
66
67
|
req.body = params if object_present?(params)
|
67
68
|
end
|
68
|
-
process_response(res)
|
69
|
+
process_response(res, need_response_header: need_response_header)
|
69
70
|
end
|
70
71
|
|
71
72
|
private
|
@@ -73,6 +74,7 @@ class JsonRequester
|
|
73
74
|
def init_conn(sort_params: true)
|
74
75
|
# https://lostisland.github.io/faraday/#/customization/index?id=order-of-parameters
|
75
76
|
Faraday::NestedParamsEncoder.sort_params = sort_params # faraday default is true
|
77
|
+
Faraday.default_connection_options = { headers: { user_agent: @user_agent } } unless @user_agent.empty?
|
76
78
|
|
77
79
|
Faraday.new(url: host, ssl: { verify: @ssl_verify }) do |faraday|
|
78
80
|
faraday.request :multipart if @multipart # multipart form POST request
|
@@ -83,7 +85,7 @@ class JsonRequester
|
|
83
85
|
end
|
84
86
|
end
|
85
87
|
|
86
|
-
def process_response(response)
|
88
|
+
def process_response(response, need_response_header: false)
|
87
89
|
result = {'status' => response.status}
|
88
90
|
begin
|
89
91
|
body = JSON.parse(response.body)
|
@@ -92,7 +94,9 @@ class JsonRequester
|
|
92
94
|
rescue
|
93
95
|
body = {'body' => response.body}
|
94
96
|
end
|
95
|
-
result.merge(body)
|
97
|
+
result.merge!(body)
|
98
|
+
result['headers'] = response.headers.to_h if need_response_header
|
99
|
+
result
|
96
100
|
end
|
97
101
|
|
98
102
|
def error_response(err)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
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.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- JiaRou Lee
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-
|
11
|
+
date: 2023-12-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: faraday
|
@@ -16,34 +16,34 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '1.
|
19
|
+
version: '1.10'
|
20
20
|
- - ">="
|
21
21
|
- !ruby/object:Gem::Version
|
22
|
-
version: 1.
|
22
|
+
version: 1.10.3
|
23
23
|
type: :runtime
|
24
24
|
prerelease: false
|
25
25
|
version_requirements: !ruby/object:Gem::Requirement
|
26
26
|
requirements:
|
27
27
|
- - "~>"
|
28
28
|
- !ruby/object:Gem::Version
|
29
|
-
version: '1.
|
29
|
+
version: '1.10'
|
30
30
|
- - ">="
|
31
31
|
- !ruby/object:Gem::Version
|
32
|
-
version: 1.
|
32
|
+
version: 1.10.3
|
33
33
|
- !ruby/object:Gem::Dependency
|
34
34
|
name: pry
|
35
35
|
requirement: !ruby/object:Gem::Requirement
|
36
36
|
requirements:
|
37
37
|
- - "~>"
|
38
38
|
- !ruby/object:Gem::Version
|
39
|
-
version: 0.14.
|
39
|
+
version: 0.14.2
|
40
40
|
type: :development
|
41
41
|
prerelease: false
|
42
42
|
version_requirements: !ruby/object:Gem::Requirement
|
43
43
|
requirements:
|
44
44
|
- - "~>"
|
45
45
|
- !ruby/object:Gem::Version
|
46
|
-
version: 0.14.
|
46
|
+
version: 0.14.2
|
47
47
|
description: wrapper of faraday
|
48
48
|
email: laura34963@kdanmobile.com
|
49
49
|
executables: []
|
@@ -59,7 +59,7 @@ licenses:
|
|
59
59
|
metadata:
|
60
60
|
source_code_uri: https://github.com/kdan-mobile-software-ltd/json_requester
|
61
61
|
changelog_uri: https://github.com/kdan-mobile-software-ltd/json_requester/blob/master/CHANGELOG.md
|
62
|
-
post_install_message:
|
62
|
+
post_install_message:
|
63
63
|
rdoc_options:
|
64
64
|
- "--charset=UTF-8"
|
65
65
|
require_paths:
|
@@ -75,8 +75,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
75
75
|
- !ruby/object:Gem::Version
|
76
76
|
version: '0'
|
77
77
|
requirements: []
|
78
|
-
rubygems_version: 3.
|
79
|
-
signing_key:
|
78
|
+
rubygems_version: 3.4.10
|
79
|
+
signing_key:
|
80
80
|
specification_version: 4
|
81
81
|
summary: Basic Wrapper of Faraday
|
82
82
|
test_files: []
|