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.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/json_requester.rb +18 -6
  3. metadata +4 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 748a32c5295898af685b4e3ed70df92ea11efc759a221fbec98cce193392c427
4
- data.tar.gz: 435e21117b3428590f3ef89239ae527bacfde144450129d57928f1aed6e27386
3
+ metadata.gz: 8fff067fc25751e8f21d18d96fbcbd0e534a9215ad1ceb0aeafeee0da9232605
4
+ data.tar.gz: b776183559aa1f441bf674e05c31a11fc326331284a4ee5b357b8c451e92b7e2
5
5
  SHA512:
6
- metadata.gz: 34d090e834fa922db9806bde4de4090bed712a4b75962228863618fafd9b1ea6bf9ecd0a6d41cb5aabf2232d16f1d00072413902f4d56b46cc0429d041cfd09a
7
- data.tar.gz: ee00fa54730c1ea8561db4967c100c7c1fcc6a6ec3d9c56abd16fdf20a2eae9bf2836edc9f540c020cf941d490ece55c38b8beaf7bde3bf9b8646c1e3f688971
6
+ metadata.gz: 325d8c2b5f0ce4f084811d7dbded18ceb54560cf6cfff188b830078e5a8111f9f38b5eb768f8d79bd14ae5411956f56359884b6e076e7cbcaca84f99a50975ca
7
+ data.tar.gz: dca6779f1259618b2ff050293090419981349a7029d6e4fdb00a82e1ef9b0fdc186618157566acd85e805fa1995046f307b2182d073f419929f3fb22ea03d34b
@@ -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 json_send(http_method, path, params={}, headers={})
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.headers['Content-Type'] = 'application/json;charset=utf-8'
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.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-09-10 00:00:00.000000000 Z
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.15.4
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.15.4
26
+ version: 1.0.0
27
27
  description: wrapper of faraday
28
28
  email: laura34963@kdanmobile.com
29
29
  executables: []