kanal 0.4.1 → 0.4.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: 22224712e17434c60d3b437d44a35a5a997dd739cddcf53eb365d588fdf19fcd
4
- data.tar.gz: f6677a4a8bc6eacf2e41469ba4a36da1b324d3b7d9092ae9271aa1b9f3fa8be6
3
+ metadata.gz: 3a651f7ffee3a091afafa59e96d5c4e90d3f15ebd8fe5c0991ef0f3d25559cb5
4
+ data.tar.gz: 80a337f6047a542982f5167a1a15c73a9df90ffa95d6d24dc07bba881a4f575e
5
5
  SHA512:
6
- metadata.gz: 5e1416fb809513b53627cb120d8b09dfb76cae30f952de0212a1c2edde48e01d733afb122189230d0e6426dba812d143a8b2fc35b8561fba95954ea0b0bccbe2
7
- data.tar.gz: 3dcc90d49fa16f4df0482d77a94f3f21a7ddc12742983a8925714199100284bfb4d84a96bef19aa289c3ad1ebcc21996d3a27fb9874837b24ca343ba53f700d6
6
+ metadata.gz: 69b67d9f7886722abe0cc0b8ba27e30daecde925a275e3147b38c5b80bb1b9d041d1acdd33151be97c8e4a668888fbcf1819192dceb763e52104bfaa97a56ce4
7
+ data.tar.gz: fb12441c0c5797397cbc40e4517cc23c6a70c6604a0314961d4c9b203c02130464baefda7c6671cd31733112d4f131076de3d92482d695f10b7898c7b5c7b2c8
data/CHANGELOG.md CHANGED
@@ -1,5 +1,10 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.4.2] 2023-03-22
4
+ - Adding any number of loggers can be now done with core.add_logger(l) - where l is logger that have same methods as default ruby logger (debug, warn, fatal, etc)
5
+ - To get logger into your class you can include Kanal::Core::Logging::Logger - this will give you method .logger to get logger
6
+ - Batteries now have .keyboard output property and several conditions for input
7
+
3
8
  ## [0.4.1] 2023-03-16
4
9
  - Added Attachment that can be used inside new input parameters in Batteries plugin
5
10
  - New input and output parameters in Batteries plugin: image, audio, file
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- kanal (0.5.0)
4
+ kanal (0.4.2)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative "../logger/logging"
3
+ require_relative "../logging/logger"
4
4
 
5
5
  module Kanal
6
6
  module Core
@@ -8,7 +8,7 @@ module Kanal
8
8
  # Base class for conditions
9
9
  # with this class you can
10
10
  class Condition
11
- include Logging
11
+ include Logging::Logger
12
12
 
13
13
  attr_reader :name
14
14
 
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative "../logger/logging"
3
+ require_relative "../logging/logger"
4
4
 
5
5
  module Kanal
6
6
  module Core
@@ -8,7 +8,7 @@ module Kanal
8
8
  # This class helps creating conditions in dsl way,
9
9
  # with using helper methods
10
10
  class ConditionCreator
11
- include Logging
11
+ include Logging::Logger
12
12
 
13
13
  def initialize(name)
14
14
  @name = name
@@ -2,7 +2,7 @@
2
2
 
3
3
  require_relative "./condition"
4
4
  require_relative "./condition_creator"
5
- require_relative "../logger/logging"
5
+ require_relative "../logging/logger"
6
6
 
7
7
  module Kanal
8
8
  module Core
@@ -11,7 +11,7 @@ module Kanal
11
11
  # It is served as some kind of namespace for conditions, with specific
12
12
  # name of pack and helper methods
13
13
  class ConditionPack
14
- include Logging
14
+ include Logging::Logger
15
15
 
16
16
  attr_reader :name
17
17
 
@@ -2,7 +2,7 @@
2
2
 
3
3
  require_relative "./condition_pack"
4
4
  require_relative "./condition_creator"
5
- require_relative "../logger/logging"
5
+ require_relative "../logging/logger"
6
6
 
7
7
  module Kanal
8
8
  module Core
@@ -10,7 +10,7 @@ module Kanal
10
10
  # This class helps in condition pack creation
11
11
  # with the help of dsl
12
12
  class ConditionPackCreator
13
- include Logging
13
+ include Logging::Logger
14
14
 
15
15
  TEMP_NAME = :temp_name
16
16
 
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative "../logger/logging"
3
+ require_relative "../logging/logger"
4
4
 
5
5
  module Kanal
6
6
  module Core
@@ -8,7 +8,7 @@ module Kanal
8
8
  # This class contains all needed functionality to store,
9
9
  # search conditions
10
10
  class ConditionStorage
11
- include Logging
11
+ include Logging::Logger
12
12
 
13
13
  def initialize
14
14
  @condition_packs = []
@@ -8,8 +8,8 @@ require_relative "./helpers/parameter_registrator"
8
8
  require_relative "./plugins/plugin"
9
9
  require_relative "./input/input"
10
10
  require_relative "./services/service_container"
11
- require_relative "./logger/logging"
12
-
11
+ require_relative "./logging/logger"
12
+ require_relative "./logging/configuration"
13
13
 
14
14
  module Kanal
15
15
  module Core
@@ -36,6 +36,7 @@ module Kanal
36
36
  include Plugins
37
37
  include Hooks
38
38
  include Services
39
+ include Logging::Logger
39
40
  include Logging
40
41
 
41
42
  # @return [Kanal::Core::Conditions::ConditionStorage]
@@ -223,9 +224,14 @@ module Kanal
223
224
  def register_hooks
224
225
  @hooks.register :input_just_created # input
225
226
  @hooks.register :input_before_router # input
227
+ @hooks.register :output_just_created # input, output
226
228
  @hooks.register :output_before_returned # input, output
227
229
  end
228
230
 
231
+ def add_logger(logger)
232
+ Kanal::Core::Logging::Configuration.add_logger logger
233
+ end
234
+
229
235
  private :register_hooks
230
236
  end
231
237
  end
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "parameter_bag"
4
- require_relative "../logger/logging"
4
+ require_relative "../logging/logger"
5
5
 
6
6
  module Kanal
7
7
  module Core
@@ -10,7 +10,7 @@ module Kanal
10
10
  # and if they are has needed by registrator allowances, types etc, whatever
11
11
  # registrator rules are stored for property
12
12
  class ParameterBagWithRegistrator < ParameterBag
13
- include Logging
13
+ include Logging::Logger
14
14
 
15
15
  def initialize(registrator)
16
16
  super()
@@ -23,7 +23,7 @@ module Kanal
23
23
  # like that
24
24
  # setters: prop value
25
25
  # getters: prop
26
- @parameter_bag.set(parameter_name, *args)
26
+ @parameter_bag.set parameter_name, args.first
27
27
  # means it is used as setter in dsl,
28
28
  # method call with argument
29
29
  else
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative "../logger/logging"
3
+ require_relative "../logging/logger"
4
4
 
5
5
  module Kanal
6
6
  module Core
@@ -21,7 +21,7 @@ module Kanal
21
21
  # Class holds parameter names that are allowed
22
22
  # to be used.
23
23
  class ParameterRegistrator
24
- include Logging
24
+ include Logging::Logger
25
25
 
26
26
  def initialize
27
27
  @parameters_by_name = {}
@@ -1,14 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "../output/output"
4
- require_relative "../logger/logging"
4
+ require_relative "../logging/logger"
5
5
 
6
6
  module Kanal
7
7
  module Core
8
8
  module Helpers
9
9
  class ResponseExecutionBlock
10
10
  include Output
11
- include Logging
11
+ include Logging::Logger
12
12
 
13
13
  attr_reader :response_block, :input
14
14
 
@@ -40,6 +40,8 @@ module Kanal
40
40
  output = Output::Output.new core.output_parameter_registrator, input, core
41
41
 
42
42
  begin
43
+ core.hooks.call :output_just_created, input, output
44
+
43
45
  output.instance_eval(&@response_block.block)
44
46
 
45
47
  core.hooks.call :output_before_returned, input, output
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative "../logger/logging"
3
+ require_relative "../logging/logger"
4
4
 
5
5
  module Kanal
6
6
  module Core
@@ -10,7 +10,7 @@ module Kanal
10
10
  # attaching to hooks, calling hooks with arguments
11
11
  #
12
12
  class HookStorage
13
- include Logging
13
+ include Logging::Logger
14
14
 
15
15
  def initialize
16
16
  @listeners = {}
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kanal
4
+ module Core
5
+ module Logging
6
+ class CompositeLogger
7
+ # Property allowed_methods defines which methods can be called.
8
+ # These methods mirror standard Logger methods.
9
+ def initialize
10
+ @loggers = []
11
+ @allowed_methods = [:debug, :info, :warn, :error, :fatal, :unknown]
12
+ end
13
+
14
+ def add_logger(logger)
15
+ @loggers << logger
16
+ end
17
+
18
+ # This method gets called when calling any method in @allowed_methods.
19
+ # It passes message string to method call of every added logger.
20
+ #
21
+ # @param method [Symbol]
22
+ # @param message [String]
23
+ def method_missing(method, message)
24
+ # Getting caller class name to put into log message later.
25
+ begin
26
+ caller_class_name = "[" + caller[0].split(".rb").first.split("/").last.split('_').collect(&:capitalize).join + "]"
27
+ rescue
28
+ caller_class_name = "[ErrorGettingClassName]"
29
+ end
30
+
31
+ raise "Wrong method called for CompositeLogger" unless @allowed_methods.include? method
32
+
33
+ @loggers.each do |logger|
34
+ # progname is a property of standard Logger. It is a prefix for log message.
35
+ # In our case progname is used to store class name of class which calls for CompositeLogger methods.
36
+ logger.progname = caller_class_name
37
+ logger.send method, message
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
44
+
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "composite_logger"
4
+
5
+ module Kanal
6
+ module Core
7
+ module Logging
8
+ module Configuration
9
+ class << self
10
+ def composite_logger
11
+ get_or_create_composite_logger
12
+ end
13
+
14
+ def add_logger(logger)
15
+ @composite_logger.add_logger logger
16
+ end
17
+
18
+ private
19
+
20
+ def get_or_create_composite_logger
21
+ @composite_logger ||= CompositeLogger.new
22
+
23
+ @composite_logger
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
30
+
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "logger"
4
+
5
+ module Kanal
6
+ module Core
7
+ module Logging
8
+ # Mixing in Logger in some class to get access to logger method
9
+ module Logger
10
+ # @return [CompositeLogger]
11
+ def logger
12
+ Kanal::Core::Logging::Configuration.composite_logger
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -3,7 +3,7 @@
3
3
  require_relative "./router_node"
4
4
  require_relative "../helpers/queue"
5
5
  require_relative "../helpers/response_execution_block"
6
- require_relative "../logger/logging"
6
+ require_relative "../logging/composite_logger"
7
7
 
8
8
  module Kanal
9
9
  module Core
@@ -14,7 +14,7 @@ module Kanal
14
14
  # router nodes and have a name.
15
15
  class Router
16
16
  include Helpers
17
- include Logging
17
+ include Logging::Logger
18
18
 
19
19
  attr_reader :name, :core, :output_ready_block
20
20
 
@@ -2,7 +2,7 @@
2
2
 
3
3
  require_relative "../output/output"
4
4
  require_relative "../helpers/response_block"
5
- require_relative "../logger/logging"
5
+ require_relative "../logging/composite_logger"
6
6
 
7
7
  module Kanal
8
8
  module Core
@@ -14,7 +14,7 @@ module Kanal
14
14
  class RouterNode
15
15
  include Output
16
16
  include Helpers
17
- include Logging
17
+ include Logging::Logger
18
18
 
19
19
  attr_reader :parent,
20
20
  :children
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative "../logger/logging"
3
+ require_relative "../logging/logger"
4
4
 
5
5
  module Kanal
6
6
  module Core
@@ -28,7 +28,7 @@ module Kanal
28
28
  # lifespan types.
29
29
  #
30
30
  class ServiceContainer
31
- include Logging
31
+ include Logging::Logger
32
32
 
33
33
  TYPE_SINGLETON = :singleton
34
34
  TYPE_TRANSIENT = :transient
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "kanal/core/plugins/plugin"
4
+ require_relative "keyboard"
4
5
 
5
6
  module Kanal
6
7
  module Plugins
@@ -17,7 +18,7 @@ module Kanal
17
18
  body_batteries core
18
19
  flow_batteries core
19
20
  attachments_batteries core
20
- reply_markup_batteries core
21
+ keyboard_batteries core
21
22
  end
22
23
 
23
24
  def flow_batteries(core)
@@ -123,8 +124,12 @@ module Kanal
123
124
  core.register_output_parameter :file
124
125
  end
125
126
 
126
- def reply_markup_batteries(core)
127
- core.register_output_parameter :reply_markup
127
+ def keyboard_batteries(core)
128
+ core.register_output_parameter :keyboard
129
+
130
+ core.hooks.attach :output_just_created do |input, output|
131
+ output.keyboard = Keyboard.new
132
+ end
128
133
  end
129
134
  end
130
135
  end
@@ -0,0 +1,98 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "kanal/core/logging/composite_logger"
4
+
5
+ module Kanal
6
+ module Plugins
7
+ module Batteries
8
+ #
9
+ # This class provides methods to construct and get keyboard.
10
+ # You can construct keyboard inside respond do block in router.
11
+ # There are two ways of constructing a keyboard
12
+ # 1. Using #to_array method: provide 2-dimensional array of button names
13
+ # 2. Using #build method: provide block with calls to DSL method #row with button names as arguments
14
+ #
15
+ class Keyboard
16
+ include Kanal::Core::Logging
17
+
18
+ def initialize
19
+ @layout = []
20
+ end
21
+
22
+ # Use this method to pass names for buttons in 2-dimensional array of strings.
23
+ # Each sub-array represents a row of button names.
24
+ # Example call below is two rows of button names:
25
+ # keyboard.from_array [["B1", "B2"], ["B3"]]
26
+ #
27
+ # @param array [Array<Array<String>>]
28
+ #
29
+ def from_array(array)
30
+ unless array.instance_of? Array
31
+ logger.error "2-dimensional array was not provided. Type of provided object is #{array.class.name}"
32
+
33
+ raise "2-dimensional array was not provided"
34
+ end
35
+
36
+ array.each do |sub_array|
37
+ unless sub_array.instance_of? Array
38
+ logger.error "Value provided as row is not of Array type. Type of provided object is #{sub_array.class.name}"
39
+
40
+ raise "Value provided as row is not of Array type"
41
+ end
42
+
43
+ sub_array.each do |value|
44
+ unless value.instance_of? String
45
+ logger.error "Value provided as button name is not of String type. Type of provided object is #{sub_array.class.name}"
46
+
47
+ raise "Value provided as button name is not of String type"
48
+ end
49
+ end
50
+ end
51
+
52
+ @layout = array
53
+ end
54
+
55
+ #
56
+ # Use this method to pass block. Code inside block should call for DSL method row.
57
+ # Example below is two rows of button names
58
+ # keyboard.build do
59
+ # row "B", "B2"
60
+ # row "B3"
61
+ # end
62
+ #
63
+ # @param block [Proc]
64
+ #
65
+ def build(&block)
66
+ instance_eval &block
67
+ end
68
+
69
+ #
70
+ # Creates a row of button names, takes each provided arg and adds it to a row
71
+ #
72
+ # @param args [Array<String>]
73
+ #
74
+ def row(*args)
75
+ args.each do |value|
76
+ unless value.instance_of? String
77
+ logger.error "Value provided as button name is not of String type. Type of provided object is #{sub_array.class.name}"
78
+
79
+ raise "Value provided as button name is not of String type"
80
+ end
81
+ end
82
+
83
+ @layout.append args
84
+ end
85
+
86
+ #
87
+ # Returns constructed 2-dimensional array of strings.
88
+ # Each sub-array represents a row of button names.
89
+ #
90
+ # @return [Array<Array<String>>]
91
+ #
92
+ def to_a
93
+ @layout
94
+ end
95
+ end
96
+ end
97
+ end
98
+ end
data/lib/kanal/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Kanal
4
- VERSION = "0.4.1"
4
+ VERSION = "0.4.2"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kanal
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.4.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - idchlife
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-03-16 00:00:00.000000000 Z
11
+ date: 2023-03-22 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Thanks to the core library, ecosystem of Kanal tools can be extendted
14
14
  to use with input-output bot-like behaviour, with routing
@@ -29,7 +29,6 @@ files:
29
29
  - LICENSE.txt
30
30
  - README.md
31
31
  - Rakefile
32
- - kanal.gemspec
33
32
  - lib/kanal.rb
34
33
  - lib/kanal/core/conditions/condition.rb
35
34
  - lib/kanal/core/conditions/condition_creator.rb
@@ -49,7 +48,9 @@ files:
49
48
  - lib/kanal/core/hooks/hook_storage.rb
50
49
  - lib/kanal/core/input/input.rb
51
50
  - lib/kanal/core/interfaces/interface.rb
52
- - lib/kanal/core/logger/logging.rb
51
+ - lib/kanal/core/logging/composite_logger.rb
52
+ - lib/kanal/core/logging/configuration.rb
53
+ - lib/kanal/core/logging/logger.rb
53
54
  - lib/kanal/core/output/output.rb
54
55
  - lib/kanal/core/output/output_creator.rb
55
56
  - lib/kanal/core/plugins/plugin.rb
@@ -60,6 +61,7 @@ files:
60
61
  - lib/kanal/interfaces/simple_cli/simple_cli_interface.rb
61
62
  - lib/kanal/plugins/batteries/attachments/attachment.rb
62
63
  - lib/kanal/plugins/batteries/batteries_plugin.rb
64
+ - lib/kanal/plugins/batteries/keyboard.rb
63
65
  - lib/kanal/version.rb
64
66
  - sig/kanal.rbs
65
67
  - sig/kanal/core/conditions/condition_pack.rbs
@@ -73,7 +75,7 @@ metadata:
73
75
  source_code_uri: https://github.com/idchlife/kanal
74
76
  changelog_uri: https://github.com/idchlife/kanal/CHANGELOG.md
75
77
  rubygems_mfa_required: 'true'
76
- post_install_message:
78
+ post_install_message:
77
79
  rdoc_options: []
78
80
  require_paths:
79
81
  - lib
@@ -89,7 +91,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
89
91
  version: '0'
90
92
  requirements: []
91
93
  rubygems_version: 3.1.6
92
- signing_key:
94
+ signing_key:
93
95
  specification_version: 4
94
96
  summary: Kanal library
95
97
  test_files: []
data/kanal.gemspec DELETED
@@ -1,40 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative "lib/kanal/version"
4
-
5
- Gem::Specification.new do |spec|
6
- spec.name = "kanal"
7
- spec.version = Kanal::VERSION
8
- spec.authors = ["idchlife"]
9
- spec.email = ["idchlife@gmail.com"]
10
-
11
- spec.summary = "Kanal library"
12
- spec.description = "Thanks to the core library, ecosystem of Kanal tools can be extendted to use with input-output bot-like behaviour, with routing"
13
- spec.homepage = "https://idchlife.github.io/kanal-documentation/"
14
- spec.license = "MIT"
15
- spec.required_ruby_version = ">= 2.7.6"
16
-
17
- spec.metadata["allowed_push_host"] = "https://rubygems.org"
18
-
19
- spec.metadata["homepage_uri"] = spec.homepage
20
- spec.metadata["source_code_uri"] = "https://github.com/idchlife/kanal"
21
- spec.metadata["changelog_uri"] = "https://github.com/idchlife/kanal/CHANGELOG.md"
22
-
23
- # Specify which files should be added to the gem when it is released.
24
- # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
25
- spec.files = Dir.chdir(__dir__) do
26
- `git ls-files -z`.split("\x0").reject do |f|
27
- (f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
28
- end
29
- end
30
- spec.bindir = "exe"
31
- spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
32
- spec.require_paths = ["lib"]
33
-
34
- # Uncomment to register a new dependency of your gem
35
- # spec.add_dependency "example-gem", "~> 1.0"
36
-
37
- # For more information and examples about making a new gem, check out our
38
- # guide at: https://bundler.io/guides/creating_gem.html
39
- spec.metadata["rubygems_mfa_required"] = "true"
40
- end
@@ -1,26 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "logger"
4
-
5
- module Kanal
6
- module Core
7
- module Logging
8
- # Logger instance will be saved inside class itself for future calls
9
- @logger_instance
10
-
11
- # Mixing in Logger in some class adds possibility to use logger instance method
12
- def logger
13
- @logger_instance ||= Logging.create_logger self.class.name
14
- end
15
-
16
- class << self
17
- def create_logger(class_name)
18
- logger = Logger.new STDOUT
19
- logger.progname = class_name.rpartition(':').last
20
- logger.datetime_format = "%d-%m-%Y %H:%M:%S"
21
- logger
22
- end
23
- end
24
- end
25
- end
26
- end