Class: Orientdb4r::RestClient
- Includes:
- Aop2
- Defined in:
- lib/orientdb4r/rest/client.rb
Instance Attribute Summary collapse
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#password ⇒ Object
readonly
Returns the value of attribute password.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
-
#ssl ⇒ Object
readonly
Returns the value of attribute ssl.
-
#user ⇒ Object
readonly
Returns the value of attribute user.
Instance Method Summary collapse
-
#command(sql, options = {}) ⇒ Object
:nodoc:.
-
#connect(options) ⇒ Object
:nodoc:.
-
#create_database(options) ⇒ Object
:nodoc:.
-
#disconnect ⇒ Object
:nodoc:.
-
#get_class(name) ⇒ Object
:nodoc:.
-
#initialize(options) ⇒ RestClient
constructor
:nodoc:.
-
#query(sql) ⇒ Object
:nodoc:.
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() #:nodoc: super() = { :host => 'localhost', :port => 2480, :ssl => false } (, ) @host = [:host] @port = [:port] @ssl = [:ssl] end |
Instance Attribute Details
#host ⇒ Object (readonly)
Returns the value of attribute host.
9 10 11 |
# File 'lib/orientdb4r/rest/client.rb', line 9 def host @host end |
#password ⇒ Object (readonly)
Returns the value of attribute password.
9 10 11 |
# File 'lib/orientdb4r/rest/client.rb', line 9 def password @password end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
9 10 11 |
# File 'lib/orientdb4r/rest/client.rb', line 9 def port @port end |
#ssl ⇒ Object (readonly)
Returns the value of attribute ssl.
9 10 11 |
# File 'lib/orientdb4r/rest/client.rb', line 9 def ssl @ssl end |
#user ⇒ Object (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, options = {}) ⇒ Object
:nodoc:
106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/orientdb4r/rest/client.rb', line 106 def command(sql, ={}) #:nodoc: begin #puts "REQ #{sql}" response = @resource["command/#{@database}/sql/#{URI.escape(sql)}"].post '' rslt = process_response(response) rslt #puts "RESP #{response.code}" rescue Exception => e raise process_error e, .select { |k,v| k.to_s.start_with? 'http_code' } 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 44 45 |
# File 'lib/orientdb4r/rest/client.rb', line 22 def connect() #:nodoc: = { :database => :mandatory, :user => :mandatory, :password => :mandatory } (, ) @database = [:database] @user = [:user] @password = [:password] @resource = ::RestClient::Resource.new(url, :user => user, :password => password) begin response = @resource["connect/#{@database}"].get rslt = process_response(response, :mode => :strict) @connected = true rescue ::RestClient::Exception => e @connected = false raise process_error e, :http_code_401 => 'connect failed (bad credentials?)' rescue Exception => e Orientdb4r::logger.error e. Orientdb4r::logger.error e.backtrace.inspect @connected = false raise e 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 78 79 80 |
# File 'lib/orientdb4r/rest/client.rb', line 61 def create_database() #:nodoc: = { :database => :mandatory, :type => 'memory', :user => :optional, :password => :optional, :ssl => false } (, ) u = .include?(:user) ? [:user] : user p = .include?(:password) ? [:password] : password resource = ::RestClient::Resource.new(url, :user => u, :password => p) begin response = resource["database/#{[:database]}/#{[:type]}"].post '' rescue ::RestClient::Exception => e raise process_error e, \ :http_code_403 => 'forbidden operation (insufficient rights?)', \ :http_code_500 => 'failed to create database (exists already?)' end process_response(response) end |
#disconnect ⇒ Object
:nodoc:
48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/orientdb4r/rest/client.rb', line 48 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 end end |
#get_class(name) ⇒ Object
:nodoc:
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/orientdb4r/rest/client.rb', line 83 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) clazz = connect_info['classes'].select { |i| i['name'] == name } raise ArgumentError, "class not found, name=#{name}" unless 1 == clazz.size rslt = clazz[0] rslt.extend Orientdb4r::OClass rslt end |
#query(sql) ⇒ Object
:nodoc:
100 101 102 103 104 |
# File 'lib/orientdb4r/rest/client.rb', line 100 def query(sql) #:nodoc: response = @resource["query/#{@database}/sql/#{URI.escape(sql)}"].get rslt = process_response(response) rslt['result'] end |