Class: Neography::Connection

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

Constant Summary collapse

USER_AGENT =
"Neography/#{Neography::VERSION}"
ACTIONS =
["get", "post", "put", "delete"]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Connection


16
17
18
19
20
21
22
23
24
25
26
# File 'lib/neography/connection.rb', line 16

def initialize(options = {})
  config = merge_configuration(options)
  save_local_configuration(config)
  @client ||= Excon.new(config[:proxy] || "#{protocol}://#{@server}:#{@port}", 
                        :read_timeout => config[:http_receive_timeout],
                        :write_timeout => config[:http_send_timeout],
                        :persistent => config[:persistent],
                        :user =>  config[:username],
                        :password => config[:password])
  #authenticate
end

Instance Attribute Details

#authenticationObject (readonly)

Returns the value of attribute authentication


8
9
10
# File 'lib/neography/connection.rb', line 8

def authentication
  @authentication
end

#clientObject (readonly)

Returns the value of attribute client


8
9
10
# File 'lib/neography/connection.rb', line 8

def client
  @client
end

#cypher_pathObject (readonly)

Returns the value of attribute cypher_path


8
9
10
# File 'lib/neography/connection.rb', line 8

def cypher_path
  @cypher_path
end

#directoryObject (readonly)

Returns the value of attribute directory


8
9
10
# File 'lib/neography/connection.rb', line 8

def directory
  @directory
end

#gremlin_pathObject (readonly)

Returns the value of attribute gremlin_path


8
9
10
# File 'lib/neography/connection.rb', line 8

def gremlin_path
  @gremlin_path
end

#http_receive_timeoutObject (readonly)

Returns the value of attribute http_receive_timeout


8
9
10
# File 'lib/neography/connection.rb', line 8

def http_receive_timeout
  @http_receive_timeout
end

#http_send_timeoutObject (readonly)

Returns the value of attribute http_send_timeout


8
9
10
# File 'lib/neography/connection.rb', line 8

def http_send_timeout
  @http_send_timeout
end

#log_enabledObject (readonly)

Returns the value of attribute log_enabled


8
9
10
# File 'lib/neography/connection.rb', line 8

def log_enabled
  @log_enabled
end

#log_fileObject (readonly)

Returns the value of attribute log_file


8
9
10
# File 'lib/neography/connection.rb', line 8

def log_file
  @log_file
end

#loggerObject (readonly)

Returns the value of attribute logger


8
9
10
# File 'lib/neography/connection.rb', line 8

def logger
  @logger
end

#max_threadsObject (readonly)

Returns the value of attribute max_threads


8
9
10
# File 'lib/neography/connection.rb', line 8

def max_threads
  @max_threads
end

#parserObject (readonly)

Returns the value of attribute parser


8
9
10
# File 'lib/neography/connection.rb', line 8

def parser
  @parser
end

#passwordObject (readonly)

Returns the value of attribute password


8
9
10
# File 'lib/neography/connection.rb', line 8

def password
  @password
end

#portObject (readonly)

Returns the value of attribute port


8
9
10
# File 'lib/neography/connection.rb', line 8

def port
  @port
end

#protocolObject (readonly)

Keeping backward compatability for folks who used “http://” syntax instead of “http”


36
37
38
# File 'lib/neography/connection.rb', line 36

def protocol
  @protocol
end

#proxyObject (readonly)

Returns the value of attribute proxy


8
9
10
# File 'lib/neography/connection.rb', line 8

def proxy
  @proxy
end

#serverObject (readonly)

Returns the value of attribute server


8
9
10
# File 'lib/neography/connection.rb', line 8

def server
  @server
end

#slow_log_thresholdObject (readonly)

Returns the value of attribute slow_log_threshold


8
9
10
# File 'lib/neography/connection.rb', line 8

def slow_log_threshold
  @slow_log_threshold
end

#usernameObject (readonly)

Returns the value of attribute username


8
9
10
# File 'lib/neography/connection.rb', line 8

def username
  @username
end

Instance Method Details

#authenticate(path = nil) ⇒ Object


93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/neography/connection.rb', line 93

def authenticate(path = nil)
  unless @authentication.empty?
    auth_type = @authentication.keys.first
    @client.set_auth(
      path,
      @authentication[auth_type][:username],
      @authentication[auth_type][:password]
    )
    # Force http client to always send auth credentials without
    # waiting for WWW-Authenticate headers, thus saving one request
    # roundtrip for each URI. Only works for HTTP basic auth.
    @client.www_auth.basic_auth.challenge(configuration) if auth_type == 'basic_auth'
  end
end

#configurationObject


40
41
42
# File 'lib/neography/connection.rb', line 40

def configuration
  @configuration ||= "#{protocol}://#{@server}:#{@port}#{@directory}"
end

#configure(protocol, server, port, directory) ⇒ Object


28
29
30
31
32
33
# File 'lib/neography/connection.rb', line 28

def configure(protocol, server, port, directory)
  @protocol = protocol
  @server = server
  @port = port
  @directory = directory
end

#log(path, body) ⇒ Object


81
82
83
84
85
86
87
88
89
90
91
# File 'lib/neography/connection.rb', line 81

def log(path, body)
  if @log_enabled
    start_time = Time.now
    response = yield
    time = ((Time.now - start_time) * 1000).round(2)
    @logger.info "[Neography::Query] #{path} #{body} [#{time}ms]" if time >= slow_log_threshold
    response
  else
    yield
  end
end

#merge_options(options) ⇒ Object


44
45
46
47
48
49
50
51
52
# File 'lib/neography/connection.rb', line 44

def merge_options(options)
  merged_options = options.merge!(@authentication)
  if merged_options[:headers]
    merged_options[:headers].merge!(@user_agent)
    merged_options[:headers].merge!('X-Stream' => true) unless merged_options[:headers].key?('X-Stream')
    merged_options[:headers].merge!(@max_execution_time)
  end
  merged_options
end