mimi-messaging 0.1.12 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (50) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -0
  3. data/.rubocop.yml +66 -0
  4. data/README.md +68 -3
  5. data/TODO.md +8 -0
  6. data/docs/Messaging_Layer_Properties.md +141 -0
  7. data/docs/Why_HTTP_is_a_bad_choice.md +20 -0
  8. data/docs/diagrams/Pattern -- Command.drawio +1 -0
  9. data/docs/diagrams/Pattern -- Event direct.drawio +1 -0
  10. data/docs/diagrams/Pattern -- Event with Queue.drawio +1 -0
  11. data/docs/diagrams/Pattern -- Event.drawio +1 -0
  12. data/docs/diagrams/Pattern -- Query.drawio +1 -0
  13. data/docs/img/pattern--command.png +0 -0
  14. data/docs/img/pattern--event-direct.png +0 -0
  15. data/docs/img/pattern--event-using-queue.png +0 -0
  16. data/docs/img/pattern--event.png +0 -0
  17. data/docs/img/pattern--query.png +0 -0
  18. data/examples/basic_event_listener.rb +35 -0
  19. data/examples/basic_request_processor.rb +38 -0
  20. data/examples/using_messaging_low.rb +59 -0
  21. data/examples/using_pure_adapter.rb +62 -0
  22. data/lib/mimi/messaging.rb +428 -92
  23. data/lib/mimi/messaging/adapters.rb +22 -0
  24. data/lib/mimi/messaging/adapters/base.rb +233 -0
  25. data/lib/mimi/messaging/adapters/memory.rb +119 -0
  26. data/lib/mimi/messaging/adapters/test.rb +50 -0
  27. data/lib/mimi/messaging/errors.rb +24 -12
  28. data/lib/mimi/messaging/json_serializer.rb +45 -0
  29. data/lib/mimi/messaging/version.rb +3 -1
  30. data/mimi-messaging.gemspec +25 -23
  31. metadata +34 -78
  32. data/lib/mimi/messaging/connection.rb +0 -182
  33. data/lib/mimi/messaging/listener.rb +0 -72
  34. data/lib/mimi/messaging/message.rb +0 -74
  35. data/lib/mimi/messaging/mock.rb +0 -13
  36. data/lib/mimi/messaging/mock/connection.rb +0 -153
  37. data/lib/mimi/messaging/mock/request.rb +0 -19
  38. data/lib/mimi/messaging/mock/request_processor.rb +0 -92
  39. data/lib/mimi/messaging/model.rb +0 -27
  40. data/lib/mimi/messaging/model_provider.rb +0 -100
  41. data/lib/mimi/messaging/msgpack/msgpack_ext.rb +0 -14
  42. data/lib/mimi/messaging/msgpack/type_packer.rb +0 -104
  43. data/lib/mimi/messaging/notification.rb +0 -35
  44. data/lib/mimi/messaging/provider.rb +0 -48
  45. data/lib/mimi/messaging/request.rb +0 -56
  46. data/lib/mimi/messaging/request_processor.rb +0 -216
  47. data/lib/mimi/messaging/request_processor/context.rb +0 -39
  48. data/lib/mimi/messaging/request_processor/dsl.rb +0 -121
  49. data/lib/tasks/console_ext.rake +0 -6
  50. data/lib/tasks/console_helpers.rb +0 -116
@@ -1,39 +0,0 @@
1
- module Mimi
2
- module Messaging
3
- class RequestProcessor
4
- module Context
5
- attr_reader :self_before_instance_eval
6
-
7
- private
8
-
9
- #
10
- # Binds passed block and block parameters to the context
11
- #
12
- # @return [Proc] bound block
13
- #
14
- def __bind(*args, &block)
15
- proc { __execute(*args, &block) }
16
- end
17
-
18
- # Executes block within context
19
- #
20
- def __execute(*args, &block)
21
- @self_before_instance_eval ||= []
22
- block_self = eval 'self', block.binding
23
- @self_before_instance_eval.push(block_self)
24
- instance_exec(*args, &block)
25
- ensure
26
- @self_before_instance_eval.pop
27
- end
28
-
29
- def method_missing(method, *args, &block)
30
- if @self_before_instance_eval
31
- @self_before_instance_eval.last.send method, *args, &block
32
- else
33
- super
34
- end
35
- end
36
- end # module Context
37
- end # class RequestProcessor
38
- end # module Messaging
39
- end # module Mimi
@@ -1,121 +0,0 @@
1
- module Mimi
2
- module Messaging
3
- class RequestProcessor
4
- module DSL
5
- attr_accessor :parent
6
-
7
- # Returns the property of the parent class
8
- #
9
- def parent_property(*args)
10
- return nil unless @parent
11
- return nil unless @parent.respond_to?(args.first)
12
- @parent.send(*args)
13
- end
14
-
15
- # Sets queue name and options
16
- #
17
- def queue(name, options = {})
18
- queue_name name
19
- queue_options options
20
- true
21
- end
22
-
23
- # Sets or gets queue name
24
- #
25
- def queue_name(name = nil)
26
- raise "#{self} has already registered '#{@queue_name}' as queue name" if name && @queue_name
27
- (@queue_name ||= name) || default_queue_name
28
- end
29
-
30
- # Default (inferred) queue name
31
- #
32
- def default_queue_name
33
- nil
34
- end
35
-
36
- # Sets or gets queue options
37
- #
38
- def queue_options(opts = {})
39
- @queue_options ||= {}
40
- @queue_options = @queue_options.merge(opts.dup)
41
- (parent_property(:queue_options) || {}).merge(@queue_options)
42
- end
43
-
44
- # Sets provider options
45
- #
46
- def options(opts = {})
47
- @options ||= {}
48
- @options = @options.merge(opts.dup)
49
- (parent_property(:options) || {}).merge(@options)
50
- end
51
-
52
- def exposed_methods
53
- m = public_instance_methods(false)
54
- m += parent.exposed_methods if parent
55
- m
56
- end
57
-
58
- # Explicitly registers this request processor as abstract
59
- #
60
- def abstract!
61
- @abstract = true
62
- end
63
-
64
- # Is this provider abstract or configured to process requests?
65
- #
66
- def abstract?
67
- @abstract
68
- end
69
-
70
- #
71
- #
72
- def before(*args, &block)
73
- register_filter(:before, args, block)
74
- end
75
-
76
- #
77
- #
78
- def around(*args, &block)
79
- register_filter(:around, args, block)
80
- end
81
-
82
- #
83
- #
84
- def after(*args, &block)
85
- register_filter(:after, args, block)
86
- end
87
-
88
- #
89
- #
90
- def error(*args, &block)
91
- register_filter(:error, args, block)
92
- end
93
-
94
- def register_filter(type, args, block)
95
- @filters ||= { before: [], around: [], after: [], error: [] }
96
- @filters[type] << { args: args, block: block }
97
- end
98
-
99
- def filters(type, parent_first = true)
100
- @filters ||= { before: [], around: [], after: [], error: [] }
101
- if parent_first
102
- (parent_property(:filters, type) || []) + @filters[type]
103
- else
104
- @filters[type] + (parent_property(:filters, type, false) || [])
105
- end
106
- end
107
-
108
- # Converts class name to a resource name (camelize with dots).
109
- #
110
- # @example
111
- # "ModuleA::ClassB" #=> "module_a.class_b"
112
- #
113
- def class_name_to_resource_name(v, suffix = nil)
114
- v = v.to_s.gsub('::', '.').gsub(/([^\.])([A-Z])/, '\1_\2').downcase
115
- v = v.sub(/_?#{suffix}\z/, '') if suffix
116
- v
117
- end
118
- end # module DSL
119
- end # class RequestProcessor
120
- end # module Messaging
121
- end # module Mimi
@@ -1,6 +0,0 @@
1
- # Install console helpers
2
- task :console_helpers do
3
- require_relative 'console_helpers'
4
- end
5
-
6
- task console: :console_helpers
@@ -1,116 +0,0 @@
1
- # Mimi::Messaging development console helpers
2
- #
3
- # get 'provider.name/method' [{param1: ..., param2: ...}]
4
- # post 'provider.name/method' [{param1: ..., param2: ...}]
5
- # listen 'notification.name'
6
- # broadcast 'notification.name/method' [{param1: ..., param2: ...}]
7
- #
8
- def message_class_for(queue_name)
9
- message_class = Class.new(Mimi::Messaging::Message)
10
- message_class.queue(queue_name)
11
- message_class
12
- end
13
-
14
- def get_timeout(value = nil)
15
- return @get_current_timeout = value if value
16
- help_get_timeout
17
- puts
18
- puts "Curret GET timeout is set at #{get_current_timeout}s"
19
- end
20
-
21
- def help_get_timeout
22
- puts 'Usage: get_timeout <seconds>'
23
- puts
24
- puts 'Sets a timeout for GET requests.'
25
- puts
26
- puts 'Example:'
27
- puts " > get_timeout 15"
28
- end
29
-
30
- def get_current_timeout
31
- @get_current_timeout || 5 # seconds
32
- end
33
-
34
- def get(queue_and_method_name = nil, params = {})
35
- return help_get unless queue_and_method_name
36
- queue_name, method_name = queue_and_method_name.split('/')
37
- message_class = message_class_for(queue_name)
38
- puts "GET #{queue_name}/#{method_name}: #{params}"
39
- ts = Time.now
40
- result = message_class.get(method_name, params, timeout: get_current_timeout)
41
- puts 'Completed in %.1fms' % ((Time.now - ts) * 1000.0)
42
- result
43
- end
44
-
45
- def help_get
46
- puts "Usage: get '<queue_name>/<method_name>' [, params]"
47
- puts
48
- puts 'Issues a GET request to a given queue/method and sends the message'
49
- puts "constructed from params Hash. Awaits for a response for at most 'get_timeout' seconds."
50
- puts
51
- puts 'Example:'
52
- puts " > get 'accounts/list', page: 2"
53
- end
54
-
55
- def post(queue_and_method_name = nil, params = {})
56
- return help_post unless queue_and_method_name
57
- queue_name, method_name = queue_and_method_name.split('/')
58
- message_class = message_class_for(queue_name)
59
- puts "POST #{queue_name}/#{method_name}: #{params}"
60
- message_class.post(method_name, params)
61
- nil
62
- end
63
-
64
- def help_post
65
- puts "Usage: post '<queue_name>/<method_name>' [, params]"
66
- puts
67
- puts 'Issues a POST request to a given queue/method and sends the message'
68
- puts 'constructed from params Hash. Returns immediately'
69
- puts
70
- puts 'Example:'
71
- puts " > post 'accounts/create', name: 'Primary account', currency: 'BTC'"
72
- end
73
-
74
- def listen(notification_name = nil, *method_names)
75
- return help_listen unless notification_name
76
- listener_class = Class.new(Mimi::Messaging::Listener)
77
- listener_class.notification(notification_name)
78
- method_names.each do |method_name|
79
- listener_class.send :define_method, method_name.to_sym do
80
- puts "LISTEN #{listener_class.resource_name}/#{request.method_name}: #{params}"
81
- end
82
- puts "Listener for '#{notification_name}/#{method_name}' registered"
83
- end
84
- listener_class.start
85
- puts "Listener for '#{notification_name}' started"
86
- nil
87
- end
88
-
89
- def help_listen
90
- puts "Usage: listen '<notification_name>' [, <method_name> ...]"
91
- puts
92
- puts 'Sets up a listener for notifications specified by notification and method names.'
93
- puts
94
- puts 'Example:'
95
- puts " > listen 'accounts', :created, :updated"
96
- end
97
-
98
- def broadcast(notification_and_method_name = nil, params = {})
99
- return help_broadcast unless notification_and_method_name
100
- notification_name, method_name = notification_and_method_name.split('/')
101
- notification_class = Class.new(Mimi::Messaging::Notification)
102
- notification_class.notification(notification_name)
103
- puts "BROADCAST #{notification_name}/#{method_name}: #{params}"
104
- notification_class.broadcast(method_name, params)
105
- nil
106
- end
107
-
108
- def help_broadcast
109
- puts "Usage: broadcast '<notification_name>/<method_name>' [, params]"
110
- puts
111
- puts 'Broadcasts a notification message with a given name and method constructed'
112
- puts 'from params Hash.'
113
- puts
114
- puts 'Example:'
115
- puts " > broadcast 'accounts/updated', id: 213, name: 'Secondary account', currency: 'BCH'"
116
- end