http-client-generator 0.1.17 → 0.1.18

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: d13c3e296e87d5522914504f9ed84dcbca0452fd4285764e560b845c0e160312
4
- data.tar.gz: 93edde393ce7d949f4ea903bb095fa51a15bd6ba872d4621a74cd2635979b417
3
+ metadata.gz: 34acc8da4293a489562f3d1c19e610344592ccee766f3172fceb1a5906311d55
4
+ data.tar.gz: 7d552173fce4777eb338d8e8b0633c66273e5fdea8ff165493b445cb7a03a371
5
5
  SHA512:
6
- metadata.gz: eaa744821cfed7698e157dca17cc5af555b2dffadd0aeaf3cc1fb051e27866c1d0ac50939dce69601a112cd2375b85028b5544bf9a129fd949fbdd2f569d6395
7
- data.tar.gz: 34e83bd6d03a8c2289fcf8c44c6046fb4f3890f414cec1e46285bd2e63ee9255778cb51050b1355ca6588dffd26666c7f77b2f6abe9da031517a3b0a04eb3e70
6
+ metadata.gz: 62522568c0933ae7084549b00d6cdd74cfe72eee36926b0c9b9b1935f8404b528f1e7e3cfeced936faca5e8c79c52445d21b69cca67026244ed47545a657cfc7
7
+ data.tar.gz: dfa1cc9dcef1bfe3bcbc39b4c689b04304e4a2638e275e251986816d4cdc052d8ea95add0c59eff7d7acd1186227d4420ab9e52fe0479466220e9e509b1135ca
@@ -5,13 +5,22 @@ module HttpClientGenerator
5
5
  class SetHeader
6
6
  Plugs.register :set_header, self
7
7
 
8
- def initialize(arg:, header:)
8
+ def initialize(arg:, header:, func: nil)
9
9
  @arg = arg
10
10
  @header = header
11
+ @func = func
11
12
  end
12
13
 
13
14
  def call(req)
14
- req.headers[@header] = req.rest_args[@arg]
15
+ arg = req.rest_args[@arg]
16
+
17
+ req.headers[@header] =
18
+ if @func
19
+ @func.(arg)
20
+ else
21
+ arg
22
+ end
23
+
15
24
  req
16
25
  end
17
26
  end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ module HttpClientGenerator
4
+ class Plugs
5
+ class ValidateResponse
6
+ Plugs.register :validate_response, self
7
+
8
+ module SchemaBuilder
9
+ def schema_for(name, &block)
10
+ @schemas ||= {}
11
+ @schemas[name] = Datacaster.choosy_schema(&block)
12
+
13
+ define_singleton_method(name) do |params|
14
+ @schemas[name].(params)
15
+ end
16
+ end
17
+ end
18
+
19
+ def initialize(schema_helper:, strict: false)
20
+ @schema_helper = schema_helper
21
+ @strict = strict
22
+ end
23
+
24
+ def call(req)
25
+ schema_name = :"#{req.name}_#{req.verb}"
26
+
27
+ return req unless @schema_helper.responds_to?(schema_name)
28
+
29
+ req.response_body = @schema_helper.public_send(schema_name)
30
+ .value_or { |e| req.raise_message(e) }
31
+
32
+ req
33
+ end
34
+ end
35
+ end
36
+ end
37
+
@@ -15,10 +15,11 @@ module HttpClientGenerator
15
15
  end
16
16
  end
17
17
 
18
- attr_accessor :verb, :content_type, :url, :headers, :body, :rest_args, :response_body, :base, :extra
18
+ attr_accessor :verb, :name, :content_type, :url, :headers, :body, :rest_args, :response_body, :base, :extra
19
19
 
20
- def initialize(base:, verb:, content_type:, url:, body:, rest_args:)
20
+ def initialize(base:, name:, verb:, content_type:, url:, body:, rest_args:)
21
21
  @base = base
22
+ @name = name
22
23
  @verb = verb
23
24
  @content_type = content_type
24
25
  @url = url
@@ -37,6 +38,11 @@ module HttpClientGenerator
37
38
  raise base::RequestError, e.message, e.backtrace, cause: nil
38
39
  end
39
40
 
41
+ def raise_message(message)
42
+ Sentry.capture_message(message, extra: { request_id: extra[:request_id] }) if Object.const_defined?(:Sentry)
43
+ raise base::RequestError, message
44
+ end
45
+
40
46
  private
41
47
 
42
48
  def default_headers
@@ -38,7 +38,7 @@ module HttpClientGenerator
38
38
  url = url_helper.public_send(:"#{name}", url_options)
39
39
 
40
40
  request = Request.new(
41
- verb: verb, url: url, content_type: content_type, body: body, rest_args: rest_args, base: base
41
+ name: name, verb: verb, url: url, content_type: content_type, body: body, rest_args: rest_args, base: base
42
42
  )
43
43
 
44
44
  req_plugs.reduce(request) { |req, plug| plug.call(req) }
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module HttpClientGenerator
4
- VERSION = '0.1.17'
4
+ VERSION = '0.1.18'
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.17
4
+ version: 0.1.18
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pavel Egorov
@@ -54,6 +54,7 @@ files:
54
54
  - lib/http_client_generator/plugs/set_header.rb
55
55
  - lib/http_client_generator/plugs/set_request_id.rb
56
56
  - lib/http_client_generator/plugs/underscore_response.rb
57
+ - lib/http_client_generator/plugs/validate_response.rb
57
58
  - lib/http_client_generator/request.rb
58
59
  - lib/http_client_generator/resource.rb
59
60
  - lib/http_client_generator/resources_definition.rb