kapellmeister 0.4.2 → 0.5.0

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: 2c2bf7cc575b579cafe9ff8f500407e25275220340498dea77ff27d18ec73e35
4
- data.tar.gz: 36476a2563dfcac9ad129188b050b3fc212b3cd8ff9b5b5546ee10afc845ebd6
3
+ metadata.gz: 3f6050eee5b66fbd57d701c5c4af3cd6bc1ee34089d1a0aaa7cf90c7fed12fa3
4
+ data.tar.gz: 47d525ef9f511166657c397257b8989ca02dc6637c360c39a0354c355fa1a595
5
5
  SHA512:
6
- metadata.gz: b62e680f94423d0822ff0a784702b023712ebf313bb40b38634808a4eb2e2d03affb5027e3d064942ba5fa475068ec42d746a9ac053d14d975923a3f85960fba
7
- data.tar.gz: bca17d751f762904131e458423e342eea9e56b521ef6ab869d45d5ff8746aa0519c8dc501d9cf76e4372df8be6205f8559f119c6de8c4ab07ba7b0745f36126c
6
+ metadata.gz: 4471a0c7b78d03d30e3b61f977d81272aa434bb46c35d5f90bb310667cb2f913a2ba0797699401c8dfee6092fcec58c88643b396c0ba80ff46a9c31fa7b598fc
7
+ data.tar.gz: 200139ca7b308fe988c74a9047484b8ed95c78b62cedbba5ff1fd848e40b1d9e8c2fb3f6b6fe06bcfceadc0906bbbdfe7173ae3c652f20616811f804a9bc97aa
data/Gemfile CHANGED
@@ -4,6 +4,7 @@ ruby '3.1.1'
4
4
 
5
5
  gemspec
6
6
 
7
+ gem 'dry-schema'
7
8
  gem 'faraday'
8
9
  gem 'faraday_middleware'
9
10
  gem 'faraday-cookie_jar'
@@ -27,6 +27,7 @@ Gem::Specification.new do |gem|
27
27
 
28
28
  gem.required_ruby_version = '>= 2.4.2'
29
29
 
30
+ gem.add_dependency 'dry-schema', '~> 1.9.1'
30
31
  gem.add_dependency 'faraday', '~> 1.0'
31
32
  gem.add_dependency 'faraday-cookie_jar', '~> 0.0.7'
32
33
  gem.add_dependency 'faraday_middleware', '~> 1.2'
@@ -50,3 +50,8 @@ module Kapellmeister
50
50
  end
51
51
  end
52
52
  end
53
+
54
+ # ROUTES = Dir['app/lib/yandex_taxi/route_scheme/**/*.yml'].each_with_object({}) do |file, result|
55
+ # routes = ::Kapellmeister::Base.routes_scheme_parse(file)
56
+ # result.merge!(routes)
57
+ # end
@@ -11,6 +11,6 @@ class <%= class_name %>::Configuration
11
11
 
12
12
  https = ssl.presence || true
13
13
 
14
- "URI::HTTP#{https.to_b ? 'S' : ''}".build(host:, path: [path, version].join('/'))
14
+ "URI::HTTP#{https.to_b ? 'S' : ''}".constantize.build(host:, path: [path, version].join('/'))
15
15
  end
16
16
  end
@@ -2,10 +2,11 @@
2
2
  # foo: => Wrapper for method
3
3
  # bar: => Method name
4
4
  # scheme: => Scheme description
5
- # method: POST => Request type
6
- # use_wrapper: true => Default true
7
- # path: buz => Real path
8
- # mock: => Mock for development
9
- # token: blablabla
5
+ # method: POST => Request type (* required)
6
+ # use_wrapper: true => Default true (* required)
7
+ # path: buz => Real path (* required)
8
+ # body: => Dry schema for checking parameters. If key doesn't exist nothing happens
9
+ # query: => Query params. If key doesn't exist nothing happens
10
+ # mock: => Structure or Path to mock file for tests. If key doesn't exist nothing happens
10
11
  #
11
12
  # Client.foo_bar { a: 'b' } => POST http://host/foo/buz DATA: { a: 'b' }
@@ -26,14 +26,18 @@ module Kapellmeister::Base
26
26
  end
27
27
 
28
28
  def self.routes_scheme_parse(path)
29
- YAML.safe_load(ERB.new(File.read(path)).result, aliases: true, permitted_classes: [Time]).deep_symbolize_keys
29
+ template = ERB.new(File.read(path)).result
30
+ YAML.safe_load(template, aliases: true, permitted_classes: [Symbol, Date, Time]).deep_symbolize_keys
31
+ rescue Errno::ENOENT => e
32
+ warn "No such file or directory", path
33
+ {}
30
34
  end
31
35
  end
32
36
 
33
37
  def generate_routes(json_scheme)
34
- json_scheme.each_with_object({}) do |(key, value), scheme|
38
+ json_scheme.dup.each_with_object({}) do |(key, value), scheme|
35
39
  scheme[key] = value.delete(:scheme) if (value.is_a?(Hash) && value.key?(:scheme)) || value.is_a?(String)
36
- next if value.length.zero?
40
+ next if value.nil? || value.length.zero?
37
41
 
38
42
  generate_routes(value).map { |deep_key, deep_value| mapping(deep_key, deep_value, key, scheme) }
39
43
  end
@@ -10,22 +10,15 @@ class Kapellmeister::Dispatcher
10
10
 
11
11
  FailedResponse = Struct.new(:success?, :response, :payload)
12
12
 
13
- def connection_by(method, path, data = {})
14
- headers_params = data.delete(:headers) || {}
13
+ def connection_by(method_name, path, data = {}, query = {})
14
+ additional_headers = data.delete(:headers) || {}
15
15
  requests_data = data.delete(:request) || {}
16
16
 
17
- generated_connection = connection(additional_headers: headers_params, requests_data:)
17
+ generated_connection = connection(additional_headers:, requests_data:)
18
18
 
19
- case method.upcase.to_sym
20
- when :GET
21
- process generated_connection.get([path, data.to_query].compact_blank!.join('?'))
22
- when :POST
23
- process generated_connection.post(path, data.to_json)
24
- when :PUT
25
- process generated_connection.put(path, data.to_json)
26
- else
27
- raise "Library can't process method #{method} yet"
28
- end
19
+ process generated_connection.run_request(method_name.downcase.to_sym, path, data.to_json, additional_headers)
20
+ rescue NoMethodError, NameError, RuntimeError
21
+ raise "Library can't process method #{method_name} yet"
29
22
  rescue Faraday::ConnectionFailed, Faraday::TimeoutError => e
30
23
  failed_response(details: e.message)
31
24
  end
@@ -44,7 +37,7 @@ class Kapellmeister::Dispatcher
44
37
  ::Faraday.new(url: configuration.url,
45
38
  headers: headers_generate(**additional_headers),
46
39
  request: requests_generate(**requests_data)) do |faraday|
47
- faraday.request :json, content_type: 'application/json'
40
+ faraday.request :json, content_type: 'application/json; charset=utf-8'
48
41
  faraday.request :multipart
49
42
  faraday.response :logger, logger
50
43
  faraday.response :json, content_type: 'application/json; charset=utf-8'
@@ -56,7 +49,7 @@ class Kapellmeister::Dispatcher
56
49
 
57
50
  def headers_generate(**additional)
58
51
  {
59
- accept: 'application/json, text/plain, */*',
52
+ accept: 'application/json, text/plain, */*, charset=utf-8',
60
53
  **additional,
61
54
  **headers
62
55
  }
@@ -2,16 +2,15 @@ module Kapellmeister::RequestsExtension
2
2
  def self.request_processing
3
3
  proc do |name, request_data|
4
4
  define_method name do |data = {}|
5
- proc { |method:, path:, mock:| # rubocop:disable Lint/UnusedBlockArgument:
6
- # return mock if ENV['APP_ENV'] == 'development'
5
+ proc { |method:, path:, body: {}, query: {}, mock: ''|
6
+ return ::Kapellmeister::Base.routes_scheme_parse(mock) if (Rails.try(:env) || ENV['APP_ENV']) == 'test'
7
7
 
8
- path = path.split('/').map do |part|
9
- next part unless part.include? '%<'
8
+ valid_body?(data, body)
9
+ valid_query?(data, query)
10
10
 
11
- data.delete(part.match(/%<(.*)>/).to_a.last.to_sym)
12
- end.join('/')
11
+ full_path = generate_full_path(path, data)
13
12
 
14
- new.connection_by(method, path, data)
13
+ new.connection_by(method, full_path, data)
15
14
  }.call(**request_data)
16
15
  end
17
16
  end
@@ -23,3 +22,41 @@ module Kapellmeister::RequestsExtension
23
22
  base.module_parent.requests.each(&request_processing)
24
23
  end
25
24
  end
25
+
26
+ def generate_full_path(original_path, data)
27
+ path = generate_path(original_path, data)
28
+ [path, data.delete(:query)&.to_query].compact_blank!.join('?')
29
+ end
30
+
31
+ def generate_path(original_path, data)
32
+ original_path.split('/').map do |part|
33
+ next part unless part.include? '%<'
34
+
35
+ data.delete(part.match(/%<(.*)>/).to_a.last.to_sym)
36
+ end.join('/')
37
+ end
38
+
39
+ def valid_body?(data, body)
40
+ return if body.blank? || body.is_a?(Hash)
41
+
42
+ schema = Object.const_get(body).schema
43
+ result = schema.(data)
44
+ return data if result.success?
45
+
46
+ raise ArgumentError, result.errors.to_h
47
+ end
48
+
49
+ def valid_query?(data, query)
50
+ return if query.blank?
51
+ required_keys = query.keys
52
+
53
+ from_data = data.slice(*required_keys)
54
+ data.except!(*required_keys)
55
+ data[:query] ||= {}
56
+ data[:query] = data[:query].to_h.merge!(from_data)
57
+
58
+ diffirent_keys = data[:query].transform_keys(&:to_sym)
59
+ return if required_keys.all? { |key| diffirent_keys.has_key? key.to_sym }
60
+
61
+ raise ArgumentError, "query params needs keys #{required_keys}"
62
+ end
@@ -1,3 +1,3 @@
1
1
  module Kapellmeister
2
- VERSION = '0.4.2'.freeze
2
+ VERSION = '0.5.0'.freeze
3
3
  end
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kapellmeister
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.2
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - DarkWater
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-05-01 00:00:00.000000000 Z
11
+ date: 2022-05-10 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: dry-schema
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 1.9.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 1.9.1
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: faraday
15
29
  requirement: !ruby/object:Gem::Requirement