json_requester 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/json_requester.rb +18 -6
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8fff067fc25751e8f21d18d96fbcbd0e534a9215ad1ceb0aeafeee0da9232605
|
4
|
+
data.tar.gz: b776183559aa1f441bf674e05c31a11fc326331284a4ee5b357b8c451e92b7e2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 325d8c2b5f0ce4f084811d7dbded18ceb54560cf6cfff188b830078e5a8111f9f38b5eb768f8d79bd14ae5411956f56359884b6e076e7cbcaca84f99a50975ca
|
7
|
+
data.tar.gz: dca6779f1259618b2ff050293090419981349a7029d6e4fdb00a82e1ef9b0fdc186618157566acd85e805fa1995046f307b2182d073f419929f3fb22ea03d34b
|
data/lib/json_requester.rb
CHANGED
@@ -4,8 +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
|
+
@multipart = multipart
|
9
10
|
@conn = init_conn
|
10
11
|
end
|
11
12
|
|
@@ -28,6 +29,18 @@ class JsonRequester
|
|
28
29
|
error_response(e)
|
29
30
|
end
|
30
31
|
|
32
|
+
def json_send(http_method, path, params={}, headers={})
|
33
|
+
res = @conn.send(http_method) do |req|
|
34
|
+
req.url path
|
35
|
+
req.headers = headers if object_present?(headers)
|
36
|
+
req.headers['Content-Type'] = 'application/json;charset=utf-8'
|
37
|
+
req.body = params.to_json if object_present?(params)
|
38
|
+
end
|
39
|
+
process_response(res)
|
40
|
+
rescue => e
|
41
|
+
error_response(e)
|
42
|
+
end
|
43
|
+
|
31
44
|
def form_send(http_method, path, params={}, headers={})
|
32
45
|
res = @conn.send(http_method) do |req|
|
33
46
|
req.url path
|
@@ -40,22 +53,20 @@ class JsonRequester
|
|
40
53
|
error_response(e)
|
41
54
|
end
|
42
55
|
|
43
|
-
def
|
56
|
+
def multipart_form_send(http_method, path, params={}, headers={})
|
44
57
|
res = @conn.send(http_method) do |req|
|
45
58
|
req.url path
|
46
59
|
req.headers = headers if object_present?(headers)
|
47
|
-
req.
|
48
|
-
req.body = params.to_json if object_present?(params)
|
60
|
+
req.body = params if object_present?(params)
|
49
61
|
end
|
50
62
|
process_response(res)
|
51
|
-
rescue => e
|
52
|
-
error_response(e)
|
53
63
|
end
|
54
64
|
|
55
65
|
private
|
56
66
|
|
57
67
|
def init_conn
|
58
68
|
Faraday.new(url: host) do |faraday|
|
69
|
+
faraday.request :multipart if @multipart # multipart form POST request
|
59
70
|
faraday.request :url_encoded # form-encode POST params
|
60
71
|
faraday.response :logger # log requests to $stdout
|
61
72
|
faraday.adapter Faraday.default_adapter # make requests with Net::HTTP
|
@@ -81,4 +92,5 @@ class JsonRequester
|
|
81
92
|
def object_present?(object)
|
82
93
|
!(object.nil? || object.empty?)
|
83
94
|
end
|
95
|
+
|
84
96
|
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.0.
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- JiaRou Lee
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
11
|
+
date: 2020-12-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: faraday
|
@@ -16,14 +16,14 @@ dependencies:
|
|
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: []
|