Class: Orientdb4r::RestClient
- Includes:
- Aop2
- Defined in:
- lib/orientdb4r/rest/client.rb
Constant Summary
Constants inherited from Client
Client::SERVER_VERSION_PATTERN
Instance Attribute Summary
Attributes inherited from Client
#connection_library, #database, #lb_strategy, #load_balancing, #nodes, #password, #proxy, #user
Instance Method Summary collapse
-
#command(sql) ⇒ Object
:nodoc:.
-
#connect(options) ⇒ Object
————————————————————— CONNECTION.
-
#create_database(options) ⇒ Object
—————————————————————– DATABASE.
-
#create_document(doc) ⇒ Object
—————————————————————– DOCUMENT.
-
#delete_database(options) ⇒ Object
:nodoc:.
-
#delete_document(rid) ⇒ Object
:nodoc:.
-
#disconnect ⇒ Object
:nodoc:.
-
#export(options = nil) ⇒ Object
:nodoc:.
-
#get_class(name) ⇒ Object
——————————————————————– CLASS.
-
#get_database(options = nil) ⇒ Object
> curl –user admin:admin localhost:2480/database/temp.
-
#get_document(rid) ⇒ Object
:nodoc:.
-
#import(options = nil) ⇒ Object
:nodoc:.
-
#initialize(options) ⇒ RestClient
constructor
:nodoc:.
-
#list_databases(options = nil) ⇒ Object
> curl –user root:root localhost:2480/listDatabases.
-
#query(sql, options = nil) ⇒ Object
———————————————————————- SQL.
-
#server(options = {}) ⇒ Object
:nodoc:.
-
#update_document(doc) ⇒ Object
:nodoc:.
Methods included from Aop2
#aop_context, #aop_context=, included
Methods inherited from Client
#class_exists?, #connected?, #create_class, #create_property, #database_exists?, #drop_class
Methods included from Utils
#blank?, #compare_versions, #random_string, #verify_and_sanitize_options, #verify_options
Constructor Details
#initialize(options) ⇒ RestClient
:nodoc:
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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/orientdb4r/rest/client.rb', line 13 def initialize() #:nodoc: super() = { :host => 'localhost', :port => 2480, :ssl => false, :nodes => :optional, :connection_library => :restclient, # :connection_library => :excon, :load_balancing => :sequence, :proxy => :optional, :user_agent => :optional, :lb_recover_time => :optional } (, ) # fake nodes for single server if [:nodes].nil? [:nodes] = [{:host => [:host], :port => [:port], :ssl => [:ssl]}] end raise ArgumentError, 'nodes has to be array' unless [:nodes].is_a? Array # instantiate nodes according to HTTP library @connection_library = [:connection_library] node_clazz = case connection_library when :restclient then Orientdb4r::RestClientNode when :excon then Orientdb4r::ExconNode else raise ArgumentError, "unknown connection library: #{connection_library}" end # nodes [:nodes].each do || (, ) @nodes << node_clazz.new([:host], [:port], [:ssl]) end # load balancing @load_balancing = [:load_balancing] @lb_strategy = case load_balancing when :sequence then Orientdb4r::Sequence.new nodes.size when :round_robin then Orientdb4r::RoundRobin.new nodes.size else raise ArgumentError, "unknow load balancing type: #{load_balancing}" end # recover time recover_time = [:lb_recover_time] @lb_strategy.recover_time = recover_time.to_i unless recover_time.nil? # proxy @proxy = [:proxy] unless proxy.nil? case connection_library when :restclient then ::RestClient.proxy = proxy when :excon then nodes.each { |node| node.proxy = proxy } end end # user-agent agent = [:user_agent] unless agent.nil? nodes.each { |node| node.user_agent = agent } end Orientdb4r::logger.info "client initialized with #{@nodes.size} node(s) " Orientdb4r::logger.info "connection_library=#{[:connection_library]}, load_balancing=#{load_balancing}" end |
Instance Method Details
#command(sql) ⇒ Object
:nodoc:
294 295 296 297 298 |
# File 'lib/orientdb4r/rest/client.rb', line 294 def command(sql) #:nodoc: raise ArgumentError, 'command is blank' if blank? sql response = call_server(:method => :post, :uri => "command/#{@database}/sql/#{CGI::escape(sql)}") process_response(response) end |
#connect(options) ⇒ Object
————————————————————— CONNECTION
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 107 108 109 |
# File 'lib/orientdb4r/rest/client.rb', line 81 def connect() #:nodoc: = { :database => :mandatory, :user => :mandatory, :password => :mandatory } (, ) @database = [:database] @user = [:user] @password = [:password] @nodes.each { |node| node.cleanup } # destroy all used session <= problem in 1.3.0-SNAPSHOT begin http_response = call_server(:method => :get, :uri => "connect/#{@database}") rescue @connected = false @user = nil @password = nil @database = nil @nodes.each { |node| node.cleanup } raise ConnectionError end rslt = process_response http_response # no metadata in connect v1.4.0+ # https://groups.google.com/forum/?fromgroups=#!topic/orient-database/R0VoOfIyDng #decorate_classes_with_model(rslt['classes']) unless rslt['classes'].nil? Orientdb4r::logger.info "successfully connected to server, code=#{rslt.code}" @connected = true @connected end |
#create_database(options) ⇒ Object
—————————————————————– DATABASE
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/orientdb4r/rest/client.rb', line 149 def create_database() #:nodoc: = { :database => :mandatory, :storage => :memory, :type => :document, :user => :optional, :password => :optional } (, ) (.select {|k,v| k === :storage or k === :type}, {:storage => [:memory, :plocal], :type => [:document, :graph]}) params = { :method => :post, :uri => "database/#{[:database]}/#{[:storage].to_s}/#{[:type].to_s}" } params[:no_session] = true # out of existing session which represents an already done authentication # additional authentication allowed, overriden in 'call_server' if not defined params[:user] = [:user] if .include? :user params[:password] = [:password] if .include? :password response = call_server params process_response(response) end |
#create_document(doc) ⇒ Object
—————————————————————– DOCUMENT
329 330 331 332 333 334 335 336 337 338 339 |
# File 'lib/orientdb4r/rest/client.rb', line 329 def create_document(doc) #:nodoc: http_response = call_server(:method => :post, :uri => "document/#{@database}", \ :content_type => 'application/json', :data => doc.to_json) resp = process_response(http_response) do raise DataError, 'validation problem' if http_response.body =~ /OValidationException/ end resp = ::JSON.parse(http_response.body) # workaround: https://groups.google.com/forum/?fromgroups=#!topic/orient-database/UJGAXYpHDmo resp.extend Orientdb4r::DocumentMetadata resp end |
#delete_database(options) ⇒ Object
:nodoc:
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 |
# File 'lib/orientdb4r/rest/client.rb', line 198 def delete_database() #:nodoc: = { :database => :mandatory, :user => :optional, :password => :optional } (, ) params = { :method => :delete, :uri => "database/#{[:database]}" } params[:no_session] = true # out of existing session which represents an already done authentication # additional authentication allowed, overriden in 'call_server' if not defined params[:user] = [:user] if .include? :user params[:password] = [:password] if .include? :password response = call_server params process_response(response) end |
#delete_document(rid) ⇒ Object
:nodoc:
373 374 375 376 377 378 379 380 381 |
# File 'lib/orientdb4r/rest/client.rb', line 373 def delete_document(rid) #:nodoc: rid = Rid.new(rid) unless rid.is_a? Rid response = call_server(:method => :delete, :uri => "document/#{@database}/#{rid.unprefixed}") process_response(response) do raise NotFoundError, 'record not found' if response.body =~ /ORecordNotFoundException/ end # empty http response end |
#disconnect ⇒ Object
:nodoc:
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/orientdb4r/rest/client.rb', line 112 def disconnect #:nodoc: return unless @connected begin call_server(:method => :get, :uri => 'disconnect') # https://groups.google.com/forum/?fromgroups#!topic/orient-database/5MAMCvFavTc # Disconnect doesn't require you're authenticated. # It always returns 401 because some browsers intercept this and avoid to reuse the same session again. ensure @connected = false @user = nil @password = nil @database = nil @nodes.each { |node| node.cleanup } Orientdb4r::logger.debug 'disconnected from server' end end |
#export(options = nil) ⇒ Object
:nodoc:
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 |
# File 'lib/orientdb4r/rest/client.rb', line 231 def export(=nil) #:nodoc: raise ArgumentError, 'options have to be a Hash' if !.nil? and !.kind_of? Hash if .nil? or (!.nil? and [:database].nil?) # use database from connect raise ConnectionError, 'client has to be connected if no database given' unless connected? = {} if .nil? [:database] = database end = { :database => :mandatory, :user => :optional, :password => :optional, :file => :optional } (, ) params = {:method => :get, :uri => "export/#{[:database]}"} params[:no_session] = true # out of existing session which represents an already done authentication # additional authentication allowed, overriden in 'call_server' if not defined params[:user] = [:user] if .include? :user params[:password] = [:password] if .include? :password response = call_server params rslt = process_response(response) filename = [:file] filename = response.headers[:content_disposition].split('filename=')[-1] if filename.nil? File.open(filename, 'w') do |f| f.write rslt end filename end |
#get_class(name) ⇒ Object
——————————————————————– CLASS
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 |
# File 'lib/orientdb4r/rest/client.rb', line 303 def get_class(name) #:nodoc: raise ArgumentError, "class name is blank" if blank?(name) response = call_server(:method => :get, :uri => "class/#{@database}/#{name}") rslt = process_response(response) do raise NotFoundError, 'class not found' if response.body =~ /Invalid class/ end classes = [rslt] 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_database(options = nil) ⇒ Object
> curl –user admin:admin localhost:2480/database/temp
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/orientdb4r/rest/client.rb', line 170 def get_database(=nil) #:nodoc: raise ArgumentError, 'options have to be a Hash' if !.nil? and !.kind_of? Hash if .nil? # use database from connect raise ConnectionError, 'client has to be connected if no params' unless connected? = { :database => database } end = { :database => :mandatory, :user => :optional, :password => :optional } (, ) params = {:method => :get, :uri => "database/#{[:database]}"} params[:no_session] = true # out of existing session which represents an already done authentication # additional authentication allowed, overriden in 'call_server' if not defined params[:user] = [:user] if .include? :user params[:password] = [:password] if .include? :password response = call_server params # NotFoundError cannot be raised - no way how to recognize from 401 bad auth rslt = process_response response decorate_classes_with_model(rslt['classes']) unless rslt['classes'].nil? rslt end |
#get_document(rid) ⇒ Object
:nodoc:
342 343 344 345 346 347 348 349 350 351 352 |
# File 'lib/orientdb4r/rest/client.rb', line 342 def get_document(rid) #:nodoc: rid = Rid.new(rid) unless rid.is_a? Rid response = call_server(:method => :get, :uri => "document/#{@database}/#{rid.unprefixed}") rslt = process_response(response) do raise NotFoundError, 'record not found' if response.body =~ /ORecordNotFoundException/ raise NotFoundError, 'record not found' if response.body =~ /Record with id .* was not found/ # why after delete? end rslt.extend Orientdb4r::DocumentMetadata rslt end |
#import(options = nil) ⇒ Object
:nodoc:
264 265 266 267 268 |
# File 'lib/orientdb4r/rest/client.rb', line 264 def import(=nil) #:nodoc: raise NotImplementedError, 'not working via REST API, see here for more info: https://github.com/nuvolabase/orientdb/issues/1345' # params = {:method => :post, :uri => 'import/'} # response = call_server params end |
#list_databases(options = nil) ⇒ Object
> curl –user root:root localhost:2480/listDatabases
217 218 219 220 221 222 223 224 225 226 227 228 |
# File 'lib/orientdb4r/rest/client.rb', line 217 def list_databases(=nil) #:nodoc: (, { :user => :optional, :password => :optional }) params = { :method => :get, :uri => 'listDatabases', :no_session => true } # additional authentication allowed, overriden in 'call_server' if not defined params[:user] = [:user] if .include? :user params[:password] = [:password] if .include? :password response = call_server params rslt = process_response(response) rslt['databases'] end |
#query(sql, options = nil) ⇒ Object
———————————————————————- SQL
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 |
# File 'lib/orientdb4r/rest/client.rb', line 273 def query(sql, =nil) #:nodoc: raise ArgumentError, 'query is blank' if blank? sql = { :limit => :optional } (, ) unless .nil? limit = '' limit = "/#{[:limit]}" if !.nil? and .include?(:limit) response = call_server(:method => :get, :uri => "query/#{@database}/sql/#{CGI::escape(sql)}#{limit}") 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 |
#server(options = {}) ⇒ Object
:nodoc:
131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/orientdb4r/rest/client.rb', line 131 def server(={}) #:nodoc: = { :user => :optional, :password => :optional } (, ) params = { :method => :get, :uri => 'server' } params[:no_session] = true # out of existing session which represents an already done authentication # additional authentication allowed, overriden in 'call_server' if not defined params[:user] = [:user] if .include? :user params[:password] = [:password] if .include? :password response = call_server params process_response(response) end |
#update_document(doc) ⇒ Object
:nodoc:
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 |
# File 'lib/orientdb4r/rest/client.rb', line 355 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.doc_rid doc.delete '@rid' # will be not updated response = call_server(:method => :put, :uri => "document/#{@database}/#{rid.unprefixed}", \ :content_type => 'application/json', :data => doc.to_json) process_response(response) do raise DataError, 'concurrent modification' if response.body =~ /OConcurrentModificationException/ raise DataError, 'validation problem' if response.body =~ /OValidationException/ end # empty http response end |