class Orientdb4r::RestClient

Public Instance Methods

batch(operations) click to toggle source

Executes a batch of operations in a single call.

# File lib/orientdb4r/rest/client.rb, line 321
def batch(operations)
  response = call_server(:method => :post, :uri => "batch/#{@database}", \
      :content_type => 'application/json', :data => operations.to_json)
  process_response(response)
end
gremlin(gremlin) click to toggle source

Executes a Gremlin command against the database.

# File lib/orientdb4r/rest/client.rb, line 300
def gremlin(gremlin)
  raise ArgumentError, 'gremlin query is blank' if blank? gremlin
  response = call_server(:method => :post, :uri => "command/#{@database}/gremlin/#{CGI::escape(gremlin)}")
  entries = process_response(response) do
    raise NotFoundError, 'record not found' if response.body =~ /ORecordNotFoundException/
  end

  rslt = entries['result']
  # mixin all document entries (they have '@class' attribute)
  rslt.each { |doc| doc.extend Orientdb4r::DocumentMetadata unless doc['@class'].nil? }
  rslt
end

Private Instance Methods

compose_error_message(http_response, max_len=200) click to toggle source

Composes message of an error raised if the HTTP response doesn't correspond with expectation.

# File lib/orientdb4r/rest/client.rb, line 462
def compose_error_message(http_response, max_len=200)
  msg = http_response.body.gsub("\n", ' ')
  if (matcher = msg.match(/"content": "([^"]+)"/))
    msg = matcher[1]
  end
  msg = "#{msg[0..max_len]} ..." if msg.size > max_len
  msg
end
decorate_classes_with_model(classes) click to toggle source
# File lib/orientdb4r/rest/client.rb, line 502
def decorate_classes_with_model(classes)
  classes.each do |clazz|
    clazz.extend Orientdb4r::HashExtension
    clazz.extend Orientdb4r::OClass
      unless clazz['properties'].nil? # there can be a class without properties
        clazz.properties.each do |prop|
          prop.extend Orientdb4r::HashExtension
          prop.extend Orientdb4r::Property
      end
    end
  end
end
process_response(response) { || ... } click to toggle source

Processes a HTTP response.

# File lib/orientdb4r/rest/client.rb, line 417
def process_response(response)
  raise ArgumentError, 'response is null' if response.nil?

  if block_given?
    yield
  end


  # return code
  if 400 == response.code
    raise InvalidRequestError, compose_error_message(response)
  elsif 401 == response.code
    raise UnauthorizedError, compose_error_message(response)
  elsif 404 == response.code
    raise NotFoundError, compose_error_message(response)
  elsif 409 == response.code
    raise StateConflictError, compose_error_message(response)
  elsif 500 == response.code
    raise ServerError, compose_error_message(response)
  elsif 2 != (response.code / 100)
    raise OrientdbError, "unexpected return code, code=#{response.code}, body=#{compose_error_message(response)}"
  end

  content_type = response.headers[:content_type] if connection_library == :restclient
  content_type = response.headers['Content-Type'] if connection_library == :excon
  content_type ||= 'text/plain'

  rslt = case
    when content_type.start_with?('text/plain')
      response.body
    when content_type.start_with?('application/x-gzip')
      response.body
    when content_type.start_with?('application/json')
      ::JSON.parse(response.body)
    else
      raise OrientdbError, "unsuported content type: #{content_type}"
    end

  rslt
end
process_restclient_response(response, options={}) click to toggle source

@deprecated

# File lib/orientdb4r/rest/client.rb, line 473
def process_restclient_response(response, options={})
  raise ArgumentError, 'response is null' if response.nil?

  # raise problem if other code than 200
  if options[:mode] == :strict and 200 != response.code
    raise OrientdbError, "unexpeted return code, code=#{response.code}"
  end
  # log warning if other than 200 and raise problem if other code than 'Successful 2xx'
  if options[:mode] == :warning
    if 200 != response.code and 2 == (response.code / 100)
      Orientdb4r::logger.warn "expected return code 200, but received #{response.code}"
    elseif 200 != response.code
      raise OrientdbError, "unexpeted return code, code=#{response.code}"
    end
  end

  content_type = response.headers[:content_type]
  content_type ||= 'text/plain'

  rslt = case
    when content_type.start_with?('text/plain')
      response.body
    when content_type.start_with?('application/json')
      ::JSON.parse(response.body)
    end

  rslt
end