Class: Neo4j::Server::CypherResponse

Inherits:
Object
  • Object
show all
Defined in:
lib/neo4j-server/cypher_response.rb

Defined Under Namespace

Classes: ConstraintViolationError, HashEnumeration, ResponseError

Constant Summary collapse

EMPTY_STRING =
''
RETRYABLE_ERROR_STATUSES =
%w(DeadlockDetectedException AcquireLockTimeoutException ExternalResourceFailureException UnknownFailureException)
CONSTRAINT_ERROR =
'Neo.ClientError.Schema.ConstraintViolation'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response, uncommited = false) ⇒ CypherResponse

Returns a new instance of CypherResponse.



121
122
123
124
# File 'lib/neo4j-server/cypher_response.rb', line 121

def initialize(response, uncommited = false)
  @response = response
  @uncommited = uncommited
end

Instance Attribute Details

#columnsObject (readonly)

Returns the value of attribute columns.



4
5
6
# File 'lib/neo4j-server/cypher_response.rb', line 4

def columns
  @columns
end

#dataObject (readonly)

Returns the value of attribute data.



4
5
6
# File 'lib/neo4j-server/cypher_response.rb', line 4

def data
  @data
end

#error_codeObject (readonly)

Returns the value of attribute error_code.



4
5
6
# File 'lib/neo4j-server/cypher_response.rb', line 4

def error_code
  @error_code
end

#error_msgObject (readonly)

Returns the value of attribute error_msg.



4
5
6
# File 'lib/neo4j-server/cypher_response.rb', line 4

def error_msg
  @error_msg
end

#error_statusObject (readonly)

Returns the value of attribute error_status.



4
5
6
# File 'lib/neo4j-server/cypher_response.rb', line 4

def error_status
  @error_status
end

#responseObject (readonly)

Returns the value of attribute response.



4
5
6
# File 'lib/neo4j-server/cypher_response.rb', line 4

def response
  @response
end

#structObject (readonly)

Returns the value of attribute struct.



119
120
121
# File 'lib/neo4j-server/cypher_response.rb', line 119

def struct
  @struct
end

Class Method Details

.create_with_no_tx(response) ⇒ Object



223
224
225
226
227
228
229
230
# File 'lib/neo4j-server/cypher_response.rb', line 223

def self.create_with_no_tx(response)
  case response.status
  when 200 then new(response).set_data(response.body)
  when 400 then new(response).set_error(response.body)
  else
    fail "Unknown response code #{response.status} for #{response.env[:url]}"
  end
end

.create_with_tx(response) ⇒ Object



232
233
234
235
236
237
238
239
# File 'lib/neo4j-server/cypher_response.rb', line 232

def self.create_with_tx(response)
  fail "Unknown response code #{response.status} for #{response.request_uri}" unless response.status == 200

  new(response, true).tap do |cr|
    body = response.body
    body[:errors].empty? ? cr.set_data(body[:results].first) : cr.set_error(body[:errors].first)
  end
end

Instance Method Details

#add_entity_id(data) ⇒ Object



145
146
147
148
149
150
151
152
# File 'lib/neo4j-server/cypher_response.rb', line 145

def add_entity_id(data)
  data[:id] = if data[:metadata] && data[:metadata][:id]
                data[:metadata][:id]
              else
                data[:self].split('/')[-1].to_i
              end
  data
end

#constraint_error?Boolean

Returns:

  • (Boolean)


213
214
215
# File 'lib/neo4j-server/cypher_response.rb', line 213

def constraint_error?
  @error_code == CONSTRAINT_ERROR || @error_msg.include?('already exists with')
end

#data?Boolean

Returns:

  • (Boolean)


168
169
170
# File 'lib/neo4j-server/cypher_response.rb', line 168

def data?
  !response.body[:data].nil?
end

#each_data_rowObject



176
177
178
179
180
181
182
183
184
185
# File 'lib/neo4j-server/cypher_response.rb', line 176

def each_data_row
  data.each do |r|
    yieldable = if @uncommitted
                  r[:row]
                else
                  transaction_row?(r) ? r[:rest] : r
                end
    yield yieldable
  end
end

#entity_data(id = nil) ⇒ Object



126
127
128
129
130
131
132
133
134
# File 'lib/neo4j-server/cypher_response.rb', line 126

def entity_data(id = nil)
  if @uncommited
    data = @data.first[:row].first
    data.is_a?(Hash) ? {data: data, id: id} : data
  else
    data = @data[0][0]
    data.is_a?(Hash) ? add_entity_id(data) : data
  end
end

#error?Boolean

Returns:

  • (Boolean)


154
155
156
# File 'lib/neo4j-server/cypher_response.rb', line 154

def error?
  !!@error
end

#first_dataObject



136
137
138
139
140
141
142
143
# File 'lib/neo4j-server/cypher_response.rb', line 136

def first_data
  if @uncommited
    @data.first[:row].first
  else
    data = @data[0][0]
    data.is_a?(Hash) ? add_entity_id(data) : data
  end
end

#hash_value_as_object(value, session) ⇒ Object



82
83
84
85
86
87
88
# File 'lib/neo4j-server/cypher_response.rb', line 82

def hash_value_as_object(value, session)
  return value unless [:node, :relationship].include?(identify_entity(value))
  add_entity_id(value)

  basic_obj = (node?(value) ? CypherNode : CypherRelationship).new(session, value)
  unwrapped? ? basic_obj : basic_obj.wrapper
end

#identify_entity(data) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/neo4j-server/cypher_response.rb', line 90

def identify_entity(data)
  self_string = data[:self]
  if self_string
    if self_string.include?('node')
      :node
    elsif self_string.include?('relationship')
      :relationship
    end
  elsif [:nodes, :relationships, :start, :end, :length].all? { |k| data.key?(k) }
    :path
  end
end

#looks_like_an_object?(value) ⇒ Boolean

Returns:

  • (Boolean)


103
104
105
# File 'lib/neo4j-server/cypher_response.rb', line 103

def looks_like_an_object?(value)
  value[:labels] || value[:type]
end

#map_row_value(value, session) ⇒ Object



72
73
74
75
76
77
78
79
80
# File 'lib/neo4j-server/cypher_response.rb', line 72

def map_row_value(value, session)
  if value.is_a?(Hash) && looks_like_an_object?(value)
    hash_value_as_object(value, session)
  elsif value.is_a?(Array)
    value.map! { |v| map_row_value(v, session) }
  else
    value
  end
end

#node?(value) ⇒ Boolean

Returns:

  • (Boolean)


115
116
117
# File 'lib/neo4j-server/cypher_response.rb', line 115

def node?(value)
  value[:labels]
end

#raise_cypher_errorObject



217
218
219
220
# File 'lib/neo4j-server/cypher_response.rb', line 217

def raise_cypher_error
  fail 'Tried to raise error without an error' unless @error
  fail Neo4j::Session::CypherError.new(@error_msg, @error_code, @error_status)
end

#raise_errorObject



207
208
209
210
211
# File 'lib/neo4j-server/cypher_response.rb', line 207

def raise_error
  fail 'Tried to raise error without an error' unless @error
  error_class = constraint_error? ? ConstraintViolationError : ResponseError
  fail error_class.new(@error_msg, @error_status, @error_code)
end

#raise_if_cypher_error!Object



158
159
160
# File 'lib/neo4j-server/cypher_response.rb', line 158

def raise_if_cypher_error!
  raise_cypher_error if error?
end

#raise_unless_response_code(code) ⇒ Object



172
173
174
# File 'lib/neo4j-server/cypher_response.rb', line 172

def raise_unless_response_code(code)
  fail "Response code #{response.status}, expected #{code} for #{response.headers[:location]}, #{response.body}" unless response.status == code
end

#retryable_error?Boolean

Returns:

  • (Boolean)


163
164
165
166
# File 'lib/neo4j-server/cypher_response.rb', line 163

def retryable_error?
  return unless error?
  RETRYABLE_ERROR_STATUSES.include?(@error_status)
end

#row_pair_in_struct(row, session) ⇒ Object



64
65
66
67
68
69
70
# File 'lib/neo4j-server/cypher_response.rb', line 64

def row_pair_in_struct(row, session)
  @struct.new.tap do |result|
    row.each_pair do |column, value|
      result[column] = map_row_value(value, session)
    end
  end
end

#set_data(response) ⇒ Object



191
192
193
194
195
196
# File 'lib/neo4j-server/cypher_response.rb', line 191

def set_data(response)
  @data = response[:data]
  @columns = response[:columns]
  @struct = @columns.empty? ? Object.new : Struct.new(*@columns.map(&:to_sym))
  self
end

#set_error(error) ⇒ Object



198
199
200
201
202
203
204
# File 'lib/neo4j-server/cypher_response.rb', line 198

def set_error(error)
  @error = true
  @error_msg = error[:message]
  @error_status = error[:status] || error[:exception] || error[:code]
  @error_code = error[:code] || error[:fullname]
  self
end

#to_node_enumeration(cypher = EMPTY_STRING, session = Neo4j::Session.current) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/neo4j-server/cypher_response.rb', line 56

def to_node_enumeration(cypher = EMPTY_STRING, session = Neo4j::Session.current)
  Enumerator.new do |yielder|
    to_struct_enumeration(cypher).each do |row|
      yielder << row_pair_in_struct(row, session)
    end
  end
end

#to_struct_enumeration(cypher = EMPTY_STRING) ⇒ Object



52
53
54
# File 'lib/neo4j-server/cypher_response.rb', line 52

def to_struct_enumeration(cypher = EMPTY_STRING)
  HashEnumeration.new(self, cypher)
end

#transaction_response?Boolean

Returns:

  • (Boolean)


187
188
189
# File 'lib/neo4j-server/cypher_response.rb', line 187

def transaction_response?
  response.respond_to?(:body) && !response.body[:commit].nil?
end

#unwrapped!Object



107
108
109
# File 'lib/neo4j-server/cypher_response.rb', line 107

def unwrapped!
  @_unwrapped_obj = true
end

#unwrapped?Boolean

Returns:

  • (Boolean)


111
112
113
# File 'lib/neo4j-server/cypher_response.rb', line 111

def unwrapped?
  !!@_unwrapped_obj
end