Class: Neo4j::Core::CypherSession::Adaptors::HTTP

Inherits:
Base
  • Object
show all
Defined in:
lib/neo4j/core/cypher_session/adaptors/http.rb

Defined Under Namespace

Classes: Requestor

Constant Summary collapse

DEFAULT_FARADAY_CONFIGURATOR =
proc do |faraday|
  require 'typhoeus'
  require 'typhoeus/adapters/faraday'
  faraday.adapter :typhoeus
end
ROW_REST =
%w[row REST]
CONSTRAINT_TYPES =
{
  'UNIQUENESS' => :uniqueness
}

Constants inherited from Base

Base::EMPTY, Base::NEWLINE_W_SPACES, Base::USER_AGENT_STRING

Instance Attribute Summary collapse

Attributes inherited from Base

#options, #wrap_level

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#close, #default_subscribe, #logger, #queries, #query, #setup_queries!, #transaction

Methods included from Instrumentable

included

Constructor Details

#initialize(url, options = {}) ⇒ HTTP

Returns a new instance of HTTP.



14
15
16
17
# File 'lib/neo4j/core/cypher_session/adaptors/http.rb', line 14

def initialize(url, options = {})
  @url = url
  @options = options
end

Instance Attribute Details

#requestorObject (readonly)

Returns the value of attribute requestor.



12
13
14
# File 'lib/neo4j/core/cypher_session/adaptors/http.rb', line 12

def requestor
  @requestor
end

#urlObject (readonly)

Returns the value of attribute url.



12
13
14
# File 'lib/neo4j/core/cypher_session/adaptors/http.rb', line 12

def url
  @url
end

Class Method Details

.transaction_classObject



85
86
87
88
# File 'lib/neo4j/core/cypher_session/adaptors/http.rb', line 85

def self.transaction_class
  require 'neo4j/core/cypher_session/transactions/http'
  Neo4j::Core::CypherSession::Transactions::HTTP
end

Instance Method Details

#check_for_schema_response_error!(response) ⇒ Object



76
77
78
79
80
81
82
83
# File 'lib/neo4j/core/cypher_session/adaptors/http.rb', line 76

def check_for_schema_response_error!(response)
  if response.body.is_a?(Hash) && response.body.key?(:errors)
    message = response.body[:errors].map { |error| "#{error[:code]}: #{error[:message]}" }.join("\n    ")
    fail CypherSession::ConnectionFailedError, "Connection failure: \n    #{message}"
  elsif !response.success?
    fail CypherSession::ConnectionFailedError, "Connection failure: \n    status: #{response.status} \n    #{response.body}"
  end
end

#connectObject



25
26
27
# File 'lib/neo4j/core/cypher_session/adaptors/http.rb', line 25

def connect
  @requestor = Requestor.new(@url, USER_AGENT_STRING, self.class.method(:instrument_request), @options.fetch(:faraday_configurator, DEFAULT_FARADAY_CONFIGURATOR))
end

#connected?Boolean

Returns:

  • (Boolean)


101
102
103
# File 'lib/neo4j/core/cypher_session/adaptors/http.rb', line 101

def connected?
  !!@requestor
end

#constraints(_session, _label = nil, _options = {}) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/neo4j/core/cypher_session/adaptors/http.rb', line 64

def constraints(_session, _label = nil, _options = {})
  response = @requestor.get('db/data/schema/constraint')

  check_for_schema_response_error!(response)
  list = response.body || []
  list.map do |item|
    {type: CONSTRAINT_TYPES[item[:type]],
     label: item[:label].to_sym,
     properties: item[:property_keys].map(&:to_sym)}
  end
end

#indexes(_session) ⇒ Object

Schema inspection methods



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/neo4j/core/cypher_session/adaptors/http.rb', line 49

def indexes(_session)
  response = @requestor.get('db/data/schema/index')

  check_for_schema_response_error!(response)
  list = response.body || []

  list.map do |item|
    {label: item[:label].to_sym,
     properties: item[:property_keys].map(&:to_sym)}
  end
end

#indexes_for_label(label) ⇒ Object

Schema inspection methods



91
92
93
94
# File 'lib/neo4j/core/cypher_session/adaptors/http.rb', line 91

def indexes_for_label(label)
  url = db_data_url + "schema/index/#{label}"
  @connection.get(url)
end

#query_set(transaction, queries, options = {}) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/neo4j/core/cypher_session/adaptors/http.rb', line 31

def query_set(transaction, queries, options = {})
  setup_queries!(queries, transaction)

  return unless path = transaction.query_path(options.delete(:commit))

  faraday_response = @requestor.post(path, queries)

  transaction.apply_id_from_url!(faraday_response.env[:response_headers][:location])

  wrap_level = options[:wrap_level] || @options[:wrap_level]
  Responses::HTTP.new(faraday_response, wrap_level: wrap_level).results
end

#supports_metadata?Boolean

Returns:

  • (Boolean)


105
106
107
# File 'lib/neo4j/core/cypher_session/adaptors/http.rb', line 105

def supports_metadata?
  Gem::Version.new(version(nil)) >= Gem::Version.new('2.1.5')
end

#version(_session) ⇒ Object



44
45
46
# File 'lib/neo4j/core/cypher_session/adaptors/http.rb', line 44

def version(_session)
  @version ||= @requestor.get('db/data/').body[:neo4j_version]
end