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

#initialize(response, uncommited = false) ⇒ CypherResponse

Returns a new instance of CypherResponse.



81
82
83
84
# File 'lib/neo4j-server/cypher_response.rb', line 81

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

Instance Attribute Details

#columnsObject (readonly)

Returns the value of attribute columns.



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

def columns
  @columns
end

#dataObject (readonly)

Returns the value of attribute data.



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

def data
  @data
end

#error_codeObject (readonly)

Returns the value of attribute error_code.



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

def error_code
  @error_code
end

#error_msgObject (readonly)

Returns the value of attribute error_msg.



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

def error_msg
  @error_msg
end

#error_statusObject (readonly)

Returns the value of attribute error_status.



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

def error_status
  @error_status
end

#responseObject (readonly)

Returns the value of attribute response.



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

def response
  @response
end

#structObject (readonly)

Returns the value of attribute struct.



79
80
81
# File 'lib/neo4j-server/cypher_response.rb', line 79

def struct
  @struct
end

Class Method Details

.create_with_no_tx(response) ⇒ Object



157
158
159
160
161
162
163
164
165
166
# File 'lib/neo4j-server/cypher_response.rb', line 157

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
      raise "Unknown response code #{response.status} for #{response.env[:url].to_s}"
  end
end

.create_with_tx(response) ⇒ Object



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

def self.create_with_tx(response)
  raise "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

Instance Method Details

#add_entity_id(data) ⇒ Object



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

def add_entity_id(data)
  data.merge!({'id' => data['self'].split('/')[-1].to_i})
end

#each_data_rowObject



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

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



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

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)


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

def error?
  !!@error
end

#first_data(id = nil) ⇒ Object



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

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

#map_row_value(value, session) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/neo4j-server/cypher_response.rb', line 61

def map_row_value(value, session)
  if value.is_a?(Hash)
    if value['labels']
      add_entity_id(value)
      CypherNode.new(session, value).wrapper
    elsif value['type']
      add_entity_id(value)
      CypherRelationship.new(session, value).wrapper
    else
      value
    end
  elsif value.is_a?(Array)
    value.map {|v| map_row_value(v, session) }
  else
    value
  end
end

#raise_cypher_errorObject



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

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

#raise_errorObject

Raises:



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

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

#raise_unless_response_code(code) ⇒ Object



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

def raise_unless_response_code(code)
  raise "Response code #{response.code}, expected #{code} for #{response.request.path}, #{response.body}" unless response.status == code
end

#set_data(data, columns) ⇒ Object



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

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

#set_error(error_msg, error_status, error_core) ⇒ Object



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

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

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



51
52
53
54
55
56
57
58
59
# File 'lib/neo4j-server/cypher_response.rb', line 51

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

#to_struct_enumeration(cypher = '') ⇒ Object



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

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

#uncommited?Boolean

Returns:

  • (Boolean)


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

def uncommited?
  @uncommited
end