json_requester 0.1.3 → 1.0.3

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 +25 -10
  3. metadata +18 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: da9e987ccbff806818b3b4e36acbb35f7e529d237acb29a279e932953fc75217
4
- data.tar.gz: ffd38fbd925c4017e5010813aaf3286c02d734bd665c167f199f5d1b80aad5f8
3
+ metadata.gz: a2d4ed52990f4aef4fdf9e2cd0648f005e7470e0b9589f32318c89446e9fbbab
4
+ data.tar.gz: 2aa352620aa571c8b19dcdf556bba9e89f6ffc5c3c515d772f7bd2b0788a4f9c
5
5
  SHA512:
6
- metadata.gz: af726a90e2f5a9e3fd1103de3bc9c830bbb59bbefbce692e0d0231f5be9f2628140a58f2c18bd0ed2191fe3dc0c586d6b0b4adad58ecb545a315feb82e03ced8
7
- data.tar.gz: 9f1a98e0f13aa4516ce84bbb85e76f7c54f50d91c764edbbe41acb69accc77142b459cbdb833b17896a43f39298a03c7564198f836bddc202c3420409aa5799f
6
+ metadata.gz: 1872fdb58e459b0086a37106bfc654cacd05807a0c81992be6ef8ab51ec58dd938d08698d03216b78918cdf1ecac3ce1fcdb96870670854f3d9895c45d9e7d0e
7
+ data.tar.gz: 95237fb35a77e6c4d3c7347ebfc1e0ef1435f67adab10a7966549187cda6e1af60562ba91b8fe974dfb2a6dff046af8511916c5dd124c64f547c55b6254b0653
@@ -4,9 +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
- @conn = init_conn
9
+ @multipart = multipart
10
10
  end
11
11
 
12
12
  def http_send(http_method, path, params={}, headers={})
@@ -18,7 +18,8 @@ class JsonRequester
18
18
  end
19
19
 
20
20
  def normal_send(http_method, path, params={}, headers={})
21
- res = @conn.send(http_method) do |req|
21
+ conn = init_conn
22
+ res = conn.send(http_method) do |req|
22
23
  req.url path
23
24
  req.headers = headers if object_present?(headers)
24
25
  req.params = params if object_present?(params)
@@ -28,8 +29,22 @@ class JsonRequester
28
29
  error_response(e)
29
30
  end
30
31
 
32
+ def json_send(http_method, path, params={}, headers={})
33
+ conn = init_conn
34
+ res = conn.send(http_method) do |req|
35
+ req.url path
36
+ req.headers = headers if object_present?(headers)
37
+ req.headers['Content-Type'] = 'application/json;charset=utf-8'
38
+ req.body = params.to_json if object_present?(params)
39
+ end
40
+ process_response(res)
41
+ rescue => e
42
+ error_response(e)
43
+ end
44
+
31
45
  def form_send(http_method, path, params={}, headers={})
32
- res = @conn.send(http_method) do |req|
46
+ conn = init_conn
47
+ res = conn.send(http_method) do |req|
33
48
  req.url path
34
49
  req.headers = headers if object_present?(headers)
35
50
  req.headers['Content-Type'] = 'application/x-www-form-urlencoded ;charset=utf-8'
@@ -40,22 +55,21 @@ class JsonRequester
40
55
  error_response(e)
41
56
  end
42
57
 
43
- def json_send(http_method, path, params={}, headers={})
44
- res = @conn.send(http_method) do |req|
58
+ def multipart_form_send(http_method, path, params={}, headers={})
59
+ conn = init_conn
60
+ res = conn.send(http_method) do |req|
45
61
  req.url path
46
62
  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)
63
+ req.body = params if object_present?(params)
49
64
  end
50
65
  process_response(res)
51
- rescue => e
52
- error_response(e)
53
66
  end
54
67
 
55
68
  private
56
69
 
57
70
  def init_conn
58
71
  Faraday.new(url: host) do |faraday|
72
+ faraday.request :multipart if @multipart # multipart form POST request
59
73
  faraday.request :url_encoded # form-encode POST params
60
74
  faraday.response :logger # log requests to $stdout
61
75
  faraday.adapter Faraday.default_adapter # make requests with Net::HTTP
@@ -81,4 +95,5 @@ class JsonRequester
81
95
  def object_present?(object)
82
96
  !(object.nil? || object.empty?)
83
97
  end
98
+
84
99
  end
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json_requester
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 1.0.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: 2019-11-20 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2021-06-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 1.0.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 1.0.0
13
27
  description: wrapper of faraday
14
28
  email: laura34963@kdanmobile.com
15
29
  executables: []
@@ -39,7 +53,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
39
53
  - !ruby/object:Gem::Version
40
54
  version: '0'
41
55
  requirements: []
42
- rubygems_version: 3.0.1
56
+ rubygems_version: 3.0.3
43
57
  signing_key:
44
58
  specification_version: 4
45
59
  summary: Basic Wrapper of Faraday