require 'rest_client' module N4j::Request extend ActiveSupport::Concern included do end module ClassMethods def destroy_everything! clean_path = N4j.neo4j_url_prefix.sub('/db/data','/cleandb/all-gone') RestClient.delete(clean_path) end def neo4j_url_prefix @neo4j_url_prefix ||= ENV['NEO4J_REST_URL'] || "http://#{host}:#{port}/db/data" end def port config = YAML.load_file("config/n4j.yml") config[Rails.env]['port'] end def host config = YAML.load_file("config/n4j.yml") config[Rails.env]['host'] || 'localhost' end def neo4j_hash?(hsh) hsh && hsh['self'] && hsh['property'] && hsh['properties'] end def batch(commands) # [{'to'=> '', 'method' => '', 'body' => '', 'id' => 0},...] commands.flatten! # commands.each_with_index {|command, index| command[:id] ||= index } commands.inject(0) do |index, command| command[:id] = command[:id] || (index + 1) end puts "Batch job: " commands.each {|c| puts " #{c}"} begin result = RestClient.post("#{neo4j_url_prefix}/batch", commands.to_json, :accept => :json, :content_type => :json) JSON.parse(result) rescue RestClient::InternalServerError => e message = JSON.parse(JSON.parse(e.response)['message'])['message'] exception = JSON.parse(JSON.parse(e.response)['message'])['exception'] puts "Neo4j error: #{message}" if message puts "Neo4j excpetion: #{exception}" if exception rescue JSON::ParserError => e puts "JSON::ParserError ... raw result:\n #{result}" end end end end