Class: Orientdb4r::ExconNode

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

Overview

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

Constant Summary

Constants inherited from RestNode

RestNode::SESSION_COOKIE_NAME

Instance Attribute Summary collapse

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

#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 Attribute Details

#proxyObject

Returns the value of attribute proxy.



10
11
12
# File 'lib/orientdb4r/rest/excon_node.rb', line 10

def proxy
  @proxy
end

Instance Method Details

#cleanupObject

:nodoc:



70
71
72
73
74
# File 'lib/orientdb4r/rest/excon_node.rb', line 70

def cleanup #:nodoc:
  super
  connection.reset
  @connection = nil
end

#post_connect(user, password, http_response) ⇒ Object

:nodoc:



62
63
64
65
66
67
# File 'lib/orientdb4r/rest/excon_node.rb', line 62

def post_connect(user, password, http_response) #:nodoc:

  cookies = CGI::Cookie::parse(http_response.headers['Set-Cookie'])
  @session_id = cookies[SESSION_COOKIE_NAME][0]

end

#request(options) ⇒ Object

:nodoc:



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
57
58
59
# File 'lib/orientdb4r/rest/excon_node.rb', line 12

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

  # Auth + Cookie + Content-Type
  opts[:headers] = headers(opts)
  opts.delete :user
  opts.delete :password
  use_session = !opts.delete(:no_session)

  opts[:body] = opts[:data] if opts.include? :data # just other naming convention
  opts.delete :data
  opts[:path] = opts[:uri] if opts.include? :uri   # just other naming convention
  opts.delete :uri

  was_ok = false
  begin
    response = connection.request opts
    was_ok = (2 == (response.status / 100))

    # store session ID if received to reuse in next request
    cookies = CGI::Cookie::parse(response.headers['Set-Cookie'])
    sessid = cookies[SESSION_COOKIE_NAME][0]
    if session_id != sessid and use_session
      @session_id = sessid
      Orientdb4r::logger.debug "new session id: #{session_id}"
    end

    def response.code
      status
    end

  rescue Excon::Errors::SocketError
    raise NodeError
  end

  # this is workaround for a strange behavior:
  # excon delivered magic response status '1' when previous request was not 20x
  unless was_ok
    connection.reset
    Orientdb4r::logger.debug 'response code not 20x -> connection reset'
  end

  response
end