json_requester 1.1.1 → 1.1.3
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 +14 -10
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b4f982cccc8a4c27d2013a2c007b907c0cbf3f6aa05eff09464bba82cc6cd59f
|
4
|
+
data.tar.gz: f36d85249ef2b341aeff0435da4c3398d10e8c7146f2233c7c35f11a61b588c3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 02bcd235be1471113c0b0c1640bad36f308a839e7d3143a072877cd124bec70af77079093934e97ace2a85db5bcdd6d7ab2ec2029dd05dee774f9dbabcc741e2
|
7
|
+
data.tar.gz: 86c13263c31f9174cb3a0ba66f0283958bc783f80c635649d28ee60b0072c2cc3b92ec931010a91a6507e1a3ecc44e6cde5217659db152ba5bbd33a11c86afbe
|
data/README.md
CHANGED
@@ -30,7 +30,8 @@ $ gem install json_requester
|
|
30
30
|
# Request by using JSON body or query params, use the `http_send` method.
|
31
31
|
# other methods: `form_send`, `multipart_form_send`
|
32
32
|
# `sort_params` at Faraday gem default is true.
|
33
|
-
|
33
|
+
# `content_type_charset` default is 'utf-8', this will add ; charset=utf-8 after `Content-Type` header (ex. `Content-Type=application/json; charset=utf-8`).
|
34
|
+
res = requester.http_send(http_method, path, params, headers, sort_params: true, content_type_charset: 'utf-8')
|
34
35
|
|
35
36
|
# http response code
|
36
37
|
puts res['status'] # 200, 404, .. etc
|
data/lib/json_requester.rb
CHANGED
@@ -12,12 +12,12 @@ class JsonRequester
|
|
12
12
|
@user_agent = user_agent.strip.to_s
|
13
13
|
end
|
14
14
|
|
15
|
-
def http_send(http_method, path, params={}, headers={}, sort_params: true, need_response_header: false)
|
16
|
-
puts "send #{http_method}
|
15
|
+
def http_send(http_method, path, params={}, headers={}, sort_params: true, need_response_header: false, content_type_charset: 'utf-8')
|
16
|
+
puts "send #{http_method} request to #{@host} with\npath: #{path}\nparams: #{params}\nheaders: #{headers}"
|
17
17
|
if http_method == :get
|
18
18
|
normal_send(http_method, path, params, headers, sort_params: sort_params, need_response_header: need_response_header)
|
19
19
|
else
|
20
|
-
json_send(http_method, path, params, headers, sort_params: sort_params, need_response_header: need_response_header)
|
20
|
+
json_send(http_method, path, params, headers, sort_params: sort_params, need_response_header: need_response_header, content_type_charset: content_type_charset)
|
21
21
|
end
|
22
22
|
end
|
23
23
|
|
@@ -38,11 +38,7 @@ class JsonRequester
|
|
38
38
|
res = conn.send(http_method) do |req|
|
39
39
|
req.url path
|
40
40
|
req.headers = headers if object_present?(headers)
|
41
|
-
|
42
|
-
req.headers['Content-Type'] = "application/json;charset=#{content_type_charset}"
|
43
|
-
else
|
44
|
-
req.headers['Content-Type'] = 'application/json'
|
45
|
-
end
|
41
|
+
req.headers['Content-Type'] = object_present?(content_type_charset) ? "application/json;charset=#{content_type_charset}" : 'application/json'
|
46
42
|
req.body = params.to_json if object_present?(params)
|
47
43
|
end
|
48
44
|
process_response(res, need_response_header: need_response_header)
|
@@ -104,11 +100,19 @@ class JsonRequester
|
|
104
100
|
end
|
105
101
|
|
106
102
|
def error_response(err)
|
107
|
-
{'status' => 500, 'message' => "#{err.class.name}: #{err.message}"}
|
103
|
+
{ 'status' => 500, 'message' => "#{err.class.name}: #{err.message}" }
|
108
104
|
end
|
109
105
|
|
110
106
|
def object_present?(object)
|
111
|
-
|
107
|
+
# Ref: https://github.com/rails/rails/blob/v7.1.4.2/activesupport/lib/active_support/core_ext/object/blank.rb#L25
|
108
|
+
# active_support present? method
|
109
|
+
!object_blank?(object)
|
110
|
+
end
|
111
|
+
|
112
|
+
def object_blank?(object)
|
113
|
+
# Ref: https://github.com/rails/rails/blob/v7.1.4.2/activesupport/lib/active_support/core_ext/object/blank.rb#L18
|
114
|
+
# active_support blank? method
|
115
|
+
object.respond_to?(:empty?) ? !!object.empty? : false
|
112
116
|
end
|
113
117
|
|
114
118
|
end
|
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.1.
|
4
|
+
version: 1.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- JiaRou Lee
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-
|
11
|
+
date: 2024-10-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: faraday
|
@@ -75,7 +75,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
75
75
|
- !ruby/object:Gem::Version
|
76
76
|
version: '0'
|
77
77
|
requirements: []
|
78
|
-
rubygems_version: 3.
|
78
|
+
rubygems_version: 3.5.16
|
79
79
|
signing_key:
|
80
80
|
specification_version: 4
|
81
81
|
summary: Basic Wrapper of Faraday
|