Class: Neo4j::Server::CypherResponse

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

Defined Under Namespace

Classes: HashEnumeration, ResponseError

Constant Summary collapse

EMPTY_STRING =
''
RETRYABLE_ERROR_STATUSES =
%w(DeadlockDetectedException AcquireLockTimeoutException ExternalResourceFailureException UnknownFailureException)

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.



124
125
126
127
# File 'lib/neo4j-server/cypher_response.rb', line 124

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.



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

def struct
  @struct
end

Class Method Details

.create_with_no_tx(response) ⇒ Object



215
216
217
218
219
220
221
222
223
224
# File 'lib/neo4j-server/cypher_response.rb', line 215

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

.create_with_tx(response) ⇒ Object



226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/neo4j-server/cypher_response.rb', line 226

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
    if body[:errors].empty?
      cr.set_data(body[:results].first)
    else
      cr.set_error(body[:errors].first)
    end
  end
end

.id_from_url(url) ⇒ Object



254
255
256
# File 'lib/neo4j-server/cypher_response.rb', line 254

def id_from_url(url)
  url.split('/')[-1].to_i
end

Instance Method Details

#add_entity_id(data) ⇒ Object



149
150
151
152
153
154
155
156
# File 'lib/neo4j-server/cypher_response.rb', line 149

def add_entity_id(data)
  data[:id] = if data[:metadata] && data[:metadata][:id]
                data[:metadata][:id]
              else
                self.class.id_from_url(data[:self])
              end
  data
end

#add_transaction_entity_idObject



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

def add_transaction_entity_id
  mapped_rest_data[:id] = mapped_rest_data[:self].split('/').last.to_i
  mapped_rest_data
end

#data?Boolean

Returns:

  • (Boolean)


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

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

#each_data_rowObject



181
182
183
184
185
186
187
# File 'lib/neo4j-server/cypher_response.rb', line 181

def each_data_row
  if @uncommited
    data.each { |r| yield r[:row] }
  else
    data.each { |r| yield r }
  end
end

#entity_data(id = nil) ⇒ Object



129
130
131
132
133
134
135
136
137
# File 'lib/neo4j-server/cypher_response.rb', line 129

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)


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

def error?
  !!@error
end

#first_dataObject



139
140
141
142
143
144
145
146
147
# File 'lib/neo4j-server/cypher_response.rb', line 139

def first_data
  if @uncommited
    @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

#hash_value_as_object(value, session) ⇒ Object



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

def hash_value_as_object(value, session)
  data =  case
          when transaction_response?
            add_transaction_entity_id
            mapped_rest_data
          when value[:labels] || value[:type]
            add_entity_id(value)
            value
          else
            return value
          end
  basic_obj = (node?(value) ? CypherNode : CypherRelationship).new(session, data)
  unwrapped? ? basic_obj : basic_obj.wrapper
end

#looks_like_an_object?(value) ⇒ Boolean

Returns:

  • (Boolean)


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

def looks_like_an_object?(value)
  if transaction_response?
    mapped_rest_data[:outgoing_relationships] || (mapped_rest_data[:start] && mapped_rest_data[:properties])
  else
    value[:labels] || value[:type]
  end
end

#map_row_value(value, session) ⇒ Object



77
78
79
80
81
82
83
84
85
# File 'lib/neo4j-server/cypher_response.rb', line 77

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)


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

def node?(value)
  transaction_response? ? !mapped_rest_data[:start] : value[:labels]
end

#raise_cypher_errorObject



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

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



204
205
206
207
# File 'lib/neo4j-server/cypher_response.rb', line 204

def raise_error
  fail 'Tried to raise error without an error' unless @error
  fail ResponseError.new(@error_msg, @error_status, @error_code)
end

#raise_unless_response_code(code) ⇒ Object



177
178
179
# File 'lib/neo4j-server/cypher_response.rb', line 177

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

#rest_dataObject



243
244
245
246
# File 'lib/neo4j-server/cypher_response.rb', line 243

def rest_data
  @result_index = @row_index = 0
  mapped_rest_data
end

#rest_data_with_idObject



248
249
250
251
# File 'lib/neo4j-server/cypher_response.rb', line 248

def rest_data_with_id
  rest_data[:id] = mapped_rest_data[:self].split('/').last.to_i
  rest_data
end

#retryable_error?Boolean

Returns:

  • (Boolean)


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

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

#row_pair_in_struct(row, session) ⇒ Object



68
69
70
71
72
73
74
75
# File 'lib/neo4j-server/cypher_response.rb', line 68

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)
      @row_index += 1
    end
  end
end

#set_data(response) ⇒ Object



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

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



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

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



57
58
59
60
61
62
63
64
65
66
# File 'lib/neo4j-server/cypher_response.rb', line 57

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

#to_struct_enumeration(cypher = EMPTY_STRING) ⇒ Object



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

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

#transaction_response?Boolean

Returns:

  • (Boolean)


239
240
241
# File 'lib/neo4j-server/cypher_response.rb', line 239

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

#unwrapped!Object



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

def unwrapped!
  @_unwrapped_obj = true
end

#unwrapped?Boolean

Returns:

  • (Boolean)


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

def unwrapped?
  !!@_unwrapped_obj
end