Class: GremlinClient::Connection
- Inherits:
-
Object
- Object
- GremlinClient::Connection
- 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
-
.pool ⇒ Object
a centralized place for you to store a connection pool of those objects recommendeded one is: github.com/mperham/connection_pool.
Instance Attribute Summary collapse
-
#connection_timeout ⇒ Object
readonly
Returns the value of attribute connection_timeout.
-
#gremlin_script_path ⇒ Object
readonly
Returns the value of attribute gremlin_script_path.
-
#timeout ⇒ Object
readonly
Returns the value of attribute timeout.
Instance Method Summary collapse
- #close ⇒ Object
-
#connect ⇒ Object
creates a new connection object.
-
#initialize(host: 'localhost', port: 8182, connection_timeout: 1, timeout: 10, gremlin_script_path: '.', autoconnect: true) ⇒ Connection
constructor
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.
- #open? ⇒ Boolean
- #receive_error(e) ⇒ Object
-
#receive_message(msg) ⇒ Object
this has to be public so the websocket client thread sees it.
- #reconnect ⇒ Object
- #send_file(filename, bindings = {}) ⇒ Object
- #send_query(command, bindings = {}) ⇒ Object
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
.pool ⇒ Object
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_timeout ⇒ Object (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_path ⇒ Object (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 |
#timeout ⇒ Object (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
#close ⇒ Object
91 92 93 |
# File 'lib/gremlin_client/connection.rb', line 91 def close @ws.close end |
#connect ⇒ Object
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.(msg) end @ws.on :error do |e| receive_error(e) end end end |
#open? ⇒ 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 (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 |
#reconnect ⇒ Object
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((command, bindings), { type: 'text' }) wait_response return treat_response end |