mimi-messaging 0.1.10

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,39 @@
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
@@ -0,0 +1,121 @@
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
@@ -0,0 +1,5 @@
1
+ module Mimi
2
+ module Messaging
3
+ VERSION = '0.1.10'.freeze
4
+ end
5
+ end
@@ -0,0 +1,6 @@
1
+ # Install console helpers
2
+ task :console_helpers do
3
+ require_relative 'console_helpers'
4
+ end
5
+
6
+ task console: :console_helpers
@@ -0,0 +1,116 @@
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
@@ -0,0 +1,39 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'mimi/messaging/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'mimi-messaging'
8
+ spec.version = Mimi::Messaging::VERSION
9
+ spec.authors = ['Alex Kukushkin']
10
+ spec.email = ['alex@kukushk.in']
11
+
12
+ spec.summary = 'Communications via RabbitMQ for mimi'
13
+ spec.description = 'Communications via RabbitMQ for mimi'
14
+ spec.homepage = 'https://github.com/kukushkin/mimi-messaging'
15
+ spec.license = 'MIT'
16
+
17
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
18
+ # delete this section to allow pushing this gem to any host.
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata['allowed_push_host'] = 'https://rubygems.org/'
21
+ else
22
+ raise 'RubyGems 2.0 or newer is required to protect against public gem pushes.'
23
+ end
24
+
25
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
+ spec.bindir = 'exe'
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ['lib']
29
+
30
+ spec.add_dependency 'bunny', '~> 2.9'
31
+ spec.add_dependency 'mimi-core', '~> 0.1'
32
+ spec.add_dependency 'mimi-logger', '~> 0.2', '>= 0.2.3'
33
+ spec.add_dependency 'msgpack', '~> 1.2'
34
+
35
+ spec.add_development_dependency 'bundler', '~> 1.11'
36
+ spec.add_development_dependency 'pry', '~> 0.10'
37
+ spec.add_development_dependency 'rake', '~> 10.0'
38
+ spec.add_development_dependency 'rspec', '~> 3.0'
39
+ end
metadata ADDED
@@ -0,0 +1,196 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mimi-messaging
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.10
5
+ platform: ruby
6
+ authors:
7
+ - Alex Kukushkin
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-05-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bunny
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.9'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.9'
27
+ - !ruby/object:Gem::Dependency
28
+ name: mimi-core
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.1'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.1'
41
+ - !ruby/object:Gem::Dependency
42
+ name: mimi-logger
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.2'
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: 0.2.3
51
+ type: :runtime
52
+ prerelease: false
53
+ version_requirements: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - "~>"
56
+ - !ruby/object:Gem::Version
57
+ version: '0.2'
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: 0.2.3
61
+ - !ruby/object:Gem::Dependency
62
+ name: msgpack
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '1.2'
68
+ type: :runtime
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: '1.2'
75
+ - !ruby/object:Gem::Dependency
76
+ name: bundler
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: '1.11'
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - "~>"
87
+ - !ruby/object:Gem::Version
88
+ version: '1.11'
89
+ - !ruby/object:Gem::Dependency
90
+ name: pry
91
+ requirement: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - "~>"
94
+ - !ruby/object:Gem::Version
95
+ version: '0.10'
96
+ type: :development
97
+ prerelease: false
98
+ version_requirements: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - "~>"
101
+ - !ruby/object:Gem::Version
102
+ version: '0.10'
103
+ - !ruby/object:Gem::Dependency
104
+ name: rake
105
+ requirement: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - "~>"
108
+ - !ruby/object:Gem::Version
109
+ version: '10.0'
110
+ type: :development
111
+ prerelease: false
112
+ version_requirements: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - "~>"
115
+ - !ruby/object:Gem::Version
116
+ version: '10.0'
117
+ - !ruby/object:Gem::Dependency
118
+ name: rspec
119
+ requirement: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - "~>"
122
+ - !ruby/object:Gem::Version
123
+ version: '3.0'
124
+ type: :development
125
+ prerelease: false
126
+ version_requirements: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - "~>"
129
+ - !ruby/object:Gem::Version
130
+ version: '3.0'
131
+ description: Communications via RabbitMQ for mimi
132
+ email:
133
+ - alex@kukushk.in
134
+ executables: []
135
+ extensions: []
136
+ extra_rdoc_files: []
137
+ files:
138
+ - ".gitignore"
139
+ - ".rspec"
140
+ - ".travis.yml"
141
+ - CODE_OF_CONDUCT.md
142
+ - Gemfile
143
+ - LICENSE.txt
144
+ - README.md
145
+ - Rakefile
146
+ - bin/console
147
+ - bin/setup
148
+ - lib/mimi/messaging.rb
149
+ - lib/mimi/messaging/connection.rb
150
+ - lib/mimi/messaging/errors.rb
151
+ - lib/mimi/messaging/listener.rb
152
+ - lib/mimi/messaging/message.rb
153
+ - lib/mimi/messaging/mock.rb
154
+ - lib/mimi/messaging/mock/connection.rb
155
+ - lib/mimi/messaging/mock/request.rb
156
+ - lib/mimi/messaging/mock/request_processor.rb
157
+ - lib/mimi/messaging/model.rb
158
+ - lib/mimi/messaging/model_provider.rb
159
+ - lib/mimi/messaging/msgpack/msgpack_ext.rb
160
+ - lib/mimi/messaging/msgpack/type_packer.rb
161
+ - lib/mimi/messaging/notification.rb
162
+ - lib/mimi/messaging/provider.rb
163
+ - lib/mimi/messaging/request.rb
164
+ - lib/mimi/messaging/request_processor.rb
165
+ - lib/mimi/messaging/request_processor/context.rb
166
+ - lib/mimi/messaging/request_processor/dsl.rb
167
+ - lib/mimi/messaging/version.rb
168
+ - lib/tasks/console_ext.rake
169
+ - lib/tasks/console_helpers.rb
170
+ - mimi-messaging.gemspec
171
+ homepage: https://github.com/kukushkin/mimi-messaging
172
+ licenses:
173
+ - MIT
174
+ metadata:
175
+ allowed_push_host: https://rubygems.org/
176
+ post_install_message:
177
+ rdoc_options: []
178
+ require_paths:
179
+ - lib
180
+ required_ruby_version: !ruby/object:Gem::Requirement
181
+ requirements:
182
+ - - ">="
183
+ - !ruby/object:Gem::Version
184
+ version: '0'
185
+ required_rubygems_version: !ruby/object:Gem::Requirement
186
+ requirements:
187
+ - - ">="
188
+ - !ruby/object:Gem::Version
189
+ version: '0'
190
+ requirements: []
191
+ rubyforge_project:
192
+ rubygems_version: 2.6.14.1
193
+ signing_key:
194
+ specification_version: 4
195
+ summary: Communications via RabbitMQ for mimi
196
+ test_files: []