bbk-utils 1.0.1.68971 → 1.0.1.72916

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,93 +1,98 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  require 'uri'
3
4
 
4
5
  module BBK
5
- module EnvHelper
6
- def self.prepare_database_envs(env)
7
- uri = build_uri_with_defaults(env)
8
- apply_env_from_uri(env, uri)
9
- env
10
- end
6
+ module Utils
7
+ module EnvHelper
11
8
 
12
- def self.prepare_mq_envs(env)
13
- apply_mq_env_from_uri(env, build_mq_uri_with_defaults(env))
14
- env
15
- end
9
+ def self.prepare_database_envs(env)
10
+ uri = build_uri_with_defaults(env)
11
+ apply_env_from_uri(env, uri)
12
+ env
13
+ end
16
14
 
17
- def self.build_uri_with_defaults(env)
18
- ::URI.parse(env['DATABASE_URL'] || '').tap do |uri|
19
- uri.scheme = env.fetch('DATABASE_ADAPTER', uri.scheme) || 'postgresql'
20
- uri.user = env.fetch('DATABASE_USER', uri.user) || 'postgres'
21
- uri.password = env.fetch('DATABASE_PASS', uri.password)
22
- uri.hostname = env.fetch('DATABASE_HOST', uri.hostname) || 'db'
23
- uri.port = env.fetch('DATABASE_PORT', uri.port) || 5432
15
+ def self.prepare_mq_envs(env)
16
+ apply_mq_env_from_uri(env, build_mq_uri_with_defaults(env))
17
+ env
18
+ end
24
19
 
25
- name = env.fetch('DATABASE_NAME', uri.path) || ''
26
- name = "/#{name}" unless name.start_with?('/')
27
- uri.path = name
20
+ def self.build_uri_with_defaults(env)
21
+ ::URI.parse(env['DATABASE_URL'] || '').tap do |uri|
22
+ uri.scheme = env.fetch('DATABASE_ADAPTER', uri.scheme) || 'postgresql'
23
+ uri.user = env.fetch('DATABASE_USER', uri.user) || 'postgres'
24
+ uri.password = env.fetch('DATABASE_PASS', uri.password)
25
+ uri.hostname = env.fetch('DATABASE_HOST', uri.hostname) || 'db'
26
+ uri.port = env.fetch('DATABASE_PORT', uri.port) || 5432
27
+
28
+ name = env.fetch('DATABASE_NAME', uri.path) || ''
29
+ name = "/#{name}" unless name.start_with?('/')
30
+ uri.path = name
31
+
32
+ if uri.query
33
+ params = URI.decode_www_form(uri.query).to_h
34
+ params['pool'] = env.fetch('DATABASE_POOL', params['pool'])
35
+ uri.query = URI.encode_www_form(params)
36
+ end
37
+ end
38
+ end
39
+
40
+ def self.apply_env_from_uri(env, uri)
41
+ env['DATABASE_URL'] = uri.to_s
42
+ env['DATABASE_ADAPTER'] = uri.scheme
43
+ env['DATABASE_USER'] = uri.user
44
+ env['DATABASE_PASS'] = uri.password
45
+ env['DATABASE_HOST'] = uri.hostname
46
+ env['DATABASE_PORT'] = uri.port.to_s
47
+ env['DATABASE_NAME'] = uri.path[1..-1]
28
48
 
29
49
  if uri.query
30
50
  params = URI.decode_www_form(uri.query).to_h
31
- params['pool'] = env.fetch('DATABASE_POOL', params['pool'])
32
- uri.query = URI.encode_www_form(params)
51
+ env['DATABASE_POOL'] = params['pool']
33
52
  end
34
-
35
- end
36
- end
37
-
38
- def self.apply_env_from_uri(env, uri)
39
- env['DATABASE_URL'] = uri.to_s
40
- env['DATABASE_ADAPTER'] = uri.scheme
41
- env['DATABASE_USER'] = uri.user
42
- env['DATABASE_PASS'] = uri.password
43
- env['DATABASE_HOST'] = uri.hostname
44
- env['DATABASE_PORT'] = uri.port.to_s
45
- env['DATABASE_NAME'] = uri.path[1..-1]
46
-
47
- if uri.query
48
- params = URI.decode_www_form(uri.query).to_h
49
- env['DATABASE_POOL'] = params['pool']
50
53
  end
51
54
 
52
- end
55
+ def self.build_mq_uri_with_defaults(env)
56
+ # Only first MQ_URL selected as template if any
57
+ url = [env.fetch('MQ_URL', '').split(/[;|]/)].flatten.select(&:present?).first || ''
53
58
 
54
- def self.build_mq_uri_with_defaults(env)
55
- # Only first MQ_URL selected as template if any
56
- url = [env.fetch('MQ_URL', '').split(/[;\|]/)].flatten.select(&:present?).first || ''
59
+ # all hosts if form of list fills url template
60
+ hosts = [env.fetch('MQ_HOST',
61
+ URI.parse(url).hostname || 'mq').split(/[;|]/)].flatten.select(&:present?).uniq
57
62
 
58
- # all hosts if form of list fills url template
59
- hosts = [env.fetch('MQ_HOST', URI.parse(url).hostname || 'mq').split(/[;\|]/)].flatten.select(&:present?).uniq
63
+ hosts.map do |host|
64
+ URI.parse(url).tap do |uri|
65
+ uri.scheme = uri.scheme || 'amqps'
66
+ uri.hostname = host
67
+ uri.port = env.fetch('MQ_PORT', uri.port) || 5671
68
+ uri.user = env.fetch('MQ_USER', uri.user)
69
+ uri.password = env.fetch('MQ_PASS', uri.password)
60
70
 
61
- hosts.map do |host|
62
- URI.parse(url).tap do |uri|
63
- uri.scheme = uri.scheme || 'amqps'
64
- uri.hostname = host
65
- uri.port = env.fetch('MQ_PORT', uri.port) || 5671
66
- uri.user = env.fetch('MQ_USER', uri.user)
67
- uri.password = env.fetch('MQ_PASS', uri.password)
71
+ vhost = [env.fetch('MQ_VHOST', uri.path), '/'].find(&:present?)
72
+ vhost = "/#{vhost}" unless vhost.start_with?('/')
68
73
 
69
- vhost = [env.fetch('MQ_VHOST', uri.path), '/'].find(&:present?)
70
- vhost = "/#{vhost}" unless vhost.start_with?('/')
74
+ uri.path = vhost
75
+ end
76
+ end
77
+ end
71
78
 
72
- uri.path = vhost
79
+ def self.apply_mq_env_from_uri(env, uris)
80
+ uri = uris.first
81
+
82
+ env['MQ_URL'] = uris.map(&:to_s).join(';')
83
+ env['MQ_HOST'] = uris.map(&:hostname).join(';')
84
+ env['MQ_PORT'] = uri.port.to_s
85
+ env['MQ_PASS'] = uri.password
86
+ env['MQ_USER'] = uri.user
87
+ vhost = if uri.path == '/'
88
+ uri.path
89
+ else
90
+ uri.path.gsub(%r{\A/}, '')
73
91
  end
92
+ env['MQ_VHOST'] = vhost
74
93
  end
75
- end
76
94
 
77
- def self.apply_mq_env_from_uri(env, uris)
78
- uri = uris.first
79
-
80
- env['MQ_URL'] = uris.map(&:to_s).join(';')
81
- env['MQ_HOST'] = uris.map(&:hostname).join(';')
82
- env['MQ_PORT'] = uri.port.to_s
83
- env['MQ_PASS'] = uri.password
84
- env['MQ_USER'] = uri.user
85
- vhost = if uri.path == '/'
86
- uri.path
87
- else
88
- uri.path.gsub(%r{\A/}, '')
89
- end
90
- env['MQ_VHOST'] = vhost
91
95
  end
92
96
  end
93
97
  end
98
+
@@ -1,15 +1,22 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'logger'
2
4
 
3
5
  module BBK
4
- class LogFormatter < ::Logger::Formatter
5
- FORMAT = "%5s [%sUTC #%d] (%s)[%s]: %s\n".freeze
6
- def call(severity, time, progname, msg)
7
- line = msg2str(msg).gsub("\n", '\\n')
8
- format(FORMAT, severity, format_datetime(time.utc), Process.pid, progname, thread_id, line)
9
- end
6
+ module Utils
7
+ class LogFormatter < ::Logger::Formatter
8
+
9
+ FORMAT = "%5s [%sUTC #%d] (%s)[%s]: %s\n"
10
+ def call(severity, time, progname, msg)
11
+ line = msg2str(msg).gsub("\n", '\\n')
12
+ format(FORMAT, severity, format_datetime(time.utc), Process.pid, progname, thread_id, line)
13
+ end
14
+
15
+ def thread_id
16
+ [Thread.current.object_id.to_s, Thread.current.name].compact.join('@')
17
+ end
10
18
 
11
- def thread_id
12
- [Thread.current.object_id.to_s, Thread.current.name].compact.join('@')
13
19
  end
14
20
  end
15
21
  end
22
+
@@ -5,44 +5,47 @@ require 'active_support/tagged_logging'
5
5
  require 'bbk/utils/log_formatter'
6
6
 
7
7
  module BBK
8
- class Logger < ::Logger
9
- DEFAULT_NAME = 'bbk'
10
- DEFAULT_LEVEL = Logger::Severity::DEBUG
8
+ module Utils
9
+ class Logger < ::Logger
11
10
 
12
- def self.new(*args, **kwargs)
13
- instance = super
14
- ActiveSupport::TaggedLogging.new(instance)
15
- end
11
+ DEFAULT_NAME = 'bbk'
12
+ DEFAULT_LEVEL = Logger::Severity::DEBUG
16
13
 
17
- def initialize(progname, level, io: STDOUT)
18
- io.sync = true
19
- super(io)
20
- self.progname = progname
21
-
22
- if level.is_a?(Integer)
23
- self.level = level
24
- else
25
- level = level.to_s.upcase
26
- level = level.present? ? level : 'INFO'
27
- self.level = Logger.const_get(level)
14
+ def self.new(*args, **kwargs)
15
+ instance = super
16
+ ActiveSupport::TaggedLogging.new(instance)
28
17
  end
29
18
 
30
- self.formatter = LogFormatter.new
31
- info "Using LOG_LEVEL=#{level}"
32
- end
19
+ def initialize(progname, level, io: STDOUT)
20
+ io.sync = true
21
+ super(io)
22
+ self.progname = progname
23
+
24
+ if level.is_a?(Integer)
25
+ self.level = level
26
+ else
27
+ level = level.to_s.upcase
28
+ level = level.present? ? level : 'INFO'
29
+ self.level = Logger.const_get(level)
30
+ end
31
+
32
+ self.formatter = LogFormatter.new
33
+ info "Using LOG_LEVEL=#{level}"
34
+ end
33
35
 
34
- def silence(*_args)
35
- yield self
36
- end
36
+ def silence(*_args)
37
+ yield self
38
+ end
37
39
 
38
- def self.default
39
- if @default
40
- @default
41
- else
42
- @default = new(DEFAULT_NAME, DEFAULT_LEVEL)
43
- @default.level = DEFAULT_LEVEL
40
+ def self.default
41
+ unless @default
42
+ @default = new(DEFAULT_NAME, DEFAULT_LEVEL)
43
+ @default.level = DEFAULT_LEVEL
44
+ end
44
45
  @default
45
46
  end
47
+
46
48
  end
47
49
  end
48
50
  end
51
+
@@ -3,37 +3,42 @@
3
3
  require 'bbk/utils/logger'
4
4
 
5
5
  module BBK
6
- class ProxyLogger
7
- attr_reader :tags, :logger
6
+ module Utils
7
+ class ProxyLogger
8
8
 
9
- def initialize(logger, tags:)
10
- @logger = logger
11
- @tagged = @logger.respond_to?(:tagged)
12
- @tags = [tags].flatten
13
- end
9
+ attr_reader :tags, :logger
14
10
 
15
- def add_tags(*tags)
16
- @tags += tags.flatten
17
- @tags = @tags.uniq
18
- end
11
+ def initialize(logger, tags:)
12
+ @logger = logger
13
+ @tagged = @logger.respond_to?(:tagged)
14
+ @tags = [tags].flatten
15
+ end
19
16
 
20
- def method_missing(method, *args, &block)
21
- super unless logger.respond_to?(method)
17
+ def add_tags(*tags)
18
+ @tags += tags.flatten
19
+ @tags = @tags.uniq
20
+ end
21
+
22
+ def method_missing(method, *args, &block)
23
+ super unless logger.respond_to?(method)
22
24
 
23
- if @tagged
24
- current_tags = tags - logger.formatter.current_tags
25
- logger.tagged(current_tags) { logger.send(method, *args, &block) }
26
- else
27
- logger.send(method, *args, &block)
25
+ if @tagged
26
+ current_tags = tags - logger.formatter.current_tags
27
+ logger.tagged(current_tags) { logger.send(method, *args, &block) }
28
+ else
29
+ logger.send(method, *args, &block)
30
+ end
28
31
  end
29
- end
30
32
 
31
- def respond_to?(*args)
32
- logger.send(:respond_to?, *args) || super
33
- end
33
+ def respond_to?(name, include_private=false)
34
+ logger.send(:respond_to?, name, include_private) || super
35
+ end
36
+
37
+ def respond_to_missing?(method_name, include_private = false)
38
+ logger.send(:respond_to_missing?, method_name, include_private) || super
39
+ end
34
40
 
35
- def respond_to_missing?(method_name, include_private = false)
36
- logger.send(:respond_to_missing?, method_name, include_private) || super
37
41
  end
38
42
  end
39
43
  end
44
+
@@ -1,5 +1,10 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module BBK
2
4
  module Utils
3
- VERSION = '1.0.1'.freeze
5
+
6
+ VERSION = '1.0.1'
7
+
4
8
  end
5
9
  end
10
+
data/lib/bbk/utils/xml.rb CHANGED
@@ -1,30 +1,41 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'securerandom'
2
4
  require 'russian'
3
5
 
4
6
  module BBK
5
- module Xml
6
- MTOM_ID_FIRST_LETTERS = %w[a b c d e f].freeze
7
+ module Utils
8
+ module Xml
7
9
 
8
- ##
9
- # Generate identifier to future substitution in XML body. Ex.: real attachment identifier when uploading to FTP
10
- def self.build_substitution_id(id)
11
- "@{#{id}}"
12
- end
10
+ MTOM_ID_FIRST_LETTERS = %w[a b c d e f].freeze
13
11
 
14
- ##
15
- # Generate uuid compatible with SOAP AttachmentContent identifier
16
- def self.generate_mtom_attachment_id
17
- id = SecureRandom.uuid
18
- id[0] = MTOM_ID_FIRST_LETTERS.sample
19
- id
20
- end
12
+ ##
13
+ # Generate identifier to future substitution in XML body. Ex.: real attachment identifier when uploading to FTP
14
+ def self.build_substitution_id(id)
15
+ "@{#{id}}"
16
+ end
17
+
18
+ ##
19
+ # Generate uuid compatible with SOAP AttachmentContent identifier
20
+ def self.generate_mtom_attachment_id
21
+ id = SecureRandom.uuid
22
+ id[0] = MTOM_ID_FIRST_LETTERS.sample
23
+ id
24
+ end
25
+
26
+ ##
27
+ # Normalize XML href to be predictible and constant in various cases
28
+ def self.normalize_slug(name, href)
29
+ href_uri = URI.parse(href)
30
+ href_slug = [href_uri.scheme, href_uri.host, *href_uri.path.split('/'), href_uri.query].select do |item|
31
+ item.present?
32
+ end.join('-').gsub(
33
+ /[.&]/, '-'
34
+ )
35
+ Russian.translit "#{name}_#{href_slug}"
36
+ end
21
37
 
22
- ##
23
- # Normalize XML href to be predictible and constant in various cases
24
- def self.normalize_slug(name, href)
25
- href_uri = URI.parse(href)
26
- href_slug = [href_uri.scheme, href_uri.host, *href_uri.path.split('/'), href_uri.query].select(&:present?).join('-').gsub(/[\.&]/, '-')
27
- Russian.translit "#{name}_#{href_slug}"
28
38
  end
29
39
  end
30
40
  end
41
+
data/lib/bbk/utils.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'active_support'
2
4
  require 'active_support/core_ext'
3
5
  require 'bbk/utils/config'
@@ -10,11 +12,15 @@ require 'bbk/utils/env_helper'
10
12
 
11
13
  module BBK
12
14
  module Utils
15
+
13
16
  class << self
17
+
14
18
  attr_accessor :logger
15
19
 
16
20
  end
17
21
 
18
- self.logger = ::BBK::Logger.default
22
+ self.logger = ::BBK::Utils::Logger.default
23
+
19
24
  end
20
25
  end
26
+
@@ -0,0 +1,55 @@
1
+ module BBK
2
+ module Utils
3
+ class Config
4
+
5
+ class BooleanCaster
6
+ FALSE_VALUES: Array[bool | Integer | String | Symbol]
7
+ def self.cast: (untyped value) -> (bool|nil)
8
+ end
9
+
10
+ interface _CallableCaster
11
+ def call: (String) -> untyped
12
+ end
13
+
14
+ interface _ClassCaster
15
+ def new: (String) -> untyped
16
+ end
17
+
18
+ type typeCaster = _CallableCaster | _ClassCaster
19
+ type configItem = {env: String, file: String?, required: bool, desc: String?, bool: bool, value: untyped, default: untyped, type: typeCaster?}
20
+ type envSource = _ENV | ENVClass
21
+
22
+ attr_accessor store: untyped
23
+ attr_accessor name: String?
24
+
25
+ def self.instance: () -> instance
26
+
27
+ def initialize: (?String name) -> void
28
+ def map: (envSource env, String file, ?required: bool, ?desc: String, ?bool: bool, ?key: String) -> void
29
+ def require: (envSource env, ?desc: String?, ?bool: bool, ?type: typeCaster, ?key: String?) -> void
30
+ def optional: (envSource env, ?default: untyped, ?desc: String, ?bool: bool, ?type: typeCaster, ?key: String) -> void
31
+
32
+ def run!: (?envSource source) -> void
33
+ def []: (String key) -> untyped
34
+ def []=: (String key, untyped value) -> void
35
+ def content: (String key) -> untyped
36
+ def fetch: (String key, ?untyped default) -> untyped
37
+ # def to_s: () -> String
38
+ def as_json: (*untyped) -> Hash[String, untyped]
39
+ def to_json: (*untyped) -> String
40
+ def to_yaml: (*untyped) -> String
41
+
42
+ private
43
+
44
+ def normalize_key: (string key) -> String
45
+ def process: (envSource source, configItem item) -> void
46
+
47
+ def required!: (configItem item) -> void
48
+ def print_file_item: (configItem item, String padding) -> String
49
+ def print_item: (configItem item, String padding) -> String
50
+ def wrap_required: (configItem item) -> String
51
+
52
+
53
+ end
54
+ end
55
+ end
data/sig/bbk/crypt.rbs ADDED
@@ -0,0 +1,9 @@
1
+ module BBK
2
+ module Utils
3
+ module Crypt
4
+ def self.full_check: (String key_path, String cert_path, *String cacert_chain) -> Array[String]?
5
+ def self.valid_key: (String key_path, String cert_path) -> bool
6
+ def self.valid_cert_sign?: (String cert_path, *String ca_certs_paths) -> bool
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,13 @@
1
+ module BBK
2
+ module Utils
3
+ module EnvHelper
4
+
5
+ def self.prepare_database_envs: (Hash[String, untyped]|::_ENV env ) -> (Hash[String,untyped]|::_ENV)
6
+ def self.prepare_mq_envs: (Hash[String, untyped]|::_ENV env) -> (Hash[String, untyped]|::_ENV)
7
+ def self.build_uri_with_defaults: (Hash[String, untyped]|::_ENV env) -> URI::Generic
8
+ def self.apply_env_from_uri: (Hash[String, untyped]|::_ENV env, URI::Generic uri) -> void
9
+ def self.build_mq_uri_with_defaults: (Hash[String, untyped]|::_ENV env) -> Array[URI::Generic]
10
+ def self.apply_mq_env_from_uri: (Hash[String, untyped]|::_ENV env, Array[URI::Generic] uris) -> void
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,10 @@
1
+ module BBK
2
+ module Utils
3
+ class LogFormatter
4
+ FORMAT: String
5
+ def call: (String severity, Time time, String progname, untyped msg) -> String
6
+
7
+ def thread_id: () -> String
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,15 @@
1
+ module BBK
2
+ module Utils
3
+ class Logger < ::Logger
4
+
5
+ def self.new: (*untyped) -> _Logger
6
+
7
+ def initialize: (String progname, String|Symbol|Integer|nil level, ?io: ::Logger::logdev) -> void
8
+
9
+ def silence: (*untyped) { (self) -> untyped } -> untyped
10
+
11
+ def self.default: () -> instance
12
+
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,16 @@
1
+ module BBK
2
+ module Utils
3
+ class ProxyLogger
4
+
5
+ include _ProxyObject
6
+
7
+ attr_reader tags: Array[String]
8
+ attr_reader logger: BBK::Utils::Logger|::_Logger
9
+
10
+ def initialize: (BBK::Utils::Logger|::_Logger logger, tags: Array[String?]|String?) -> void
11
+
12
+ def add_tags: (*String tags) -> void
13
+
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,7 @@
1
+ module BBK
2
+ module Utils
3
+ interface _ProxyObject
4
+ def method_missing: (Symbol, *untyped) ?{(*untyped, **untyped) -> untyped} -> untyped
5
+ end
6
+ end
7
+ end
data/sig/bbk/utils.rbs ADDED
@@ -0,0 +1,5 @@
1
+ module BBK
2
+ module App
3
+ VERSION: String
4
+ end
5
+ end
data/sig/bbk/xml.rbs ADDED
@@ -0,0 +1,10 @@
1
+ module BBK
2
+ module Utils
3
+ module Xml
4
+ MTOM_ID_FIRST_LETTERS: Array[String]
5
+ def self.build_substituion_id: (string id) -> String
6
+ def self.generate_mtom_attachment_id: -> String
7
+ def self.normalize_slug: (String name, String href) -> String
8
+ end
9
+ end
10
+ end
data/sig/env.rbs ADDED
@@ -0,0 +1,4 @@
1
+ interface _ENV
2
+ def []: (String) -> String
3
+ def []=: (String) -> String
4
+ end
data/sig/logger.rbs ADDED
@@ -0,0 +1,37 @@
1
+ interface _Logger
2
+
3
+ def add: (untyped severity, ?untyped message, ?untyped progname) -> untyped
4
+
5
+ def close: () -> untyped
6
+
7
+ def debug: (?untyped progname) { (*untyped) -> untyped } -> untyped
8
+
9
+ def debug!: () -> untyped
10
+
11
+ def error: (?untyped progname) { (*untyped) -> untyped } -> untyped
12
+
13
+ def error!: () -> untyped
14
+
15
+ def fatal: (?untyped progname) { (*untyped) -> untyped } -> untyped
16
+
17
+ def fatal!: () -> untyped
18
+
19
+ def info: (?untyped progname) { (*untyped) -> untyped } -> untyped
20
+
21
+ def info!: () -> untyped
22
+
23
+ def info?: () -> untyped
24
+
25
+ def level: () -> untyped
26
+
27
+ def level=: (untyped severity) -> untyped
28
+
29
+ def progname: () -> untyped
30
+
31
+ def progname=: (untyped) -> untyped
32
+
33
+ def warn: (?untyped progname) { (*untyped) -> untyped } -> untyped
34
+
35
+ def warn!: () -> untyped
36
+
37
+ end