Class: Orientdb4r::RestClientNode

Inherits:
RestNode show all
Defined in:
lib/orientdb4r/rest/restclient_node.rb

Overview

This class represents a single sever/node in the Distributed Multi-Master Architecture accessible via REST API and ‘rest-client’ library on the client side.

Constant Summary

Constants inherited from RestNode

Orientdb4r::RestNode::SESSION_COOKIE_NAME

Instance Attribute Summary

Attributes inherited from RestNode

#ssl, #user_agent

Attributes inherited from Node

#host, #port, #session_id

Instance Method Summary collapse

Methods inherited from RestNode

#initialize, #url

Methods inherited from Node

#cleanup, #initialize, #to_s, #url

Methods included from Utils

#blank?, #compare_versions, #random_string, #verify_and_sanitize_options, #verify_options

Constructor Details

This class inherits a constructor from Orientdb4r::RestNode

Instance Method Details

#request(options) ⇒ Object

:nodoc:



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/orientdb4r/rest/restclient_node.rb', line 10

def request(options) #:nodoc:
  verify_options(options, {:user => :mandatory, :password => :mandatory, \
      :uri => :mandatory, :method => :mandatory, :content_type => :optional, :data => :optional,
      :no_session => :optional})

  opts = options.clone # if not cloned we change original hash map that cannot be used more with load balancing

  # URL
  opts[:url] = "#{url}/#{opts[:uri]}"
  opts.delete :uri

  if content_type = opts.delete(:content_type)
    opts[:headers] ||= {}
    opts[:headers][:content_type] = content_type
  end

  # data
  data = opts.delete :data
  data = '' if data.nil? and :post == opts[:method] # POST has to have data
  opts[:payload] = data unless data.nil?

  # cookies
  use_session = !opts.delete(:no_session)
  opts[:cookies] = { SESSION_COOKIE_NAME => session_id} if use_session and !session_id.nil?
  # headers
  opts[:headers] = { 'User-Agent' => user_agent } unless user_agent.nil?

  begin
    response = ::RestClient::Request.new(opts).execute

    # store session ID if received to reuse in next request
    sessid = response.cookies[SESSION_COOKIE_NAME]
    if session_id != sessid and use_session
      @session_id = sessid
      Orientdb4r::logger.debug "new session id: #{session_id}"
    end

  rescue Errno::ECONNREFUSED
    raise NodeError
  rescue ::RestClient::ServerBrokeConnection
    raise NodeError
  rescue ::RestClient::Exception => e
    response = transform_error2_response(e)
  end

  response
end