orientdb4r 0.2.3 → 0.2.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,7 +3,9 @@ module Orientdb4r
3
3
  class Client
4
4
  include Utils
5
5
 
6
+ # Server version used if no concrete version identified.
6
7
  DEFAULT_SERVER_VERSION = '1.0.0--'
8
+
7
9
  SERVER_VERSION_PATTERN = /^\d+\.\d+\.\d+/
8
10
 
9
11
  attr_reader :server_version
@@ -3,10 +3,13 @@ module Orientdb4r
3
3
  class RestClient < Client
4
4
  include Aop2
5
5
 
6
+ # Name of cookie that represents a session.
7
+ SESSION_COOKIE_NAME = 'OSESSIONID'
8
+
6
9
  before [:create_database, :get_class, :query, :command], :assert_connected
7
10
  around [:query, :command], :time_around
8
11
 
9
- attr_reader :host, :port, :ssl, :user, :password
12
+ attr_reader :host, :port, :ssl, :user, :password, :database, :session_id
10
13
 
11
14
 
12
15
  def initialize(options) #:nodoc:
@@ -26,12 +29,17 @@ module Orientdb4r
26
29
  @user = options[:user]
27
30
  @password = options[:password]
28
31
 
29
- @resource = ::RestClient::Resource.new(url, :user => user, :password => password)
30
32
 
31
33
  begin
32
- response = @resource["connect/#{@database}"].get
34
+ response = ::RestClient::Request.new(:method => :get, :url => "#{url}/connect/#{@database}", \
35
+ :user => user, :password => password).execute
36
+ @session_id = response.cookies[SESSION_COOKIE_NAME]
33
37
  rslt = process_response(response, :mode => :strict)
34
38
 
39
+ # resource used for all request
40
+ @resource = ::RestClient::Resource.new(url, \
41
+ :user => user, :password => password, :cookies => { SESSION_COOKIE_NAME => session_id})
42
+
35
43
  decorate_classes_with_model(rslt['classes'])
36
44
 
37
45
  # try to read server version
@@ -41,11 +49,18 @@ module Orientdb4r
41
49
  @server_version = DEFAULT_SERVER_VERSION
42
50
  end
43
51
  raise OrientdbError, "bad version format, version=#{server_version}" unless server_version =~ SERVER_VERSION_PATTERN
44
- Orientdb4r::logger.debug "successfully connected to server, version=#{server_version}"
52
+ Orientdb4r::logger.debug "successfully connected to server, version=#{server_version}, session=#{session_id}"
45
53
 
46
54
  @connected = true
55
+ @session_id = response.cookies[SESSION_COOKIE_NAME]
47
56
  rescue
48
57
  @connected = false
58
+ @session_id = nil
59
+ @server_version = nil
60
+ @user = nil
61
+ @password = nil
62
+ @database = nil
63
+ @resource = nil
49
64
  raise ConnectionError
50
65
  end
51
66
  rslt
@@ -63,8 +78,12 @@ module Orientdb4r
63
78
  # It always returns 401 because some browsers intercept this and avoid to reuse the same session again.
64
79
  ensure
65
80
  @connected = false
81
+ @server_version = nil
82
+ @session_id = nil
66
83
  @user = nil
67
84
  @password = nil
85
+ @database = nil
86
+ @resource = nil
68
87
  Orientdb4r::logger.debug 'disconnected from server'
69
88
  end
70
89
  end
@@ -144,7 +163,7 @@ module Orientdb4r
144
163
  response = @resource["command/#{@database}/sql/#{CGI::escape(sql)}"].post ''
145
164
  rslt = process_response(response)
146
165
  rslt
147
- #puts "RESP #{response.code}"
166
+ puts "RESP #{response.body}"
148
167
  rescue
149
168
  raise OrientdbError
150
169
  end
@@ -152,10 +171,11 @@ module Orientdb4r
152
171
 
153
172
 
154
173
  def server(options={}) #:nodoc:
174
+ # 'server' does NOT use the RestClient Resource to construct the HTTP request
175
+
155
176
  options_pattern = { :user => :optional, :password => :optional }
156
177
  verify_options(options, options_pattern)
157
178
 
158
-
159
179
  u = options.include?(:user) ? options[:user] : user
160
180
  p = options.include?(:password) ? options[:password] : password
161
181
  resource = ::RestClient::Resource.new(url, :user => u, :password => p)
@@ -274,7 +294,6 @@ module Orientdb4r
274
294
 
275
295
  def decorate_classes_with_model(classes)
276
296
  classes.each do |clazz|
277
- #puts "OOO #{classes}"
278
297
  clazz.extend Orientdb4r::HashExtension
279
298
  clazz.extend Orientdb4r::OClass
280
299
  unless clazz['properties'].nil? # there can be a class without properties
@@ -2,9 +2,11 @@ module Orientdb4r
2
2
 
3
3
  # Version history.
4
4
  VERSION_HISTORY = [
5
+ # v-- https://groups.google.com/forum/?fromgroups#!topic/orient-database/5MAMCvFavTc
6
+ ['0.2.4', '2012-06-26', "Added session management"],
5
7
  ['0.2.3', '2012-06-24', "Documents received by a query are kind of Orientdb4r::DocumentMetadata"],
6
- # https://groups.google.com/forum/?fromgroups#!topic/orient-database/jK4EZd068AE
7
- # https://groups.google.com/forum/?fromgroups#!topic/orient-database/nJOAsgwSnKI
8
+ # v-- https://groups.google.com/forum/?fromgroups#!topic/orient-database/jK4EZd068AE
9
+ # v-- https://groups.google.com/forum/?fromgroups#!topic/orient-database/nJOAsgwSnKI
8
10
  ['0.2.2', '2012-06-23', "Added support for server version detection [r5913]"],
9
11
  ['0.2.1', '2012-06-19', "Fixed linked property definition"],
10
12
  ['0.2.0', '2012-06-12', "Introduces document's CRUD operations"],
@@ -22,6 +22,14 @@ class TestDatabase < Test::Unit::TestCase
22
22
  assert_instance_of Hash, rslt
23
23
  assert rslt.size > 0
24
24
  assert rslt.include? 'classes'
25
+
26
+ assert_equal 'localhost', @client.host
27
+ assert_equal 2480, @client.port
28
+ assert_equal false, @client.ssl
29
+ assert_equal 'admin', @client.user
30
+ assert_equal 'admin', @client.password
31
+ assert_equal 'temp', @client.database
32
+ assert_not_nil @client.session_id
25
33
  assert_not_nil @client.server_version
26
34
 
27
35
  # connection refused
@@ -50,6 +58,15 @@ class TestDatabase < Test::Unit::TestCase
50
58
  assert !@client.connected?
51
59
  # unable to query after disconnect
52
60
  assert_raise Orientdb4r::ConnectionError do @client.query 'SELECT FROM OUser'; end
61
+
62
+ assert_equal 'localhost', @client.host
63
+ assert_equal 2480, @client.port
64
+ assert_equal false, @client.ssl
65
+ assert_nil @client.user
66
+ assert_nil @client.password
67
+ assert_nil @client.database
68
+ assert_nil @client.session_id
69
+ assert_nil @client.server_version
53
70
  end
54
71
 
55
72
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: orientdb4r
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
4
+ version: 0.2.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-06-24 00:00:00.000000000 Z
12
+ date: 2012-06-26 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rest-client