client-api-builder 0.2.2 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6e9f87a8414f328789843aa8a4cc91736c2453908cae60041e4bc780e43fda62
4
- data.tar.gz: ccb71fac620b9beae7a308ae1f575da6ba7850578480b358c26e6191cafe5dfe
3
+ metadata.gz: '01795148989ed0a14c42cb886fd3ce76e3692d82c388f49adf09b7b7229b928f'
4
+ data.tar.gz: 4ce828ecc5ee2017e6318ab83e8c1c02e3a557f746a7f3f6fc8d7471cdac2750
5
5
  SHA512:
6
- metadata.gz: 1139fd2bcf58929790aa3923b4f3fae69f63e4bc23c9e3e59bd785e56997434c287942fb563b7c6f2c7f6689076e00ac07a550a1b198868e2137d46ae33c24de
7
- data.tar.gz: c77f20fc74bada0c03764d945506d8ebcf51fb4a0db96ebf2221d0b4d14191a762f08a76ac046f1450f80b5b6f51f19757d074e733e737294870e779f4ba479e
6
+ metadata.gz: 57b713cda946c263a13f6642d8548442f574eaea1f0374b1e02c7d2f071a3826aeb3e49eaa3a785784eeb7caa671584192f0391e6a508b5dbbd4fff32868063e
7
+ data.tar.gz: d41bfea574c3399d534455ab7165a9fc6f91c347fd5c6d6634fa67731bb5198ccc843532e06fea853097316f766522daebf875cce855694b87e67bb0ea6771fb
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = 'client-api-builder'
5
- s.version = '0.2.2'
5
+ s.version = '0.2.3'
6
6
  s.licenses = ['MIT']
7
7
  s.summary = 'Develop Client API libraries faster'
8
8
  s.description = 'Utility for constructing API clients'
@@ -28,7 +28,30 @@ module ClientApiBuilder
28
28
  request.body = body if body
29
29
 
30
30
  Net::HTTP.start(uri.hostname, uri.port, connection_options.merge(use_ssl: uri.scheme == 'https')) do |http|
31
- http.request(request)
31
+ http.request(request) do |response|
32
+ yield response if block_given?
33
+ end
34
+ end
35
+ end
36
+
37
+ def stream(method:, uri:, body:, headers:, connection_options:)
38
+ request(method: method, uri: uri, body: body, headers: headers, connection_options: connection_options) do |response|
39
+ response.read_body do |chunk|
40
+ yield chunk
41
+ end
42
+ end
43
+ end
44
+
45
+ def stream_to_io(method:, uri:, body:, headers:, connection_options:, io:)
46
+ stream(method: method, uri: uri, body: body, headers: headers, connection_options: connection_options) do |chunk|
47
+ io.write chunk
48
+ end
49
+ end
50
+
51
+ def stream_to_file(method:, uri:, body:, headers:, connection_options:, file:)
52
+ mode = connection_options.delete(:file_mode) || 'wb'
53
+ File.open(file, mode) do |io|
54
+ stream_to_io(method: method, uri: uri, body: body, headers: headers, connection_options: connection_options, io: io)
32
55
  end
33
56
  end
34
57
  end
@@ -7,7 +7,7 @@ module ClientApiBuilder
7
7
  base.extend InheritanceHelper::Methods
8
8
  base.extend ClassMethods
9
9
  base.include ::ClientApiBuilder::NetHTTP::Request
10
- base.attr_reader :response
10
+ base.attr_reader :response, :request_options
11
11
  end
12
12
 
13
13
  module ClassMethods
@@ -192,8 +192,18 @@ module ClientApiBuilder
192
192
  end
193
193
  expected_response_codes.map!(&:to_s)
194
194
 
195
+ stream_param =
196
+ case options[:stream]
197
+ when true,
198
+ :file
199
+ :file
200
+ when :io
201
+ :io
202
+ end
203
+
195
204
  method_args = named_arguments.map { |arg_name| "#{arg_name}:" }
196
205
  method_args += ['body:'] if has_body_param
206
+ method_args += ["#{stream_param}:"] if stream_param
197
207
  method_args += ['**__options__', '&block']
198
208
 
199
209
  code = "def #{method_name}(" + method_args.join(', ') + ")\n"
@@ -205,9 +215,30 @@ module ClientApiBuilder
205
215
  code += " __body__ = build_body(__body__, __options__)\n"
206
216
  code += " __headers__ = build_headers(__options__)\n"
207
217
  code += " __connection_options__ = build_connection_options(__options__)\n"
208
- code += " @response = request(method: #{http_method.inspect}, uri: __uri__, body: __body__, headers: __headers__, connection_options: __connection_options__)\n"
218
+ code += " @request_options = {method: #{http_method.inspect}, uri: __uri__, body: __body__, headers: __headers__, connection_options: __connection_options__}\n"
219
+ code += " @request_options[:#{stream_param}] = #{stream_param}\n" if stream_param
220
+
221
+ case options[:stream]
222
+ when true,
223
+ :file,
224
+ :io
225
+ code += " @response = stream_to_file(**@request_options)\n"
226
+ when :block
227
+ code += " @response = stream(**@request_options, &block)\n"
228
+ else
229
+ code += " @response = request(**@request_options)\n"
230
+ end
231
+
209
232
  code += " expected_response_code!(@response, __expected_response_codes__, __options__)\n"
210
- code += " handle_response(@response, __options__, &block)\n"
233
+
234
+ if options[:stream] || options[:return] == :response
235
+ code += " @response\n"
236
+ elsif options[:return] == :body
237
+ code += " @response.body\n"
238
+ else
239
+ code += " handle_response(@response, __options__, &block)\n"
240
+ end
241
+
211
242
  code += "end\n"
212
243
  code
213
244
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: client-api-builder
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Doug Youch
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-07-06 00:00:00.000000000 Z
11
+ date: 2021-07-17 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Utility for constructing API clients
14
14
  email: dougyouch@gmail.com