Class: Neo4j::Server::CypherResponse

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

Defined Under Namespace

Classes: HashEnumeration, ResponseError

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (CypherResponse) initialize(response, uncommited = false)

Returns a new instance of CypherResponse



91
92
93
94
# File 'lib/neo4j-server/cypher_response.rb', line 91

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

Instance Attribute Details

- (Object) columns (readonly)

Returns the value of attribute columns



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

def columns
  @columns
end

- (Object) data (readonly)

Returns the value of attribute data



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

def data
  @data
end

- (Object) error_code (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

- (Object) error_msg (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

- (Object) error_status (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

- (Object) response (readonly)

Returns the value of attribute response



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

def response
  @response
end

- (Object) struct (readonly)

Returns the value of attribute struct



89
90
91
# File 'lib/neo4j-server/cypher_response.rb', line 89

def struct
  @struct
end

Class Method Details

+ (Object) create_with_no_tx(response)



170
171
172
173
174
175
176
177
178
179
# File 'lib/neo4j-server/cypher_response.rb', line 170

def self.create_with_no_tx(response)
  case response.status
  when 200
    CypherResponse.new(response).set_data(response.body['data'], response.body['columns'])
  when 400
    CypherResponse.new(response).set_error(response.body['message'], response.body['exception'], response.body['fullname'])
  else
    fail "Unknown response code #{response.status} for #{response.env[:url]}"
  end
end

+ (Object) create_with_tx(response)



181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/neo4j-server/cypher_response.rb', line 181

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

  first_result = response.body['results'][0]
  cr = CypherResponse.new(response, true)

  if response.body['errors'].empty?
    cr.set_data(first_result['data'], first_result['columns'])
  else
    first_error = response.body['errors'].first
    cr.set_error(first_error['message'], first_error['status'], first_error['code'])
  end
  cr
end

+ (Object) id_from_url(url)



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

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

Instance Method Details

- (Object) add_entity_id(data)



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

def add_entity_id(data)
  data.merge!('id' => self.class.id_from_url(data['self']))
end

- (Object) add_transaction_entity_id



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

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

- (Boolean) data?

Returns:

  • (Boolean)


128
129
130
# File 'lib/neo4j-server/cypher_response.rb', line 128

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

- (Object) each_data_row



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

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

- (Object) entity_data(id = nil)



96
97
98
99
100
101
102
103
104
# File 'lib/neo4j-server/cypher_response.rb', line 96

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

- (Boolean) error?

Returns:

  • (Boolean)


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

def error?
  !!@error
end

- (Object) first_data(id = nil)



106
107
108
109
110
111
112
113
114
# File 'lib/neo4j-server/cypher_response.rb', line 106

def first_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

- (Object) hash_value_as_object(value, session)



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

def hash_value_as_object(value, session)
  return value unless value['labels'] || value['type'] || transaction_response?

  is_node, data =  if transaction_response?
                     add_transaction_entity_id
                     [!mapped_rest_data['start'], mapped_rest_data]
                   elsif value['labels'] || value['type']
                     add_entity_id(value)
                     [value['labels'], value]
                   end
  (is_node ? CypherNode : CypherRelationship).new(session, data).wrapper
end

- (Object) map_row_value(value, session)



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

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

- (Object) raise_cypher_error



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

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

- (Object) raise_error



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

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

- (Object) raise_unless_response_code(code)



132
133
134
# File 'lib/neo4j-server/cypher_response.rb', line 132

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

- (Object) rest_data



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

def rest_data
  @result_index = @row_index = 0
  mapped_rest_data
end

- (Object) rest_data_with_id



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

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

- (Object) set_data(data, columns)



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

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

- (Object) set_error(error_msg, error_status, error_core)



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

def set_error(error_msg, error_status, error_core)
  @error = true
  @error_msg = error_msg
  @error_status = error_status
  @error_code = error_core
  self
end

- (Object) to_node_enumeration(cypher = '', session = Neo4j::Session.current)



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/neo4j-server/cypher_response.rb', line 52

def to_node_enumeration(cypher = '', session = Neo4j::Session.current)
  Enumerator.new do |yielder|
    @result_index = 0
    to_struct_enumeration(cypher).each do |row|
      @row_index = 0
      yielder << row.each_pair.each_with_object(@struct.new) do |(column, value), result|
        result[column] = map_row_value(value, session)
        @row_index += 1
      end
      @result_index += 1
    end
  end
end

- (Object) to_struct_enumeration(cypher = '')



48
49
50
# File 'lib/neo4j-server/cypher_response.rb', line 48

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

- (Boolean) transaction_response?

Returns:

  • (Boolean)


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

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