json_requester 0.1.1 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +10 -4
  3. data/lib/json_requester.rb +27 -3
  4. metadata +18 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4731f398adfa4196fb8e5d35d44138fb825969028616f64660c7fc6dbe5939a4
4
- data.tar.gz: c1b0377db8f87d79d69ecb8ad7ed813c24e27fce1590543d3c4b63bb02c07edb
3
+ metadata.gz: 8fff067fc25751e8f21d18d96fbcbd0e534a9215ad1ceb0aeafeee0da9232605
4
+ data.tar.gz: b776183559aa1f441bf674e05c31a11fc326331284a4ee5b357b8c451e92b7e2
5
5
  SHA512:
6
- metadata.gz: dc786f2468558ccc80aba19db09645720dc2d63903f3c235447ae7ae523b38c2181ecf8081605bef9eeb88b8a48c3d31cd1a3dd23f4acfcf1c5c43d3bcaa555d
7
- data.tar.gz: 62aebb2d176eb6fea520b983a7876ff5791b8da1218e7e64fed4c8700befbd314f28362328498209d6aba9d1a6fae83aa0342a102a390e37d31d3082e0143dc9
6
+ metadata.gz: 325d8c2b5f0ce4f084811d7dbded18ceb54560cf6cfff188b830078e5a8111f9f38b5eb768f8d79bd14ae5411956f56359884b6e076e7cbcaca84f99a50975ca
7
+ data.tar.gz: dca6779f1259618b2ff050293090419981349a7029d6e4fdb00a82e1ef9b0fdc186618157566acd85e805fa1995046f307b2182d073f419929f3fb22ea03d34b
data/README.md CHANGED
@@ -2,10 +2,16 @@
2
2
 
3
3
  ### A Wrapper of Faraday Gem
4
4
 
5
+ ### Install
6
+
7
+ gem install json_requester
8
+
5
9
  ### How to Use
6
10
 
7
- * init requester
8
- `requester = JsonRequester.new([host])`
11
+ requester = JsonRequester.new(host)
12
+ res = requester.http_send(http_method, path, params, headers)
13
+ puts res['status'] # 200
14
+ puts res['body'] # { foo: 'bar' }
9
15
 
10
- * http request
11
- `requester.http_send([http_method], [path], [params], [headers])`
16
+ puts res['status'] # 500
17
+ puts res['message'] # invalid method
@@ -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
 
@@ -20,8 +21,8 @@ class JsonRequester
20
21
  def normal_send(http_method, path, params={}, headers={})
21
22
  res = @conn.send(http_method) do |req|
22
23
  req.url path
23
- req.params = params if object_present?(params)
24
24
  req.headers = headers if object_present?(headers)
25
+ req.params = params if object_present?(params)
25
26
  end
26
27
  process_response(res)
27
28
  rescue => e
@@ -32,7 +33,7 @@ class JsonRequester
32
33
  res = @conn.send(http_method) do |req|
33
34
  req.url path
34
35
  req.headers = headers if object_present?(headers)
35
- req.headers['Content-Type'] = 'application/json'
36
+ req.headers['Content-Type'] = 'application/json;charset=utf-8'
36
37
  req.body = params.to_json if object_present?(params)
37
38
  end
38
39
  process_response(res)
@@ -40,10 +41,32 @@ class JsonRequester
40
41
  error_response(e)
41
42
  end
42
43
 
44
+ def form_send(http_method, path, params={}, headers={})
45
+ res = @conn.send(http_method) do |req|
46
+ req.url path
47
+ req.headers = headers if object_present?(headers)
48
+ req.headers['Content-Type'] = 'application/x-www-form-urlencoded ;charset=utf-8'
49
+ req.body = URI.encode_www_form(params) if object_present?(params)
50
+ end
51
+ process_response(res)
52
+ rescue => e
53
+ error_response(e)
54
+ end
55
+
56
+ def multipart_form_send(http_method, path, params={}, headers={})
57
+ res = @conn.send(http_method) do |req|
58
+ req.url path
59
+ req.headers = headers if object_present?(headers)
60
+ req.body = params if object_present?(params)
61
+ end
62
+ process_response(res)
63
+ end
64
+
43
65
  private
44
66
 
45
67
  def init_conn
46
68
  Faraday.new(url: host) do |faraday|
69
+ faraday.request :multipart if @multipart # multipart form POST request
47
70
  faraday.request :url_encoded # form-encode POST params
48
71
  faraday.response :logger # log requests to $stdout
49
72
  faraday.adapter Faraday.default_adapter # make requests with Net::HTTP
@@ -69,4 +92,5 @@ class JsonRequester
69
92
  def object_present?(object)
70
93
  !(object.nil? || object.empty?)
71
94
  end
95
+
72
96
  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.1
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: 2019-08-08 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2020-12-17 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