kapellmeister 0.6.2 → 0.7.2

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: 409792ce404710854c377a9d87c4b36e03d86b03e0370459bc6b99dfacfec516
4
- data.tar.gz: 6cee24733a1acfc189bbdb34b2f3b3d28857402ba926cc77ce58f2d4be853164
3
+ metadata.gz: 03e830a5335e55d2acd0db7d93e54bf12e657fc0cf21cc9c5676cf90a0cddc80
4
+ data.tar.gz: db3fc0c80480c15e84e6a40e019b569d7a7dc1645d1eaed8a28b73ccaf8b87a3
5
5
  SHA512:
6
- metadata.gz: 1d67914af1df363b0e89cfd7a71f48419da7db47350f307c7e0c251c0a4e3252446e10c1a33769e30c6bcf7c4b4f10c09d75a96608052aff68a7473e31ca74a8
7
- data.tar.gz: 373bac9c6d765bfb2a0350bb21dcfff117839e76b82b7a09dd3dc7ef48c77bba8090dc311c936c9e956b5cce3b0616a5f0ed00c2a98679ac34ec93c5bacee425
6
+ metadata.gz: c79f2c5b731932fa74af03a1ddf08f89ed089d09104cac257d6720c9cdcccb0689ead099cf4a9a32b83b7c323434387a463b9cd9ea7a26615f7812d6bb8c6281
7
+ data.tar.gz: 69b47a4b15b8075a772273dc99a2a41e710859db437714b3a4e8e5963b79dacd6a7b69f59ee676e78e55c4e9e8267a774ec76093c0c593c0ce5bcc084e2697b8
@@ -1,28 +1,21 @@
1
1
  require 'faraday_middleware'
2
+ require_relative './requests_extension'
2
3
 
3
4
  class Kapellmeister::Dispatcher
5
+ include Kapellmeister::RequestsExtension
6
+ delegate :request_processing, to: Kapellmeister::RequestsExtension
7
+
8
+ def initialize
9
+ self.class.module_parent.requests.each(&request_processing)
10
+ end
11
+
4
12
  def self.inherited(base)
5
13
  super
6
- base.extend(Kapellmeister::RequestsExtension)
7
-
8
14
  delegate :report, :logger, to: base.module_parent
9
15
  end
10
16
 
11
17
  FailedResponse = Struct.new(:success?, :response, :payload)
12
18
 
13
- def connection_by(method_name, path, data = {})
14
- additional_headers = data.delete(:headers) || {}
15
- requests_data = data.delete(:request) || {}
16
-
17
- generated_connection = connection(additional_headers: additional_headers, requests_data: requests_data) # rubocop:disable Style/HashSyntax (for support ruby 2.4+)
18
-
19
- process generated_connection.run_request(method_name.downcase.to_sym, path, data.to_json, additional_headers)
20
- rescue NameError, RuntimeError
21
- raise "Library can't process method #{method_name} yet"
22
- rescue Faraday::ConnectionFailed, Faraday::TimeoutError => e
23
- failed_response(details: e.message)
24
- end
25
-
26
19
  def headers
27
20
  {}
28
21
  end
@@ -31,16 +24,36 @@ class Kapellmeister::Dispatcher
31
24
  {}
32
25
  end
33
26
 
27
+ def query_params
28
+ {}
29
+ end
30
+
34
31
  def configuration
35
32
  self.class.module_parent.configuration
36
33
  end
37
34
 
38
35
  private
39
36
 
37
+ def connection_by(method_name, path, data = {})
38
+ additional_headers = data.delete(:headers) || {}
39
+ requests_data = data.delete(:request) || {}
40
+ data_json = data.blank? ? '' : data.to_json
41
+
42
+ generated_connection = connection(additional_headers: additional_headers, requests_data: requests_data) # rubocop:disable Style/HashSyntax (for support ruby 2.4+)
43
+
44
+ process generated_connection.run_request(method_name.downcase.to_sym,
45
+ url_with_params(path),
46
+ data_json,
47
+ additional_headers)
48
+
49
+ rescue Faraday::ConnectionFailed, Faraday::TimeoutError => e
50
+ failed_response(details: e.message)
51
+ end
52
+
40
53
  def connection(additional_headers:, requests_data:)
41
- ::Faraday.new(url: configuration.url,
42
- headers: headers_generate(**additional_headers),
43
- request: requests_generate(**requests_data)) do |faraday|
54
+ @connection ||= ::Faraday.new(url: configuration.url,
55
+ headers: headers_generate(**additional_headers),
56
+ request: requests_generate(**requests_data)) do |faraday|
44
57
  faraday.request :json, content_type: 'application/json; charset=utf-8'
45
58
  faraday.request :multipart
46
59
  faraday.response :logger, logger
@@ -66,10 +79,23 @@ class Kapellmeister::Dispatcher
66
79
  }
67
80
  end
68
81
 
82
+ def path_generate(path)
83
+ path.query_parameters
84
+ end
85
+
69
86
  def process(data)
70
87
  report(data).result
71
88
  end
72
89
 
90
+ def url_with_params(url)
91
+ return url if query_params.blank?
92
+
93
+ uri = URI(url)
94
+ params = URI.decode_www_form(uri.query || '').to_h.merge(query_params)
95
+ uri.query = URI.encode_www_form(params)
96
+ uri.to_s
97
+ end
98
+
73
99
  def failed_response(**args)
74
100
  FailedResponse.new(false, { message: "#{self.class} no connection" }, { status: 555, **args })
75
101
  end
@@ -3,24 +3,22 @@ module Kapellmeister::RequestsExtension
3
3
  proc do |name, request_data|
4
4
  define_method name do |data = {}|
5
5
  proc { |method:, path:, body: {}, query_params: {}, mock: ''|
6
- return ::Kapellmeister::Base.routes_scheme_parse(mock) if (Rails.try(:env) || ENV['APP_ENV']) == 'test'
6
+ if (Rails.try(:env) || ENV.fetch('APP_ENV', nil)) == 'test'
7
+ return ::Kapellmeister::Base.routes_scheme_parse(mock)
8
+ end
7
9
 
8
10
  valid_body?(data, body)
9
11
  valid_query?(data, query_params)
10
12
 
11
13
  full_path = generate_full_path(path, data)
12
14
 
13
- new.connection_by(method, full_path, data)
15
+ connection_by(method, full_path, data)
14
16
  }.call(**request_data)
15
17
  end
16
18
  end
17
19
  rescue NoMethodError
18
20
  raise "You need to define #{self} class with connection_by method"
19
21
  end
20
-
21
- def self.extended(base)
22
- base.module_parent.requests.each(&request_processing)
23
- end
24
22
  end
25
23
 
26
24
  def generate_full_path(original_path, data)
@@ -40,7 +38,7 @@ def valid_body?(data, body)
40
38
  return if body.blank? || body.is_a?(Hash)
41
39
 
42
40
  schema = Object.const_get(body).schema
43
- result = schema.(data)
41
+ result = schema.call(data)
44
42
  return data if result.success?
45
43
 
46
44
  raise ArgumentError, result.errors.to_h
@@ -48,6 +46,7 @@ end
48
46
 
49
47
  def valid_query?(data, query)
50
48
  return if query.blank?
49
+
51
50
  required_keys = query.keys
52
51
 
53
52
  from_data = data.slice(*required_keys)
@@ -55,8 +54,8 @@ def valid_query?(data, query)
55
54
  data[:query_params] ||= {}
56
55
  data[:query_params] = data[:query_params].to_h.merge!(from_data)
57
56
 
58
- diffirent_keys = data[:query_params].transform_keys(&:to_sym)
59
- return if required_keys.all? { |key| diffirent_keys.has_key? key.to_sym }
57
+ different_keys = data[:query_params].transform_keys(&:to_sym)
58
+ return if required_keys.all? { |key| different_keys.key? key.to_sym }
60
59
 
61
60
  raise ArgumentError, "Query params needs keys #{required_keys}"
62
61
  end
@@ -1,3 +1,3 @@
1
1
  module Kapellmeister
2
- VERSION = '0.6.2'.freeze
2
+ VERSION = '0.7.2'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kapellmeister
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.2
4
+ version: 0.7.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - DarkWater
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-08-05 00:00:00.000000000 Z
11
+ date: 2022-08-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: dry-schema