Sha256: 8153c7358b58543b8f1fd04baee802d27a571b1edcd9ea73b7b15ab9260ca0c0

Contents?: true

Size: 1.83 KB

Versions: 30

Compression:

Stored size: 1.83 KB

Contents

module Neo4j
  module Server
    module Resource
      class ServerException < Exception
      end

      attr_reader :resource_data, :resource_url

      def init_resource_data(resource_data, resource_url)
        fail "Exception #{resource_data[:exception]}" if resource_data[:exception]
        fail "Expected @resource_data to be Hash got #{resource_data.inspect}" unless resource_data.respond_to?(:[])

        @resource_data = resource_data
        @resource_url = resource_url

        self
      end

      def wrap_resource(connection = Neo4j::Session.current)
        url = resource_url(:transaction)
        CypherTransaction.new(url, connection)
      end

      def resource_url(key = nil)
        return @resource_url if key.nil?
        @resource_data.fetch key
      rescue KeyError
        raise "No resource key '#{key}', available #{@resource_data.keys.inspect}"
      end

      def expect_response_code!(response, expected_code, msg = 'Error for request')
        handle_response_error!(response, "Expected response code #{expected_code} #{msg}") unless response.status == expected_code
        response
      end

      def handle_response_error!(response, msg = 'Error for request')
        fail ServerException, "#{msg} #{response.env && response.env[:url].to_s}, #{response.status}"
      end

      def response_exception(response)
        return nil if response.body.nil? || response.body.empty?
        JSON.parse(response.body)[:exception]
      end

      def resource_headers
        {'Content-Type' => 'application/json', 'Accept' => 'application/json; charset=UTF-8', 'User-Agent' => ::Neo4j::Session.user_agent_string}
      end

      def resource_url_id(url = resource_url)
        url.match(/\/(\d+)$/)[1].to_i
      end

      def convert_from_json_value(value)
        JSON.parse(value, quirks_mode: true)
      end
    end
  end
end

Version data entries

30 entries across 30 versions & 1 rubygems

Version Path
neo4j-core-5.1.14 lib/neo4j-server/resource.rb
neo4j-core-5.1.12 lib/neo4j-server/resource.rb
neo4j-core-5.1.11 lib/neo4j-server/resource.rb
neo4j-core-5.1.10 lib/neo4j-server/resource.rb
neo4j-core-5.1.7 lib/neo4j-server/resource.rb
neo4j-core-5.1.6 lib/neo4j-server/resource.rb
neo4j-core-5.1.5 lib/neo4j-server/resource.rb
neo4j-core-5.1.4 lib/neo4j-server/resource.rb
neo4j-core-5.1.3 lib/neo4j-server/resource.rb
neo4j-core-5.1.2 lib/neo4j-server/resource.rb
neo4j-core-5.1.1 lib/neo4j-server/resource.rb
neo4j-core-5.1.0 lib/neo4j-server/resource.rb
neo4j-core-5.1.0.rc.3 lib/neo4j-server/resource.rb
neo4j-core-5.1.0.rc.2 lib/neo4j-server/resource.rb
neo4j-core-5.1.0.rc.1 lib/neo4j-server/resource.rb
neo4j-core-5.0.10 lib/neo4j-server/resource.rb
neo4j-core-5.0.9 lib/neo4j-server/resource.rb
neo4j-core-5.0.8 lib/neo4j-server/resource.rb
neo4j-core-5.0.7 lib/neo4j-server/resource.rb
neo4j-core-5.0.6 lib/neo4j-server/resource.rb