ruby_rabbitmq_janus 0.1.1 → 0.3.0

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
  SHA1:
3
- metadata.gz: c04994698a3ccfc133c0826a4bcd92dcc58ff41d
4
- data.tar.gz: f9e4b1151c22937d7e31b572c4166a103e27709e
3
+ metadata.gz: 6030e029ea0bd2a5679dcad05087626f714ff188
4
+ data.tar.gz: a58783ef89a8357ca40f1fbcb67caf83ec622d07
5
5
  SHA512:
6
- metadata.gz: b07f2dbd3e7e6dc9cd32eed1f321da24ff80348b98d222e609c5a6f2ba24d7afa50a43b262a154be81cbee55b05b744c7627cc8e0fbe33f09aa78924c48e5d14
7
- data.tar.gz: b45f399e4e97bbad4b33f5a9e529f2508464c4128215b5b74b89ef915ac74eaba31b6e72396733f9e34057a59adfc6dac332d5fbaa6d320708ab8662bd8ac238
6
+ metadata.gz: 9e8fc17ef4c1567c5e59080c6516661fb0f7300e77ccaaf2dd45d181f6111b736085ad58a56d6f2e3808a8e6adb518d18e5854980e4d49e7a75de3a2badcd75e
7
+ data.tar.gz: 0ef125e275f278881be03b53c194d81d7a1a562faad21b2aa88c571876c705776a401d9be961fc247974e2b77ec04fddbadbb6f22af10bdd148be7238796ccee
data/README.md CHANGED
@@ -1,5 +1,4 @@
1
1
  # ruby-rabbitmq-janus
2
-
3
2
  Ruby Gem for Janus WebRTC Gateway integration using RabbitMQ message queue
4
3
 
5
4
  This gem is used to communicate to a server Janus through RabbitMQ software (
@@ -12,6 +11,9 @@ in a queue for gem. Once the received message is decoded and returned through th
12
11
  * [Installation](#installation)
13
12
  * [Configuration](#configuration)
14
13
  * [Usage](#usage)
14
+ * [Usage Synchrounous](#usage-with-synchronous-request)
15
+ * [Usage Asynchrounous](#usage-with-asynchronous-request)
16
+ * [Aliases](#aliases)
15
17
  * [Development](#development)
16
18
  * [RSpec](#rspec-test)
17
19
  * [Documentation](#documentation)
@@ -22,7 +24,7 @@ in a queue for gem. Once the received message is decoded and returned through th
22
24
 
23
25
  Use bitbucket for installing gem in your Gemfile
24
26
  ```ruby
25
- gem 'ruby-rabbitmq-janus', '~> 0.1.0'
27
+ gem 'ruby_rabbitmq_janus'
26
28
  ```
27
29
 
28
30
  ### Configuration
@@ -31,13 +33,15 @@ If you want used a customize configuration see [ruby-rabbitmq-janus.yml](config/
31
33
 
32
34
  ### Usage
33
35
 
34
- Exemple usage :
36
+ #### Usage with synchronous request
37
+
38
+ Exemple usage simple :
35
39
  ```ruby
36
40
  # Initialize gem
37
41
  transaction = RubyRabbitmqJanus::RRJ.new
38
42
 
39
43
  # Send a message type 'create'
40
- create = transaction.message_template_ask('create')
44
+ create = transaction.message_template_ask_sync('create')
41
45
  => {"janus"=>"create",
42
46
  "transaction"=>"U5MZ8IYNLF",
43
47
  "correlation"=>"6e77d355-6c3e-450c-89ad-a5daeb8e006a"}
@@ -45,7 +49,7 @@ create_response = transaction.message_template_response(create)
45
49
  => {"janus"=>"success", "transaction"=>"U5MZ8IYNLF", "data"=>{"id"=>7123088323743398}}
46
50
 
47
51
  # Destroy session created
48
- destroy = transaction.message_template_ask('destroy')
52
+ destroy = transaction.message_template_ask_sync('destroy')
49
53
  => {"janus"=>"destroy",
50
54
  "session_id"=>7123088323743398,
51
55
  "transaction"=>"PKS63VJD8C",
@@ -54,10 +58,44 @@ destroy_response = transaction_template_response(destroy)
54
58
  => {"janus"=>"success", "session_id"=>7123088323743398, "transaction"=>"PKS63VJD8C"}
55
59
  ```
56
60
 
57
- The methods message_template_ask and message_template_response have an aliases :wink:
61
+ Example usage with a complex request :
62
+ ```ruby
63
+ # Initialize gem
64
+ transaction = RubyRabbitmqJanus::RRJ.new
65
+
66
+ # Send a request type
67
+ transaction.transaction_plugin do |session_running|
68
+ test = transaction.ask_sync('test', session_running)
69
+ session = transaction.response_sync(session_running)
70
+ end
71
+ ```
72
+
73
+ #### Usage with asynchronous request
74
+
75
+ Exemple usage simple :
76
+
58
77
  ```ruby
59
- message_template_ask => ask
60
- message_template_response => response
78
+ # Initialize gem
79
+ transaction = RubyRabbitmqJanus::RRJ.new
80
+
81
+ # Send a message type create
82
+ create = transaction.ask_async('create')
83
+ => {"janus"=>"success", "session_id"=>1144864650609378, "transaction"=>"0XGUTFDLBK"}
84
+
85
+ # Send a message type destroy
86
+ transaction.ask_async('destroy', create)
87
+ => {"janus"=>"success", "session_id"=>1144864650609378, "transaction"=>"UPODB8YEG1"}
88
+ ```
89
+
90
+ #### Aliases
91
+
92
+ ```ruby
93
+ # Aliases to methods synchronous
94
+ message_template_ask_sync => ask_sync
95
+ message_template_response => response_sync
96
+
97
+ # Aliases to methods asynchronous
98
+ message_template_ask_async => ask_async
61
99
  ```
62
100
 
63
101
  ## Development
@@ -0,0 +1,9 @@
1
+ {
2
+ "janus": "message",
3
+ "session_id": "<number>",
4
+ "handle_id": "<number>",
5
+ "transaction": "<string>",
6
+ "body": {
7
+ "audio": false
8
+ }
9
+ }
data/config.reek CHANGED
@@ -1,3 +1,3 @@
1
1
  ---
2
2
  TooManyInstanceVariables:
3
- max_instance_variables: 7
3
+ max_instance_variables: 8
data/lib/rrj/config.rb CHANGED
@@ -5,9 +5,9 @@ require 'yaml'
5
5
  module RubyRabbitmqJanus
6
6
  # @author VAILLANT Jeremy <jeremy.vaillant@dazzl.tv>
7
7
  # Loading a yaml file for apply a configuration to gem.
8
- # @!attribute [r] options
9
- # @return [RubyRabbitmqJanus::Config] Options to gem.
10
8
  class Config
9
+ include Singleton
10
+
11
11
  attr_reader :options
12
12
 
13
13
  # Define a default path to file configuration
@@ -20,12 +20,11 @@ module RubyRabbitmqJanus
20
20
  CUSTOMIZE_CONF = 'config/ruby-rabbitmq-janus.yml'
21
21
 
22
22
  # Initialize configuration file default or customize if exist
23
- # @param logs [RubyRabbitmqJanus::Log] Load RubyRabbitmqJanus::Log to gem
24
- def initialize(logs)
25
- @logs = logs
26
- @options = load_configuration(File.join(DEFAULT_PATH, DEFAULT_CONF))
27
- override_configuration(File.join(Dir.pwd, CUSTOMIZE_CONF))
28
- precise_log_level
23
+ def initialize
24
+ @options = nil
25
+ conf_customize
26
+ conf_default
27
+ define_log_level_used
29
28
  end
30
29
 
31
30
  private
@@ -33,20 +32,26 @@ module RubyRabbitmqJanus
33
32
  # Load configuration file yaml
34
33
  # @return [Yaml] Configuration file
35
34
  # @param file [String] Path to configuration file (with name)
35
+ # :reek:UtilityFunction { public_methods_only: true }
36
36
  def load_configuration(file)
37
- @logs.info("Loading configuration file : #{file}")
37
+ Log.instance.info("Loading configuration file : #{file}")
38
38
  YAML.load(File.read(file))
39
39
  end
40
40
 
41
41
  # Load customize configuration file if exist
42
- # @return [Yaml] Configuration file
43
- # @param file [String] Path to configuration file (with name)
44
- def override_configuration(file)
42
+ def conf_customize
43
+ file = File.join(Dir.pwd, CUSTOMIZE_CONF)
45
44
  @options = load_configuration(file) if File.exist?(file)
46
45
  end
47
46
 
48
- def precise_log_level
49
- @logs.level = @options['gem']['log']['level']
47
+ # Load default configuration if customize configuration doesn't exist
48
+ def conf_default
49
+ file = File.join(DEFAULT_PATH, DEFAULT_CONF)
50
+ @options ||= load_configuration(file)
51
+ end
52
+
53
+ def define_log_level_used
54
+ Log.instance.level = @options['gem']['log']['level']
50
55
  end
51
56
  end
52
57
  end
data/lib/rrj/init.rb CHANGED
@@ -3,21 +3,20 @@
3
3
  module RubyRabbitmqJanus
4
4
  # @author VAILLANT Jeremy <jeremy.vaillant@dazzl.tv>
5
5
  # Initialize gem
6
- # @!attribute [r] rabbit
7
- # @return [RubyRabbitmqJanus::RabbitMQ] Object RabbitMQ for connection to RabbitMQ
8
- # server
9
- # @!attribute [r] logs
10
- # @return [RubyRabbitmqJanus::Log] Object Log for manipulate logs
11
- # @!attribute [r] settings
12
- # @return [RubyRabbitmqJanus::Config] Object Config to gem
6
+ # @!attribute [r] session
7
+ # @return [Hash] Response to request sending in transaction
13
8
  class RRJ
9
+ attr_reader :session
10
+
14
11
  # Returns a new instance of RubyRabbitmqJanus
15
12
  def initialize
16
- @logs = Log.instance
13
+ Log.instance
14
+ Config.instance
15
+ Requests.instance
16
+
17
+ @rabbit = RabbitMQ.new
17
18
 
18
- @settings = RubyRabbitmqJanus::Config.new(@logs)
19
- @requests = RubyRabbitmqJanus::Requests.new(@logs)
20
- @rabbit = RubyRabbitmqJanus::RabbitMQ.new(@settings, @requests.requests, @logs)
19
+ @session = nil
21
20
  end
22
21
 
23
22
  # Send a message, to RabbitMQ, with a template JSON
@@ -27,8 +26,8 @@ module RubyRabbitmqJanus
27
26
  # @option opts [String] :janus The message type
28
27
  # @option opts [String] :transaction The transaction identifier
29
28
  # @option opts [Hash] :data The option data to request
30
- def message_template_ask(template_used = 'info', opts = {})
31
- @rabbit.ask_request(template_used, opts)
29
+ def message_template_ask_sync(template_used = 'info', opts = {})
30
+ @rabbit.ask_request_sync(template_used, opts)
32
31
  end
33
32
 
34
33
  # Send a message to RabbitMQ for reading a response
@@ -41,7 +40,34 @@ module RubyRabbitmqJanus
41
40
  @rabbit.ask_response(info_request)
42
41
  end
43
42
 
44
- alias ask message_template_ask
45
- alias response message_template_response
43
+ # Manage a transaction with an plugin in janus
44
+ # Is create an session and attach with a plugin configured in file conf to gem, then
45
+ # when a treatment is complet is destroy a session
46
+ # @yieldparam session_attach [Hash] Use a session created
47
+ # @yieldreturn [Hash] Contains a result to transaction with janus server
48
+ def transaction_plugin
49
+ @session = yield attach_session
50
+ destroy_session
51
+ end
52
+
53
+ def message_template_ask_async(template_used = 'info', opts = {})
54
+ @rabbit.ask_request_async(template_used, opts)
55
+ end
56
+
57
+ alias ask_async message_template_ask_async
58
+ alias ask_sync message_template_ask_sync
59
+ alias response_sync message_template_response
60
+
61
+ private
62
+
63
+ def attach_session
64
+ Log.instance.debug 'Create an session'
65
+ response_sync(ask_sync('attach', ask_sync('create')))
66
+ end
67
+
68
+ def destroy_session
69
+ Log.instance.debug 'Destroy an session'
70
+ response_sync(ask_sync('destroy', response_sync(ask_sync('detach', @session))))
71
+ end
46
72
  end
47
73
  end
@@ -9,23 +9,31 @@ module RubyRabbitmqJanus
9
9
  # Returns a new instance of Janus
10
10
  # @param connection [String] Connection to RabbitMQ server
11
11
  # @param logs [RRJ::Log] Instance to log
12
- def initialize(connection, options, logs)
12
+ def initialize(connection)
13
13
  @channel = connection.create_channel
14
- @queues = options['queues']
15
- @plugins = options['janus']['plugins']
16
- @logs = logs
17
14
  end
18
15
 
19
16
  # Send a message to RabbitMQ
17
+ # @param request [String] Type request used by transaction
18
+ # @param opts [Hash] Contains the parameters used by request if necessary
19
+ # @return [Hash] Result to request
20
20
  def send(request, opts)
21
- message = MessageJanus.new(@plugins, @logs, opts)
22
- message.send(request, @channel, @queues['queue_to'])
21
+ message = Sync.new(opts, @channel)
22
+ message.send(request)
23
+ end
24
+
25
+ def send_async(request, opts)
26
+ message = ASync.new(opts, @channel)
27
+ message.send(request)
23
28
  end
24
29
 
25
30
  # Read a message to RabbitMQ
31
+ # @param info_message [Hash] Information request asking to janus
32
+ # @param connection [Object] Object contains a information to connection
33
+ # @return [Hash] Result to request
26
34
  def read(info_message, connection)
27
- response = ResponseJanus.new(@channel, connection, @logs, info_message)
28
- response.read(@queues['queue_from'])
35
+ response = ResponseJanus.new(@channel, connection, info_message)
36
+ response.read
29
37
  end
30
38
  end
31
39
  end
@@ -1,84 +1,31 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'json'
4
3
  require 'securerandom'
4
+ require 'thread'
5
5
 
6
6
  module RubyRabbitmqJanus
7
7
  # @author VAILLANT Jeremy <jeremy.vaillant@dazzl.tv>
8
8
  # Message Janus sending to rabbitmq server
9
9
  class MessageJanus
10
10
  # Initialiaze a message posting to RabbitMQ
11
- def initialize(plugins, logs, opts)
11
+ # @param plugins [String] Name of plugin used by transaction
12
+ # @param logs [RubyRabbitmqJanus::Log] Instance log
13
+ # @param opts [Hash] Contains information to request sending
14
+ def initialize(opts_request, channel)
12
15
  @correlation = SecureRandom.uuid
13
- @opts = opts
14
- @plugins = plugins
15
- @logs = logs
16
- @my_request = nil
17
- @message = nil
18
- end
19
-
20
- # Send a message to RabbitMQ server
21
- def send(json, channel, queue_to)
22
- @message = channel.default_exchange
23
- @message.publish(test_request_and_replace(json),
24
- routing_key: queue_to,
25
- correlation_id: @correlation,
26
- content_type: 'application/json')
27
- return_info_message
16
+ @options_request = opts_request
17
+ @channel = channel
28
18
  end
29
19
 
30
20
  private
31
21
 
32
- # Prepare a hash request and replace information necessary for janus
33
- def test_request_and_replace(json_file)
34
- @my_request = JSON.parse(File.read(json_file))
35
- replaces
36
- @my_request.to_json
37
- end
38
-
39
- def replaces
40
- replace_transaction
41
- if @opts
42
- replace_element('session_id')
43
- replace_plugin
44
- replace_element(@opts['handle_id'] ? 'handle_id' : 'sender', 'handle_id')
45
- end
46
- end
47
-
48
- # Replace a transaction field with an String format
49
- def replace_transaction
50
- @my_request['transaction'] = [*('A'..'Z'), *('0'..'9')].sample(10).join
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 @my_request[value_replace]
57
- end
58
-
59
- # Replace plugin used for transaction
60
- def replace_plugin
61
- my_plugin = @my_request['plugin']
62
- if my_plugin
63
- my_plugin = @plugins[my_plugin.delete('<plugin[').delete(']').to_i]
64
- add_return('plugin', my_plugin)
65
- end
66
- end
67
-
68
- # Prepare an Hash with information necessary to read a response in RabbitMQ queue
69
- def return_info_message
70
- @my_request['correlation'] = @correlation
71
- @logs.debug @my_request
72
- @my_request
73
- end
74
-
75
- def add_return(key, value)
76
- @my_request[key] = value
77
- @my_request.merge!(key => value)
78
- end
22
+ attr_reader :channel, :options_request, :opts, :correlation, :my_request, :message
23
+ attr_reader :log
79
24
 
80
- def value_data_or_precise(key)
81
- @opts[key] || @opts['data']['id']
25
+ def define_request_sending(json)
26
+ request = Replace.new(json, @options_request)
27
+ @my_request = request.to_hash
28
+ request.to_json
82
29
  end
83
30
  end
84
31
  end
@@ -0,0 +1,54 @@
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
@@ -0,0 +1,34 @@
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
@@ -6,20 +6,20 @@ module RubyRabbitmqJanus
6
6
  # @author VAILLANT Jeremy <jeremy.vaillant@dazzl.tv>
7
7
  # Response Janus received to RabbitMQ server
8
8
  class ResponseJanus
9
- def initialize(channel, connection, logs, opts = {})
9
+ # Initialiaze a response reading in RabbitMQ queue
10
+ def initialize(channel, connection, opts = {})
10
11
  @channel = channel
11
12
  @connection = connection
12
13
  @opts = opts
13
- @logs = logs
14
14
  @response = nil
15
15
  end
16
16
 
17
17
  # Read a response to janus (in RabbitMQ queue)
18
18
  # @return [Hash] resultat to request
19
- def read(queue_from)
20
- option_sub = { block: true, manual_ack: false, exclusive: false }
21
- the_queue = @channel.queue(queue_from)
22
- the_queue.subscribe(option_sub) do |info, prop, pay|
19
+ def read
20
+ Log.instance.debug 'Read a response'
21
+ the_queue = @channel.queue(Config.instance.options['queues']['queue_from'])
22
+ the_queue.subscribe(block: true) do |info, prop, pay|
23
23
  listen(info, prop, pay)
24
24
  end
25
25
  return_response_json
@@ -29,12 +29,13 @@ module RubyRabbitmqJanus
29
29
 
30
30
  # Listen a response to queue
31
31
  def listen(_delivery_info, properties, payload)
32
- if @opts['correlation'] == properties[:correlation_id]
32
+ if @opts['properties']['correlation'] == properties[:correlation_id]
33
33
  @response = payload
34
34
  @connection.close
35
35
  end
36
36
  end
37
37
 
38
+ # Format the response returning
38
39
  def return_response_json
39
40
  @response = JSON.parse(@response)
40
41
  case @opts['janus']
@@ -43,16 +44,12 @@ module RubyRabbitmqJanus
43
44
  when 'attach'
44
45
  merge('handle_id')
45
46
  end
46
- @logs.debug @response
47
47
  @response
48
48
  end
49
49
 
50
- def resp
51
- @response['data']['id']
52
- end
53
-
50
+ # Update value to element in Hash
54
51
  def merge(key)
55
- @response[key] = resp
52
+ @response[key] = @response['data']['id']
56
53
  end
57
54
  end
58
55
  end
data/lib/rrj/log.rb CHANGED
@@ -17,38 +17,45 @@ module RubyRabbitmqJanus
17
17
  # Define a default level to gem (:warn, :info, :debug)
18
18
  DEFAULT_LEVEL = :debug
19
19
 
20
+ attr_reader :log_file
21
+
20
22
  # Returns a new instance to Log
21
23
  def initialize
22
24
  @logs = Logging.logger[Time.now.to_s + ' --']
23
25
  @logs.level = DEFAULT_LEVEL
24
- @logs.add_appenders Logging.appenders.file(DEFAULT_LOG_DIR + '/' + DEFAULT_LOG_NAME)
26
+ @log_file = DEFAULT_LOG_DIR + '/' + DEFAULT_LOG_NAME
27
+ @logs.add_appenders Logging.appenders.file(@log_file)
25
28
 
26
- welcome = "\n"\
27
- "====================================\n" \
28
- "### Start gem Rails Rabbit Janus ###\n" \
29
- '===================================='
29
+ welcome = '### Start gem Rails Rabbit Janus ###'
30
30
  @logs.info(welcome)
31
31
  end
32
32
 
33
+ # Level to log
34
+ # @return [Integer] The level to log instance
33
35
  def level
34
36
  @logs.level
35
37
  end
36
38
 
39
+ # Save level to log
40
+ # @param level [Symbol] Save log level used by this gem
37
41
  def level=(level)
38
42
  @logs.level = level
39
43
  end
40
44
 
41
45
  # Write a message in log with a warn level
46
+ # @param message [String] Message writing in warning level in log
42
47
  def warn(message)
43
48
  @logs.warn(message)
44
49
  end
45
50
 
46
51
  # Write a message in log with a info level
52
+ # @param message [String] Message writing in info level in log
47
53
  def info(message)
48
54
  @logs.info(message)
49
55
  end
50
56
 
51
57
  # Write a message in log with a debug level
58
+ # @param message [String] Message writing in debug level in log
52
59
  def debug(message)
53
60
  @logs.debug(message)
54
61
  end
@@ -7,22 +7,24 @@ module RubyRabbitmqJanus
7
7
  # Class for connection with RabbitMQ Server
8
8
  class RabbitMQ
9
9
  # Return a new instance to RabbitMQ
10
- # @param configuration [RRJ::Config] Configuration file to gem
11
- # @param requests [Hash] Request sending to RabbitMQ
12
- # @param logs [RRJ::Log] Log to gem
13
- def initialize(configuration, requests, logs)
14
- @settings = configuration
15
- @logs = logs
16
- @requests = requests
10
+ def initialize
17
11
  @connection = Bunny.new(read_options_server)
18
- @response = nil
19
- @janus = nil
12
+ @janus, @response = nil
20
13
  end
21
14
 
22
15
  # Connect to server RabbitMQ and post a message in queue ('to-janus' by default)
23
- def ask_request(request_type, opts)
16
+ def ask_request_sync(request_type, opts)
24
17
  execute_request do
25
- @response = @janus.send(@requests[request_type.to_s], opts)
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)
26
28
  close_server_rabbitmq
27
29
  end
28
30
  end
@@ -49,10 +51,22 @@ module RubyRabbitmqJanus
49
51
  end
50
52
 
51
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
52
57
  def read_options_server
53
58
  hash = {}
54
- @settings.options.fetch('server').each do |key, server|
55
- hash.merge!(key.to_sym => server.to_s)
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
56
70
  end
57
71
  hash
58
72
  end
@@ -60,7 +74,7 @@ module RubyRabbitmqJanus
60
74
  # Execute request
61
75
  def execute_request
62
76
  open_server_rabbitmq
63
- @janus = Janus.new(@connection, @settings.options, @logs)
77
+ @janus = Janus.new(@connection)
64
78
  yield
65
79
  @response
66
80
  end
@@ -0,0 +1,21 @@
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
@@ -0,0 +1,109 @@
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
@@ -5,17 +5,18 @@ module RubyRabbitmqJanus
5
5
  # Load files json in config/request.
6
6
  # This file is used for sending a request to RabbitMQ
7
7
  # @!attribute [r] requests
8
- # @return [RubyRabbitmqJanus::Request] Array to request
9
8
  class Requests
9
+ include Singleton
10
+
10
11
  attr_reader :requests
11
12
 
12
13
  # Define folder to request
13
14
  PATH_REQUEST = 'config/requests/'
14
15
 
15
16
  # Load all requests in folder
16
- def initialize(logs)
17
+ def initialize
17
18
  @requests = {}
18
- logs.info "Loading all requests in : #{PATH_REQUEST}"
19
+ Log.instance.info "Loading all requests in : #{PATH_REQUEST}"
19
20
  Dir[File.join(PATH_REQUEST, '*')].count do |file|
20
21
  if File.file?(file)
21
22
  read_file(file)
@@ -0,0 +1,30 @@
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 CHANGED
@@ -3,7 +3,7 @@
3
3
  # Define constant to gem.
4
4
  module RubyRabbitmqJanus
5
5
  # Define version to gem
6
- VERSION = '0.1.1'
6
+ VERSION = '0.3.0'
7
7
 
8
8
  # Define a summary description to gem
9
9
  SUMMARY = 'Ruby RabbitMQ Janus'
@@ -4,11 +4,17 @@ require 'rrj/version'
4
4
  require 'rrj/init'
5
5
  require 'rrj/log'
6
6
  require 'rrj/config'
7
- require 'rrj/requests'
7
+
8
+ require 'rrj/request/requests'
9
+ require 'rrj/request/type_data'
10
+ require 'rrj/request/replaces'
11
+ require 'rrj/request/path'
8
12
 
9
13
  require 'rrj/rabbitmq/rabbitmq'
10
14
 
11
15
  require 'rrj/janus/janus'
12
16
  require 'rrj/janus/message'
17
+ require 'rrj/janus/message_sync'
18
+ require 'rrj/janus/message_async'
13
19
  require 'rrj/janus/response'
14
20
  require 'rrj/janus/error'
data/rrj.gemspec CHANGED
@@ -32,4 +32,6 @@ Gem::Specification.new do |spec|
32
32
 
33
33
  spec.add_runtime_dependency 'bunny', '~> 2.5'
34
34
  spec.add_runtime_dependency 'logging', '~> 2.1'
35
+ spec.add_runtime_dependency 'key_path', '~> 1.2'
36
+ spec.add_runtime_dependency 'thread', '~> 0.2.2'
35
37
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby_rabbitmq_janus
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - VAILLANT Jeremy
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-09-07 00:00:00.000000000 Z
11
+ date: 2016-09-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -224,6 +224,34 @@ dependencies:
224
224
  - - "~>"
225
225
  - !ruby/object:Gem::Version
226
226
  version: '2.1'
227
+ - !ruby/object:Gem::Dependency
228
+ name: key_path
229
+ requirement: !ruby/object:Gem::Requirement
230
+ requirements:
231
+ - - "~>"
232
+ - !ruby/object:Gem::Version
233
+ version: '1.2'
234
+ type: :runtime
235
+ prerelease: false
236
+ version_requirements: !ruby/object:Gem::Requirement
237
+ requirements:
238
+ - - "~>"
239
+ - !ruby/object:Gem::Version
240
+ version: '1.2'
241
+ - !ruby/object:Gem::Dependency
242
+ name: thread
243
+ requirement: !ruby/object:Gem::Requirement
244
+ requirements:
245
+ - - "~>"
246
+ - !ruby/object:Gem::Version
247
+ version: 0.2.2
248
+ type: :runtime
249
+ prerelease: false
250
+ version_requirements: !ruby/object:Gem::Requirement
251
+ requirements:
252
+ - - "~>"
253
+ - !ruby/object:Gem::Version
254
+ version: 0.2.2
227
255
  description: This gem is used to communicate to a server Janus through RabbitMQ software
228
256
  (Message-oriented middleware). It waiting a messages to Rails API who send to RabbitMQ
229
257
  server in a queue for janus server. Janus processes a message and send to RabbitMQ
@@ -253,15 +281,21 @@ files:
253
281
  - config/requests/destroy.json
254
282
  - config/requests/detach.json
255
283
  - config/requests/info.json
284
+ - config/requests/test.json
256
285
  - lib/rrj/config.rb
257
286
  - lib/rrj/init.rb
258
287
  - lib/rrj/janus/error.rb
259
288
  - lib/rrj/janus/janus.rb
260
289
  - lib/rrj/janus/message.rb
290
+ - lib/rrj/janus/message_async.rb
291
+ - lib/rrj/janus/message_sync.rb
261
292
  - lib/rrj/janus/response.rb
262
293
  - lib/rrj/log.rb
263
294
  - lib/rrj/rabbitmq/rabbitmq.rb
264
- - lib/rrj/requests.rb
295
+ - lib/rrj/request/path.rb
296
+ - lib/rrj/request/replaces.rb
297
+ - lib/rrj/request/requests.rb
298
+ - lib/rrj/request/type_data.rb
265
299
  - lib/rrj/version.rb
266
300
  - lib/ruby_rabbitmq_janus.rb
267
301
  - rrj.gemspec