diff --git a/gremlin_client.gemspec b/gremlin_client.gemspec index 7cc36d3..ea10706 100644 --- a/gremlin_client.gemspec +++ b/gremlin_client.gemspec @@ -6,7 +6,7 @@ require 'gremlin_client/version' Gem::Specification.new do |spec| spec.name = "gremlin_client" - spec.version = GremliClient::VERSION + spec.version = GremlinClient::VERSION spec.authors = ["Marcelo Coraça de Freitas"] spec.email = ["marcelo.freitas@finc.com"] spec.summary = %q{Simple Gremlin server client for the WebSocketChannelizer} @@ -20,7 +20,7 @@ Gem::Specification.new do |spec| spec.require_paths = ["lib"] spec.add_dependency 'websocket-client-simple', '~> 0.3' - spec.add_dependency 'oj', '~> 2.16' + spec.add_dependency 'oj' spec.add_development_dependency 'bundler', '~> 1.13' spec.add_development_dependency 'rake', '~> 12.0' diff --git a/lib/gremlin_client/connection.rb b/lib/gremlin_client/connection.rb index 8ee35da..fad058a 100644 --- a/lib/gremlin_client/connection.rb +++ b/lib/gremlin_client/connection.rb @@ -37,6 +37,7 @@ def initialize( connection_timeout: 1, timeout: 10, gremlin_script_path: '.', + secure: false, autoconnect: true ) @host = host @@ -46,6 +47,7 @@ def initialize( @timeout = timeout @gremlin_script_path = gremlin_script_path @gremlin_script_path = Pathname.new(@gremlin_script_path) unless @gremlin_script_path.is_a?(Pathname) + @secure = secure @autoconnect = autoconnect connect if @autoconnect end @@ -53,7 +55,8 @@ def initialize( # creates a new connection object def connect gremlin = self - WebSocket::Client::Simple.connect("ws://#{@host}:#{@port}#{@path}") do |ws| + protocol = @secure ? "wss" : "ws" + WebSocket::Client::Simple.connect("#{protocol}://#{@host}:#{@port}#{@path}") do |ws| @ws = ws @ws.on :message do |msg| @@ -61,7 +64,7 @@ def connect end @ws.on :error do |e| - receive_error(e) + gremlin.receive_error(e) end end end @@ -103,7 +106,7 @@ def receive_message(msg) if @response.nil? @response = response else - @response['result']['data'].concat response['result']['data'] + @response['result']['data'] = deep_merge(@response['result']['data'], response['result']['data']) @response['result']['meta'].merge! response['result']['meta'] @response['status'] = response['status'] end @@ -116,6 +119,18 @@ def receive_error(e) protected + def deep_merge(a, b) + a.merge(b) do |key, a_val, b_val| + if a_val.is_a?(Hash) && b_val.is_a?(Hash) + deep_merge(a_val, b_val) + elsif a_val.is_a?(Array) && b_val.is_a?(Array) + a_val + b_val + else + b_val + end + end + end + def wait_connection(skip_reconnect = false) w_from = Time.now.to_i while !open? && Time.now.to_i - @connection_timeout < w_from diff --git a/lib/gremlin_client/version.rb b/lib/gremlin_client/version.rb index a1e8ddf..7ec266a 100644 --- a/lib/gremlin_client/version.rb +++ b/lib/gremlin_client/version.rb @@ -1,3 +1,3 @@ -module GremliClient - VERSION = "0.1.6" +module GremlinClient + VERSION = "0.1.7" end diff --git a/spec/connection_spec.rb b/spec/connection_spec.rb index 7174d7e..9b6708e 100644 --- a/spec/connection_spec.rb +++ b/spec/connection_spec.rb @@ -58,6 +58,11 @@ def self.data conn = GremlinClient::Connection.new(host: :SERVER_A, port: 123, path: '/path/to/gremlin') end + it :websocket_secure do + expect(WebSocket::Client::Simple).to receive(:connect).with('wss://SERVER_A:123/path/to/secure') + conn = GremlinClient::Connection.new(host: :SERVER_A, port: 123, path: '/path/to/secure', secure: true) + end + it :gremlin_script_path do conn = GremlinClient::Connection.new expect(conn.gremlin_script_path).to eq(Pathname.new('.'))