json_requester 1.0.10 → 1.0.11

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 +28 -8
  3. data/lib/json_requester.rb +16 -13
  4. metadata +22 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 61edfdc55b1925b3e1907fe89a32298b4baf64e2f63c7f7781bb3b1599801dce
4
- data.tar.gz: 73be7e54ea0e2523f32c500bd2367caf339207173a1e17ab3d8a0bf5230648a6
3
+ metadata.gz: 9f5093495b6cc5e5ea2778c405bac7f712130a0d5a4e4b4dbd5d307606c20b98
4
+ data.tar.gz: a01404be960fc093d0637ab4890981d42a67161fea04f26151984bf661a6e48c
5
5
  SHA512:
6
- metadata.gz: 8666c62ffea8ed9a5c5f9b5ff46ffdddd2e11ff972508b6d7c13ceeb2f9101f01d84724baf06b0a89ec4a3d563e6dd7e2e786ed93f306595d4c3a0f41ca968a2
7
- data.tar.gz: 001c495f700f71de07002edc11a92a7e9b7c862c2e9edb5c207ea8496c966753258242f514a69de1133eb7af16e2bab73a27ec77958ddc50da18182bb523dc68
6
+ metadata.gz: 95a2aedf37245b98f0167a7e099c5dbf650796dab009025b3075022cb5d8fe9c56ff5759b1075e7d272fcad51a43a68a33e999558d1f0e3932d099eb14bb9781
7
+ data.tar.gz: 6d32b1f828a49cb7f9fd98a6c1ea487feb74472e1cd64ec6256c4fccb4969a9389144c15d26957de2de838b162d41af0c1c4f01845a6dce4981b79eba737555a
data/README.md CHANGED
@@ -4,15 +4,35 @@
4
4
 
5
5
  ### Install
6
6
 
7
- gem install json_requester
7
+ ```bash
8
+ $ gem install json_requester
9
+ ```
8
10
 
9
11
  ### How to Use
10
12
 
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' }
15
- puts res['body_status'] # 'success'
13
+ ```ruby
14
+ host = 'http://httpbingo.org'
15
+ # `timeout` at Faraday gem default is 60 secs.
16
+ requester = JsonRequester.new(host, timeout: 120)
17
+
18
+ http_method = :get # :get / :post / :put / :delete
19
+ path = ''
20
+ headers = { 'Authorization' => 'Bearer token' }
16
21
 
17
- puts res['status'] # 500
18
- puts res['message'] # invalid method
22
+ # The `:get` method will use query params;
23
+ # Other HTTP methods will use JSON body to request.
24
+ params = {
25
+ key_1: 'value_1',
26
+ key_2: 'value_2'
27
+ }
28
+
29
+ # Request by using JSON body or query params, use the `http_send` method.
30
+ # other methods: `form_send`, `multipart_form_send`
31
+ # `sort_params` at Faraday gem default is true.
32
+ res = requester.http_send(http_method, path, params, headers, sort_params: true)
33
+
34
+ # http response code
35
+ puts res['status'] # 200, 404, .. etc
36
+ # the response JSON body
37
+ puts res['body'] # { foo: 'bar' }
38
+ ```
@@ -11,17 +11,17 @@ class JsonRequester
11
11
  @timeout = timeout
12
12
  end
13
13
 
14
- def http_send(http_method, path, params={}, headers={})
14
+ def http_send(http_method, path, params={}, headers={}, sort_params: true)
15
15
  puts "send #{http_method} reqeust to #{@host} with\npath: #{path}\nparams: #{params}\nheaders: #{headers}"
16
16
  if http_method == :get
17
- normal_send(http_method, path, params, headers)
17
+ normal_send(http_method, path, params, headers, sort_params: sort_params)
18
18
  else
19
- json_send(http_method, path, params, headers)
19
+ json_send(http_method, path, params, headers, sort_params: sort_params)
20
20
  end
21
21
  end
22
22
 
23
- def normal_send(http_method, path, params={}, headers={})
24
- conn = init_conn
23
+ def normal_send(http_method, path, params={}, headers={}, sort_params: true)
24
+ conn = init_conn(sort_params: sort_params)
25
25
  res = conn.send(http_method) do |req|
26
26
  req.url path
27
27
  req.headers = headers if object_present?(headers)
@@ -32,8 +32,8 @@ class JsonRequester
32
32
  error_response(e)
33
33
  end
34
34
 
35
- def json_send(http_method, path, params={}, headers={})
36
- conn = init_conn
35
+ def json_send(http_method, path, params={}, headers={}, sort_params: true)
36
+ conn = init_conn(sort_params: sort_params)
37
37
  res = conn.send(http_method) do |req|
38
38
  req.url path
39
39
  req.headers = headers if object_present?(headers)
@@ -45,12 +45,12 @@ class JsonRequester
45
45
  error_response(e)
46
46
  end
47
47
 
48
- def form_send(http_method, path, params={}, headers={})
49
- conn = init_conn
48
+ def form_send(http_method, path, params={}, headers={}, sort_params: true)
49
+ conn = init_conn(sort_params: sort_params)
50
50
  res = conn.send(http_method) do |req|
51
51
  req.url path
52
52
  req.headers = headers if object_present?(headers)
53
- req.headers['Content-Type'] = 'application/x-www-form-urlencoded ;charset=utf-8'
53
+ req.headers['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8'
54
54
  req.body = URI.encode_www_form(params) if object_present?(params)
55
55
  end
56
56
  process_response(res)
@@ -58,8 +58,8 @@ class JsonRequester
58
58
  error_response(e)
59
59
  end
60
60
 
61
- def multipart_form_send(http_method, path, params={}, headers={})
62
- conn = init_conn
61
+ def multipart_form_send(http_method, path, params={}, headers={}, sort_params: true)
62
+ conn = init_conn(sort_params: sort_params)
63
63
  res = conn.send(http_method) do |req|
64
64
  req.url path
65
65
  req.headers = headers if object_present?(headers)
@@ -70,7 +70,10 @@ class JsonRequester
70
70
 
71
71
  private
72
72
 
73
- def init_conn
73
+ def init_conn(sort_params: true)
74
+ # https://lostisland.github.io/faraday/#/customization/index?id=order-of-parameters
75
+ Faraday::NestedParamsEncoder.sort_params = sort_params # faraday default is true
76
+
74
77
  Faraday.new(url: host, ssl: { verify: @ssl_verify }) do |faraday|
75
78
  faraday.request :multipart if @multipart # multipart form POST request
76
79
  faraday.request :url_encoded # form-encode POST params
metadata CHANGED
@@ -1,19 +1,22 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json_requester
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.10
4
+ version: 1.0.11
5
5
  platform: ruby
6
6
  authors:
7
7
  - JiaRou Lee
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-07-03 00:00:00.000000000 Z
11
+ date: 2023-08-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
17
20
  - - ">="
18
21
  - !ruby/object:Gem::Version
19
22
  version: 1.0.0
@@ -21,9 +24,26 @@ dependencies:
21
24
  prerelease: false
22
25
  version_requirements: !ruby/object:Gem::Requirement
23
26
  requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '1.0'
24
30
  - - ">="
25
31
  - !ruby/object:Gem::Version
26
32
  version: 1.0.0
33
+ - !ruby/object:Gem::Dependency
34
+ name: pry
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: 0.14.1
40
+ type: :development
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: 0.14.1
27
47
  description: wrapper of faraday
28
48
  email: laura34963@kdanmobile.com
29
49
  executables: []