require 'rest_client' module N4j::Request extend ActiveSupport::Concern included do end module ClassMethods def neo4j_url_prefix @neo4j_url_prefix ||= "http://localhost:#{port}/db/data" end def port config = YAML.load_file("#{Rails.root}/config/n4j.yml") config[Rails.env]['port'] 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 } # puts "Batch job: #{commands.inspect}" 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