Sha256: e8e615db4f0850f712c09e40338bf82a5ac5e46e882cf3c8213dee89731e8c5d

Contents?: true

Size: 1.79 KB

Versions: 2

Compression:

Stored size: 1.79 KB

Contents

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 neo4j_url_prefix=(url)
      @neo4j_url_prefix = url
    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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
n4j-0.0.1.7 lib/n4j/request.rb
n4j-0.0.1.6.3 lib/n4j/request.rb