ruby_rabbitmq_janus 0.3.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (66) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +64 -7
  3. data/.reek +7 -0
  4. data/.rubocop.yml +0 -3
  5. data/README.md +40 -2
  6. data/config/default.md +22 -6
  7. data/config/default.yml +24 -6
  8. data/config/requests/{test.json → admin/handle_info.json} +2 -4
  9. data/config/requests/admin/handles.json +6 -0
  10. data/config/requests/admin/log_level.json +6 -0
  11. data/config/requests/admin/sessions.json +5 -0
  12. data/config/requests/{attach.json → base/attach.json} +0 -0
  13. data/config/requests/{create.json → base/create.json} +0 -0
  14. data/config/requests/{destroy.json → base/destroy.json} +0 -0
  15. data/config/requests/{detach.json → base/detach.json} +0 -0
  16. data/config/requests/{info.json → base/info.json} +0 -0
  17. data/config/requests/base/keepalive.json +5 -0
  18. data/lib/generators/ruby_rabbitmq_janus/configuration_generator.rb +23 -0
  19. data/lib/generators/ruby_rabbitmq_janus/create_request_generator.rb +90 -0
  20. data/lib/generators/ruby_rabbitmq_janus/default_request_generator.rb +21 -0
  21. data/lib/generators/ruby_rabbitmq_janus/initializer_generator.rb +17 -0
  22. data/lib/rrj/errors/config.rb +21 -0
  23. data/lib/rrj/errors/error.rb +56 -0
  24. data/lib/rrj/errors/janus.rb +14 -0
  25. data/lib/rrj/errors/janus_message.rb +45 -0
  26. data/lib/rrj/errors/janus_response.rb +78 -0
  27. data/lib/rrj/errors/janus_transaction.rb +31 -0
  28. data/lib/rrj/errors/rabbit.rb +21 -0
  29. data/lib/rrj/errors/request.rb +21 -0
  30. data/lib/rrj/info.rb +40 -0
  31. data/lib/rrj/init.rb +113 -44
  32. data/lib/rrj/janus/admin.rb +27 -0
  33. data/lib/rrj/janus/janus.rb +10 -33
  34. data/lib/rrj/janus/keepalive.rb +69 -0
  35. data/lib/rrj/janus/message.rb +77 -22
  36. data/lib/rrj/janus/response.rb +79 -41
  37. data/lib/rrj/janus/transaction.rb +53 -0
  38. data/lib/rrj/janus/transaction_admin.rb +43 -0
  39. data/lib/rrj/janus/transaction_handle.rb +40 -0
  40. data/lib/rrj/janus/transaction_session.rb +17 -0
  41. data/lib/rrj/rabbit/connect.rb +75 -0
  42. data/lib/rrj/rabbit/propertie.rb +37 -0
  43. data/lib/rrj/rabbit/publish.rb +100 -0
  44. data/lib/rrj/rabbit/rabbit.rb +11 -0
  45. data/lib/rrj/tools/config.rb +63 -0
  46. data/lib/rrj/tools/env.rb +21 -0
  47. data/lib/rrj/tools/log.rb +106 -0
  48. data/lib/rrj/tools/replaces.rb +115 -0
  49. data/lib/rrj/tools/replaces_admin.rb +44 -0
  50. data/lib/rrj/tools/requests.rb +57 -0
  51. data/lib/rrj/tools/tools.rb +14 -0
  52. data/lib/ruby_rabbitmq_janus.rb +8 -13
  53. data/rrj.gemspec +7 -3
  54. metadata +108 -28
  55. data/config.reek +0 -3
  56. data/lib/rrj/config.rb +0 -57
  57. data/lib/rrj/janus/error.rb +0 -8
  58. data/lib/rrj/janus/message_async.rb +0 -54
  59. data/lib/rrj/janus/message_sync.rb +0 -34
  60. data/lib/rrj/log.rb +0 -63
  61. data/lib/rrj/rabbitmq/rabbitmq.rb +0 -82
  62. data/lib/rrj/request/path.rb +0 -21
  63. data/lib/rrj/request/replaces.rb +0 -109
  64. data/lib/rrj/request/requests.rb +0 -55
  65. data/lib/rrj/request/type_data.rb +0 -30
  66. data/lib/rrj/version.rb +0 -17
@@ -1,54 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module RubyRabbitmqJanus
4
- # @author VAILLANT Jeremy <jeremy.vaillant@dazzl.tv>
5
- # Message Janus sending to rabbitmq server
6
- class ASync < MessageJanus
7
- def initialize(opts_request, channel)
8
- super
9
- @response = nil
10
- @condition = ConditionVariable.new
11
- @lock = Mutex.new
12
- @reply = channel.queue('', exclusive: true)
13
- end
14
-
15
- # Send a message to RabbitMQ server
16
- # @param json [String] Name of request used
17
- # @param channel [Bunny::Channel] Channel used in transaction
18
- # @param queue_to [String] Name of queue used for sending request in RabbitMQ
19
- # @return [Hash] Result to request executed
20
- def send(json)
21
- Log.instance.debug 'Send a message ASYNCHRONE'
22
- subscribe
23
- channel_publish_message(json)
24
- end
25
-
26
- private
27
-
28
- attr_reader :lock, :response
29
-
30
- def channel_publish_message(json)
31
- message = channel.default_exchange
32
- message.publish(define_request_sending(json),
33
- reply_to: @reply.name,
34
- routing_key: Config.instance.options['queues']['queue_to'],
35
- correlation_id: correlation,
36
- content_type: 'application/json')
37
- @lock.synchronize { @condition.wait(lock) }
38
- @response
39
- end
40
-
41
- def subscribe
42
- @reply.subscribe do |_delivery_info, properties, payload|
43
- parse_queue properties, payload
44
- end
45
- end
46
-
47
- def parse_queue(properties, payload)
48
- if properties[:correlation_id] == correlation
49
- @response = JSON.parse payload
50
- @lock.synchronize { @condition.signal }
51
- end
52
- end
53
- end
54
- end
@@ -1,34 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module RubyRabbitmqJanus
4
- # @author VAILLANT Jeremy <jeremy.vaillant@dazzl.tv>
5
- # Message Janus sending to rabbitmq server
6
- class Sync < MessageJanus
7
- def initialize(opts_request, channel)
8
- super
9
- end
10
-
11
- # Send a message to RabbitMQ server
12
- # @param json [String] Name of request used
13
- # @param channel [Bunny::Channel] Channel used in transaction
14
- # @param queue_to [String] Name of queue used for sending request in RabbitMQ
15
- # @return [Hash] Result to request executed
16
- def send(json)
17
- message = channel.default_exchange
18
- Log.instance.debug 'Send a message SYNCHRONE'
19
- message.publish(define_request_sending(json),
20
- routing_key: Config.instance.options['queues']['queue_to'],
21
- correlation_id: correlation,
22
- content_type: 'application/json')
23
- return_info_message
24
- end
25
-
26
- private
27
-
28
- # Prepare an Hash with information necessary to read a response in RabbitMQ queue
29
- def return_info_message
30
- my_request['properties'] = { 'correlation' => correlation }
31
- my_request
32
- end
33
- end
34
- end
data/lib/rrj/log.rb DELETED
@@ -1,63 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'logging'
4
-
5
- module RubyRabbitmqJanus
6
- # @author VAILLANT Jeremy <jeremy.vaillant@dazzl.tv>
7
- # Class for wrtting logs. Define level used (:warn, :info, :debug).
8
- class Log
9
- include Singleton
10
-
11
- # Define folder to writing logs
12
- DEFAULT_LOG_DIR = 'log'
13
-
14
- # Define file name to writing logs
15
- DEFAULT_LOG_NAME = 'rails-rabbit-janus.log'
16
-
17
- # Define a default level to gem (:warn, :info, :debug)
18
- DEFAULT_LEVEL = :debug
19
-
20
- attr_reader :log_file
21
-
22
- # Returns a new instance to Log
23
- def initialize
24
- @logs = Logging.logger[Time.now.to_s + ' --']
25
- @logs.level = DEFAULT_LEVEL
26
- @log_file = DEFAULT_LOG_DIR + '/' + DEFAULT_LOG_NAME
27
- @logs.add_appenders Logging.appenders.file(@log_file)
28
-
29
- welcome = '### Start gem Rails Rabbit Janus ###'
30
- @logs.info(welcome)
31
- end
32
-
33
- # Level to log
34
- # @return [Integer] The level to log instance
35
- def level
36
- @logs.level
37
- end
38
-
39
- # Save level to log
40
- # @param level [Symbol] Save log level used by this gem
41
- def level=(level)
42
- @logs.level = level
43
- end
44
-
45
- # Write a message in log with a warn level
46
- # @param message [String] Message writing in warning level in log
47
- def warn(message)
48
- @logs.warn(message)
49
- end
50
-
51
- # Write a message in log with a info level
52
- # @param message [String] Message writing in info level in log
53
- def info(message)
54
- @logs.info(message)
55
- end
56
-
57
- # Write a message in log with a debug level
58
- # @param message [String] Message writing in debug level in log
59
- def debug(message)
60
- @logs.debug(message)
61
- end
62
- end
63
- end
@@ -1,82 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'bunny'
4
-
5
- module RubyRabbitmqJanus
6
- # @author VAILLANT Jeremy <jeremy.vaillant@dazzl.tv>
7
- # Class for connection with RabbitMQ Server
8
- class RabbitMQ
9
- # Return a new instance to RabbitMQ
10
- def initialize
11
- @connection = Bunny.new(read_options_server)
12
- @janus, @response = nil
13
- end
14
-
15
- # Connect to server RabbitMQ and post a message in queue ('to-janus' by default)
16
- def ask_request_sync(request_type, opts)
17
- execute_request do
18
- rqt = Requests.instance.requests[request_type.to_s]
19
- @response = @janus.send(rqt, opts)
20
- close_server_rabbitmq
21
- end
22
- end
23
-
24
- def ask_request_async(request_type, opts)
25
- execute_request do
26
- rqt = Requests.instance.requests[request_type.to_s]
27
- @response = @janus.send_async(rqt, opts)
28
- close_server_rabbitmq
29
- end
30
- end
31
-
32
- # Connect to server RabbitMQ and read a message in queue ('from-janus' by default)
33
- def ask_response(info_request)
34
- execute_request do
35
- @response = @janus.read(info_request, @connection)
36
- end
37
- end
38
-
39
- private
40
-
41
- # Establish connection with RabbitMQ server
42
- # @return [RRJ::Janus] Janus object for manipulating data sending and receiving to
43
- # rabbitmq server
44
- def open_server_rabbitmq
45
- @connection.start
46
- end
47
-
48
- # Close connection to rabbitmq server
49
- def close_server_rabbitmq
50
- @connection.close
51
- end
52
-
53
- # Use configuration information to connect RabbitMQ
54
- # This method smells of :reek:DuplicateMethodCall
55
- # This method smells of :reek:TooManyStatements
56
- # This method smells of :reek:FeatureEnvy
57
- def read_options_server
58
- hash = {}
59
- hash.merge!(define_options)
60
- hash['log_level'] = Log.instance.level
61
- hash['log_file'] = Log.instance.log_file
62
- hash
63
- end
64
-
65
- # This method smells of :reek:UtilityFunction
66
- def define_options
67
- hash = {}
68
- Config.instance.options.fetch('server').each do |key, server|
69
- hash[key.to_sym] = server.to_s
70
- end
71
- hash
72
- end
73
-
74
- # Execute request
75
- def execute_request
76
- open_server_rabbitmq
77
- @janus = Janus.new(@connection)
78
- yield
79
- @response
80
- end
81
- end
82
- end
@@ -1,21 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'key_path'
4
-
5
- module RubyRabbitmqJanus
6
- # aze
7
- # @author VAILLANT Jeremy <jeremy.vaillant@dazzl.tv>
8
- class Path
9
- attr_reader :path, :parent, :child, :value
10
-
11
- def initialize(key)
12
- @path = KeyPath::Path.new key
13
- @parent = @path.parent.to_s
14
- @child = @path.to_s.gsub(@parent + '.', '')
15
- end
16
-
17
- def path
18
- @path.to_s
19
- end
20
- end
21
- end
@@ -1,109 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'json'
4
- require 'key_path'
5
-
6
- module RubyRabbitmqJanus
7
- # Format message request with good data
8
- # @author VAILLANT Jeremy <jeremy.vaillant@dazzl.tv>
9
- class Replace
10
- def initialize(json_file, opts)
11
- @request = JSON.parse(File.read(json_file))
12
- @opts = opts
13
- @path = nil
14
- end
15
-
16
- def to_hash
17
- Hash @request
18
- end
19
-
20
- def to_json
21
- replaces
22
- @request.to_json
23
- end
24
-
25
- private
26
-
27
- # Replace element in json request with information used by transaction
28
- def replaces
29
- create_transaction
30
- return unless @opts
31
- replace_standard_elements
32
- replace_specific_elements if @opts.key?(:other_key) && @request.key?('body')
33
- end
34
-
35
- # Create an transaction string and replace in request field with an String format
36
- def create_transaction
37
- @request['transaction'] = [*('A'..'Z'), *('0'..'9')].sample(10).join
38
- end
39
-
40
- def replace_standard_elements
41
- replace_element('session_id')
42
- replace_plugin
43
- replace_element(@opts['handle_id'] ? 'handle_id' : 'sender', 'handle_id')
44
- end
45
-
46
- def replace_specific_elements
47
- if request_as_replace_specific
48
- new_hash = rewrite_key_to_string(@opts[:other_key])
49
- running_hash(new_hash)
50
- end
51
- end
52
-
53
- # Replace a session_id field with an Integer
54
- def replace_element(value, value_replace = value)
55
- add_return(value_replace, value_data_or_precise(value)) \
56
- if @request[value_replace]
57
- end
58
-
59
- # Format the response return
60
- def value_data_or_precise(key)
61
- @opts[key] || @opts['data']['id']
62
- end
63
-
64
- # Format the json used for request sending
65
- def add_return(key, value)
66
- @request[key] = value
67
- @request.merge!(key => value)
68
- end
69
-
70
- # Replace plugin used for transaction
71
- def replace_plugin
72
- my_plugin = @request['plugin']
73
- if my_plugin
74
- number = TypeData.new(my_plugin)
75
- add_return('plugin', Config.instance.options['janus']['plugins'][number.format])
76
- end
77
- end
78
-
79
- # Rewrite key symbol to string
80
- def rewrite_key_to_string(node)
81
- Hash[
82
- node.map do |key, value|
83
- [key.to_s, value.is_a?(Hash) ? rewrite_key_to_string(value) : value]
84
- end
85
- ]
86
- end
87
-
88
- def running_hash(hash, parent = '')
89
- hash.each do |key, value|
90
- if value.is_a?(Hash)
91
- running_hash(value, new_parent(key, parent))
92
- else
93
- @request[parent][key] = value
94
- end
95
- end
96
- end
97
-
98
- def request_as_replace_specific
99
- ['<string>', '<number>'].each do |value|
100
- return true if @request['body'].value?(value)
101
- end
102
- end
103
-
104
- # This is method smells of :reek:UtilityFunction
105
- def new_parent(key, parent)
106
- "#{parent}#{'.' unless parent.empty?}#{key}"
107
- end
108
- end
109
- end
@@ -1,55 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module RubyRabbitmqJanus
4
- # @author VAILLANT Jeremy <jeremy.vaillant@dazzl.tv>
5
- # Load files json in config/request.
6
- # This file is used for sending a request to RabbitMQ
7
- # @!attribute [r] requests
8
- class Requests
9
- include Singleton
10
-
11
- attr_reader :requests
12
-
13
- # Define folder to request
14
- PATH_REQUEST = 'config/requests/'
15
-
16
- # Load all requests in folder
17
- def initialize
18
- @requests = {}
19
- Log.instance.info "Loading all requests in : #{PATH_REQUEST}"
20
- Dir[File.join(PATH_REQUEST, '*')].count do |file|
21
- if File.file?(file)
22
- read_file(file)
23
- elsif File.directory?(file)
24
- each_folder(File.basename(file))
25
- end
26
- end
27
- end
28
-
29
- private
30
-
31
- # Run folder contains templates json
32
- def each_folder(subfolder)
33
- Dir[File.join(PATH_REQUEST + subfolder, '*')].count do |file|
34
- if File.file?(file)
35
- read_folder(subfolder.gsub('/', '::') + '::', file)
36
- elsif File.directory?(file)
37
- each_folder(subfolder + '/' + File.basename(file))
38
- end
39
- end
40
- end
41
-
42
- # Add template json to requests array
43
- # @param file [File]
44
- def read_file(file)
45
- @requests[File.basename(file, '.json').to_s] = File.path(file)
46
- end
47
-
48
- # Add template json to requests array with a path
49
- # @param folder [Dir]
50
- # @param file [File]
51
- def read_folder(folder, file)
52
- @requests[folder + File.basename(file, '.json').to_s] = File.path(file)
53
- end
54
- end
55
- end
@@ -1,30 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module RubyRabbitmqJanus
4
- # Format data with type corresponding
5
- class TypeData
6
- def initialize(type, value = nil)
7
- @type = type
8
- @value = value
9
- end
10
-
11
- # Cast a string
12
- def format
13
- case @type
14
- when '<number>'
15
- @value.to_i
16
- when '<string>'
17
- @value.to_s
18
- when /^(?<plugin\[[0-9]\]>)/
19
- extract_plugin_number
20
- end
21
- end
22
-
23
- private
24
-
25
- # Extract the number of plugin to used for this request
26
- def extract_plugin_number
27
- @type.delete('<plugin[').delete(']').to_i
28
- end
29
- end
30
- end
data/lib/rrj/version.rb DELETED
@@ -1,17 +0,0 @@
1
- # frozen_string_literal: true
2
- # @author VAILLANT Jeremy <jeremy.vaillant@dazzl.tv>
3
- # Define constant to gem.
4
- module RubyRabbitmqJanus
5
- # Define version to gem
6
- VERSION = '0.3.0'
7
-
8
- # Define a summary description to gem
9
- SUMMARY = 'Ruby RabbitMQ Janus'
10
-
11
- # Define a long description to gem
12
- DESCRIPTION = 'This gem is used to communicate to a server Janus through RabbitMQ '\
13
- 'software (Message-oriented middleware). It waiting a messages to Rails API who ' \
14
- 'send to RabbitMQ server in a queue for janus server. Janus processes a message ' \
15
- 'and send to RabbitMQ server in a queue for gem. Once the received message is ' \
16
- 'decoded and returned through the Rails API.'
17
- end