Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions gremlin_client.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -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'
Expand Down
21 changes: 18 additions & 3 deletions lib/gremlin_client/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def initialize(
connection_timeout: 1,
timeout: 10,
gremlin_script_path: '.',
secure: false,
autoconnect: true
)
@host = host
Expand All @@ -46,22 +47,24 @@ 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

# 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|
gremlin.receive_message(msg)
end

@ws.on :error do |e|
receive_error(e)
gremlin.receive_error(e)
end
end
end
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lib/gremlin_client/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module GremliClient
VERSION = "0.1.6"
module GremlinClient
VERSION = "0.1.7"
end
5 changes: 5 additions & 0 deletions spec/connection_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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('.'))
Expand Down