http-client-generator 0.1.34 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: acddb4318af4f465b24564f5a26ed9e6d4321c9f42f5956da78a725abd8fca9d
4
- data.tar.gz: 3c7646ba26b536407699c2d8ebec413ebd202c4da224b13824e3c46098a501aa
3
+ metadata.gz: 21d65c38e42bcd0404d1eaf2e444adfae16136940b1c162c41f29757cd88531b
4
+ data.tar.gz: 869036773a9dbcac9ea6651304a97d48c5fd8d5ee48a6d34dbc052c88bebd9eb
5
5
  SHA512:
6
- metadata.gz: fb513ea7142a239df8711486aa24b89bf1b5c9615cce290c236bd65920309d68ab70f1b656bc26ebafe7d4323ff745a6661c7187ab0760ff2560fa707934656e
7
- data.tar.gz: dd425389e7a6c5484a28e55c00f05f644431870f8654b75a5a9bebd2e28a28078c4eea974ff2b9085cf1c8a37b462b9b98aaec5e2ce624460c316364c7990100
6
+ metadata.gz: bd8c24c756289f12cf0868a39b2954898b05c9eface230e1f2c3c34e2b63fca31fa9654e810e2d39348b31758929a3a96bcded22d3155110f0757e56804a0df5
7
+ data.tar.gz: aa75c5897ed9e37c3bea2a432160d0226a6d3b43ec267eac87c7dcd362983848f46f15eba413e3ef65e7adda4d43cab5b5aa5d52e2ca20e401770dab0d40b61a
@@ -5,13 +5,16 @@ module HttpClientGenerator
5
5
  class SetHeader
6
6
  Plugs.register :set_header, self
7
7
 
8
- def initialize(arg:, header:, func: nil)
8
+ def initialize(arg: nil, value: nil, header:, func: nil)
9
9
  @arg = arg
10
10
  @header = header
11
11
  @func = func
12
+ @value = value
12
13
  end
13
14
 
14
15
  def call(req)
16
+ return process_header_value(req) if @value
17
+
15
18
  arg = req.rest_args[@arg]
16
19
 
17
20
  req.headers[@header] =
@@ -26,6 +29,19 @@ module HttpClientGenerator
26
29
 
27
30
  req
28
31
  end
32
+
33
+ private
34
+
35
+ def process_header_value(req)
36
+ req.headers[@header] =
37
+ if @value.is_a?(Proc)
38
+ @value.()
39
+ else
40
+ @value
41
+ end
42
+
43
+ req
44
+ end
29
45
  end
30
46
  end
31
47
  end
@@ -35,7 +35,7 @@ module HttpClientGenerator
35
35
 
36
36
  def raw_body
37
37
  if json?
38
- body.to_json
38
+ body && body.to_json
39
39
  else
40
40
  body
41
41
  end
@@ -11,8 +11,8 @@ module HttpClientGenerator
11
11
  @base = base
12
12
  @content_type = content_type
13
13
  @name = name
14
- @req_plugs = req_plugs
15
- @resp_plugs = resp_plugs
14
+ @req_plugs = select_plugs(req_plugs)
15
+ @resp_plugs = select_plugs(resp_plugs)
16
16
  end
17
17
 
18
18
  def perform_request(url_helper, url_options, body, rest_args)
@@ -48,5 +48,16 @@ module HttpClientGenerator
48
48
 
49
49
  request.response_body
50
50
  end
51
+
52
+ def select_plugs(plug_entries)
53
+ plug_entries.filter_map do |entry|
54
+ entry in only:, except:, plug:
55
+
56
+ next if only.any? && !only.include?(name)
57
+ next if except.include?(name)
58
+
59
+ plug
60
+ end
61
+ end
51
62
  end
52
63
  end
@@ -23,12 +23,12 @@ module HttpClientGenerator
23
23
  end
24
24
  end
25
25
 
26
- def req_plug(plug, *args, **kwargs)
27
- @req_plugs << build_plug(plug, *args, **kwargs)
26
+ def req_plug(plug, *args, only: nil, except: nil, **kwargs)
27
+ @req_plugs << build_plug_entry(plug, *args, only: only, except: except, **kwargs)
28
28
  end
29
29
 
30
- def resp_plug(plug, *args, **kwargs)
31
- @resp_plugs << build_plug(plug, *args, **kwargs)
30
+ def resp_plug(plug, *args, only: nil, except: nil, **kwargs)
31
+ @resp_plugs << build_plug_entry(plug, *args, only: only, except: except, **kwargs)
32
32
  end
33
33
 
34
34
  def namespace(_name, &block)
@@ -39,6 +39,14 @@ module HttpClientGenerator
39
39
 
40
40
  private
41
41
 
42
+ def build_plug_entry(plug, *args, only:, except:, **kwargs)
43
+ {
44
+ plug: build_plug(plug, *args, **kwargs),
45
+ only: Array(only).compact.uniq.map(&:to_sym),
46
+ except: Array(except).compact.uniq.map(&:to_sym)
47
+ }
48
+ end
49
+
42
50
  def build_plug(plug, *args, **kwargs)
43
51
  if plug.respond_to?(:call)
44
52
  plug
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module HttpClientGenerator
4
- VERSION = '0.1.34'
4
+ VERSION = '0.2.1'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: http-client-generator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.34
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pavel Egorov