Class: Orientdb4r::RestClient

Inherits:
Client
  • Object
show all
Includes:
Aop2
Defined in:
lib/orientdb4r/rest/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Aop2

#aop_context, #aop_context=, included

Methods inherited from Client

#connected?, #create_class, #create_property, #drop_class

Methods included from Utils

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

Constructor Details

#initialize(options) ⇒ RestClient

:nodoc:



12
13
14
15
16
17
18
19
# File 'lib/orientdb4r/rest/client.rb', line 12

def initialize(options) #:nodoc:
  super()
  options_pattern = { :host => 'localhost', :port => 2480, :ssl => false }
  verify_and_sanitize_options(options, options_pattern)
  @host = options[:host]
  @port = options[:port]
  @ssl = options[:ssl]
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



9
10
11
# File 'lib/orientdb4r/rest/client.rb', line 9

def host
  @host
end

#passwordObject (readonly)

Returns the value of attribute password.



9
10
11
# File 'lib/orientdb4r/rest/client.rb', line 9

def password
  @password
end

#portObject (readonly)

Returns the value of attribute port.



9
10
11
# File 'lib/orientdb4r/rest/client.rb', line 9

def port
  @port
end

#sslObject (readonly)

Returns the value of attribute ssl.



9
10
11
# File 'lib/orientdb4r/rest/client.rb', line 9

def ssl
  @ssl
end

#userObject (readonly)

Returns the value of attribute user.



9
10
11
# File 'lib/orientdb4r/rest/client.rb', line 9

def user
  @user
end

Instance Method Details

#command(sql) ⇒ Object

:nodoc:

Raises:

  • (ArgumentError)


118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/orientdb4r/rest/client.rb', line 118

def command(sql) #:nodoc:
  raise ArgumentError, 'command is blank' if blank? sql
  begin
#puts "REQ #{sql}"
    response = @resource["command/#{@database}/sql/#{URI.escape(sql)}"].post ''
    rslt = process_response(response)
    rslt
#puts "RESP #{response.code}"
  rescue
    raise OrientdbError
  end
end

#connect(options) ⇒ Object

:nodoc:



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/orientdb4r/rest/client.rb', line 22

def connect(options) #:nodoc:
  options_pattern = { :database => :mandatory, :user => :mandatory, :password => :mandatory }
  verify_and_sanitize_options(options, options_pattern)
  @database = options[:database]
  @user = options[:user]
  @password = options[:password]

  @resource = ::RestClient::Resource.new(url, :user => user, :password => password)

  begin
    response = @resource["connect/#{@database}"].get
    rslt = process_response(response, :mode => :strict)

    decorate_classes_with_model(rslt['classes'])

    @connected = true
  rescue
    @connected = false
    raise ConnectionError
  end
  rslt
end

#create_database(options) ⇒ Object

:nodoc:



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/orientdb4r/rest/client.rb', line 61

def create_database(options) #:nodoc:
  options_pattern = {
    :database => :mandatory, :type => 'memory',
    :user => :optional, :password => :optional, :ssl => false
  }
  verify_and_sanitize_options(options, options_pattern)

  u = options.include?(:user) ? options[:user] : user
  p = options.include?(:password) ? options[:password] : password
  resource = ::RestClient::Resource.new(url, :user => u, :password => p)
  begin
    response = resource["database/#{options[:database]}/#{options[:type]}"].post ''
  rescue
    raise OrientdbError
  end
  process_response(response)
end

#create_document(doc) ⇒ Object

—————————————————————– DOCUMENT

Raises:

  • (ArgumentError)


151
152
153
154
155
156
157
158
159
160
# File 'lib/orientdb4r/rest/client.rb', line 151

def create_document(doc)
  begin
    response = @resource["document/#{@database}"].post doc.to_json, :content_type => 'application/json'
  rescue
    raise DataError
  end
  rid = process_response(response)
  raise ArgumentError, "invalid RID format, RID=#{rid}" unless rid =~ /^#[0-9]+:[0-9]+/
  rid
end

#delete_document(rid) ⇒ Object

:nodoc:

Raises:

  • (ArgumentError)


196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/orientdb4r/rest/client.rb', line 196

def delete_document(rid) #:nodoc:
  raise ArgumentError, 'blank RID' if blank? rid
  # remove the '#' prefix
  rid = rid[1..-1] if rid.start_with? '#'

  begin
    response = @resource["document/#{@database}/#{rid}"].delete
    puts "DELETE '#{response}'"
  rescue
    raise DataError
  end
  # empty http response
end

#disconnectObject

:nodoc:



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/orientdb4r/rest/client.rb', line 46

def disconnect #:nodoc:
  return unless @connected

  begin
    response = @resource['disconnect'].get
  rescue ::RestClient::Unauthorized
    Orientdb4r::logger.warn '401 Unauthorized - bug in disconnect?'
  ensure
    @connected = false
    @user = nil
    @password = nil
  end
end

#get_class(name) ⇒ Object

:nodoc:

Raises:

  • (ArgumentError)


80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/orientdb4r/rest/client.rb', line 80

def get_class(name) #:nodoc:
  raise ArgumentError, "class name is blank" if blank?(name)

  # there seems to be a bug in REST API, only data are returned
  #response = @resource["class/#{@database}/#{name}"].get
  #rslt = process_response(response)

  # workaround - use metadate delivered by 'connect'
  response = @resource["connect/#{@database}"].get
  connect_info = process_response(response, :mode => :strict)

  classes = connect_info['classes'].select { |i| i['name'] == name }
  raise ArgumentError, "class not found, name=#{name}" unless 1 == classes.size
  decorate_classes_with_model(classes)
  clazz = classes[0]
  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


  clazz
end

#get_document(rid) ⇒ Object

:nodoc:

Raises:

  • (ArgumentError)


163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/orientdb4r/rest/client.rb', line 163

def get_document(rid) #:nodoc:
  raise ArgumentError, 'blank RID' if blank? rid
  # remove the '#' prefix
  rid = rid[1..-1] if rid.start_with? '#'

  begin
    response = @resource["document/#{@database}/#{rid}"].get
  rescue
    raise NotFoundError
  end
  rslt = process_response(response)
  rslt.extend Orientdb4r::DocumentMetadata
  rslt
end

#query(sql) ⇒ Object

:nodoc:

Raises:

  • (ArgumentError)


109
110
111
112
113
114
115
# File 'lib/orientdb4r/rest/client.rb', line 109

def query(sql) #:nodoc:
  raise ArgumentError, 'query is blank' if blank? sql

  response = @resource["query/#{@database}/sql/#{URI.escape(sql)}"].get
  rslt = process_response(response)
  rslt['result']
end

#server(options = {}) ⇒ Object

:nodoc:



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/orientdb4r/rest/client.rb', line 132

def server(options={}) #:nodoc:
  options_pattern = { :user => :optional, :password => :optional }
  verify_options(options, options_pattern)


  u = options.include?(:user) ? options[:user] : user
  p = options.include?(:password) ? options[:password] : password
  resource = ::RestClient::Resource.new(url, :user => u, :password => p)
  begin
    response = resource['server'].get
  rescue
    raise OrientdbError
  end
  process_response(response)
end

#update_document(doc) ⇒ Object

:nodoc:

Raises:

  • (ArgumentError)


179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/orientdb4r/rest/client.rb', line 179

def update_document(doc) #:nodoc:
  raise ArgumentError, 'document is nil' if doc.nil?
  raise ArgumentError, 'document has no RID' if doc.doc_rid.nil?
  raise ArgumentError, 'document has no version' if doc.doc_version.nil?

  rid = doc.delete '@rid'
  rid = rid[1..-1] if rid.start_with? '#'

  begin
    @resource["document/#{@database}/#{rid}"].put doc.to_json, :content_type => 'application/json'
  rescue
    raise DataError
  end
  # empty http response
end