Class: GremlinClient::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/gremlin_client/connection.rb

Overview

represents the connection to our gremlin server

Constant Summary collapse

STATUS =
{
  success: 200,
  no_content: 204,
  partial_content: 206,

  unauthorized: 401,
  authenticate: 407,
  malformed_request: 498,
  invalid_request_arguments: 499,
  server_error: 500,
  script_evaluation_error: 597,
  server_timeout: 598,
  server_serialization_error: 599
}

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host: 'localhost', port: 8182, connection_timeout: 1, timeout: 10, gremlin_script_path: '.', autoconnect: true) ⇒ Connection

initialize a new connection using:

host    => hostname/ip where to connect
port    => listen port of the server
timeout => how long the client might wait for response from the server


33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/gremlin_client/connection.rb', line 33

def initialize(
  host: 'localhost',
  port: 8182,
  connection_timeout: 1,
  timeout: 10,
  gremlin_script_path: '.',
  autoconnect: true
)
  @host = host
  @port = port
  @connection_timeout = connection_timeout
  @timeout = timeout
  @gremlin_script_path = gremlin_script_path
  @gremlin_script_path = Pathname.new(@gremlin_script_path) unless @gremlin_script_path.is_a?(Pathname)
  @autoconnect = autoconnect
  connect if @autoconnect
end

Class Attribute Details

.poolObject

a centralized place for you to store a connection pool of those objects recommendeded one is: github.com/mperham/connection_pool



26
27
28
# File 'lib/gremlin_client/connection.rb', line 26

def pool
  @pool
end

Instance Attribute Details

#connection_timeoutObject (readonly)

Returns the value of attribute connection_timeout.



6
7
8
# File 'lib/gremlin_client/connection.rb', line 6

def connection_timeout
  @connection_timeout
end

#gremlin_script_pathObject (readonly)

Returns the value of attribute gremlin_script_path.



6
7
8
# File 'lib/gremlin_client/connection.rb', line 6

def gremlin_script_path
  @gremlin_script_path
end

#timeoutObject (readonly)

Returns the value of attribute timeout.



6
7
8
# File 'lib/gremlin_client/connection.rb', line 6

def timeout
  @timeout
end

Instance Method Details

#closeObject



91
92
93
# File 'lib/gremlin_client/connection.rb', line 91

def close
  @ws.close
end

#connectObject

creates a new connection object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/gremlin_client/connection.rb', line 52

def connect
  gremlin = self
  WebSocket::Client::Simple.connect("ws://#{@host}:#{@port}/") do |ws|
    @ws = ws

    @ws.on :message do |msg|
      gremlin.receive_message(msg)
    end

    @ws.on :error do |e|
      receive_error(e)
    end
  end
end

#open?Boolean

Returns:

  • (Boolean)


84
85
86
87
88
89
# File 'lib/gremlin_client/connection.rb', line 84

def open?
  @ws.open?
rescue ::NoMethodError
  # #2 => it appears to happen in some situations when the situation is dropped
  return false
end

#receive_error(e) ⇒ Object



111
112
113
# File 'lib/gremlin_client/connection.rb', line 111

def receive_error(e)
  @error = e
end

#receive_message(msg) ⇒ Object

this has to be public so the websocket client thread sees it



97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/gremlin_client/connection.rb', line 97

def receive_message(msg)
  response = JSON.parse(msg.data)
  # this check is important in case a request timeout and we make new ones after
  if response['requestId'] == @request_id
    if @response.nil?
      @response = response
    else
      @response['result']['data'].concat response['result']['data']
      @response['result']['meta'].merge! response['result']['meta']
      @response['status'] = response['status']
    end
  end
end

#reconnectObject



67
68
69
70
# File 'lib/gremlin_client/connection.rb', line 67

def reconnect
  @ws.close unless @ws.nil?
  connect
end

#send_file(filename, bindings = {}) ⇒ Object



80
81
82
# File 'lib/gremlin_client/connection.rb', line 80

def send_file(filename, bindings={})
  send_query(IO.read(resolve_path(filename)), bindings)
end

#send_query(command, bindings = {}) ⇒ Object



72
73
74
75
76
77
78
# File 'lib/gremlin_client/connection.rb', line 72

def send_query(command, bindings={})
  wait_connection
  reset_request
  @ws.send(build_message(command, bindings), { type: 'text' })
  wait_response
  return treat_response
end