Sha256: d725cc4e787915c156e74f28cf11c23c6c580b4ae9399ba8c761847d15048d7c

Contents?: true

Size: 1.55 KB

Versions: 3

Compression:

Stored size: 1.55 KB

Contents

# frozen_string_literal: true

module Redgraph
  # Wraps the GRAPH.QUERY response, assuming we use the `--compact` output.
  #
  # The response is an array with these objects:
  #
  # - header row
  # - result rows
  # - query stats
  #
  class QueryResponse
    def initialize(response)
      @response = response
    end

    def stats
      @stats ||= parse_stats
    end

    def entities
      @entities ||= parse_header
    end

    def resultset
      @resultset ||= parse_resultset
    end

    private

    # The header lists the entities described in the RETURN clause. It is an
    # array of [ColumnType (enum), name (string)] elements. We can ignore the
    # enum, it is always 1 (COLUMN_SCALAR).
    def parse_header
      @response[0].map{|item| item[1]}
    end

    def parse_stats
      stats = {}

      @response[2].each do |item|
        label, value = item.split(":")

        case label
        when /^Nodes created/
          stats[:nodes_created] = value.to_i
        when /^Relationships created/
          stats[:relationships_created] = value.to_i
        when /^Properties set/
          stats[:properties_set] = value.to_i
        when /^Query internal execution time/
          stats[:internal_execution_time] = value
        end
      end

      stats
    end

    # The resultset has one element per entity (as described by the header)
    def parse_resultset
      @response[1].map do |item|
        out = {}

        item.each.with_index do |(type, value), i|
          out[entities[i]] = value
        end

        out
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
redgraph-0.1.2 lib/redgraph/query_response.rb
redgraph-0.1.1 lib/redgraph/query_response.rb
redgraph-0.1.0 lib/redgraph/query_response.rb