Sha256: bf1e29b7acaa35439407b6d3b9230c5e11c4a286b61d382197f58f2e000acfd9

Contents?: true

Size: 1.3 KB

Versions: 2

Compression:

Stored size: 1.3 KB

Contents

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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
n4j-0.0.1.1 lib/n4j/request.rb
n4j-0.0.1 lib/n4j/request.rb