Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Extract CypherResponseParser

  • Loading branch information...
commit 78502cae6d94b3f776cfcc3c8155861293df898d 1 parent 7f640e6
Micah Cooper and Travis Anderson authored Hashrocket Workstation committed
View
1  lib/keymaker.rb
@@ -7,6 +7,7 @@
require 'keymaker/match_method'
require 'keymaker/errors'
require 'keymaker/response'
+require 'parsers/cypher_response_parser'
require 'keymaker/configuration'
require 'keymaker/service'
View
20 lib/keymaker/service.rb
@@ -18,8 +18,8 @@ def connection=(connection)
def connection
@connection ||= Faraday.new(url: config.connection_service_root_url) do |conn|
- conn.use FaradayMiddleware::Mashify
conn.request :json
+ conn.response :mashify
conn.response :json, :content_type => /\bjson$/
conn.adapter :net_http
end
@@ -80,22 +80,8 @@ def batch_get_nodes(node_ids)
end
def execute_cypher(query, params)
- # TODO: factor this out into proper parser objects or its
- # own middleware
- body = execute_cypher_request({query: query, params: params}).body
- body.data.map do |result|
- if body.columns.count > 1
- Hashie::Mash.new(Hash[body.columns.zip(result)])
- else
- if result[0].kind_of?(Hashie::Mash)
- result[0].merge!(result[0].data)
- result[0].delete(:data)
- result[0]
- else
- Hashie::Mash.new(body.columns[0] => result[0])
- end
- end
- end
+ response = execute_cypher_request({query: query, params: params})
+ Keymaker::CypherResponseParser.parse(response.body)
end
def execute_script(script, params={})
View
19 lib/parsers/cypher_response_parser.rb
@@ -0,0 +1,19 @@
+module Keymaker
+ class CypherResponseParser
+
+ def self.parse(response_body)
+ response_body.data.map do |result|
+ if result.first.kind_of?(Hashie::Mash)
+ result.first.data
+ else
+ translate_response(response_body, result)
+ end
+ end
+ end
+
+ def self.translate_response(response_body, result)
+ Hashie::Mash.new(Hash[response_body.columns.zip(result)])
+ end
+
+ end
+end
View
30 spec/keymaker_spec.rb
@@ -152,20 +152,32 @@ def do_it
describe "#execute_cypher" do
- def do_it
- # service.execute_cypher("START user=node:users(email={email}) RETURN user", email: john_email)
- # service.execute_cypher("START n=node(*) RETURN n.email AS email, ID(n) AS neo4j_id", {})
- service.execute_cypher("START n=node(*) RETURN n.email AS email", {})
- # service.execute_cypher("START n=node(*) RETURN n AS user", {})
- end
+ let(:do_it) { service.execute_cypher(cypher_string, {}) }
+
+ before { service.add_node_to_index(:users, :email, john_email, john_node_id) }
+
+ context "setting key with as" do
+ let(:cypher_string) { "START n=node(*) RETURN n.email AS email" }
- context "given existing values" do
- before { service.add_node_to_index(:users, :email, john_email, john_node_id) }
it "performs the cypher query and responds" do
- require 'ruby-debug'; Debugger.start; Debugger.settings[:autoeval] = 1; Debugger.settings[:autolist] = 1; debugger
do_it.first.email.should == john_email
end
+ end
+
+ context "not setting the key" do
+ let(:cypher_string) { "START n=node(*) RETURN n.email" }
+
+ it "performs the cypher query and responds" do
+ do_it.first["n.email"].should == john_email
+ end
+ end
+
+ context "asks for entire object" do
+ let(:cypher_string) { "START n=node(*) RETURN n" }
+ it "performs the cypher query and responds" do
+ do_it.first.email.should == john_email
+ end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.