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) ⇒ Object
:nodoc:.
-
#connect(options) ⇒ Object
:nodoc:.
-
#create_database(options) ⇒ Object
:nodoc:.
-
#create_document(doc) ⇒ Object
—————————————————————– DOCUMENT.
-
#delete_document(rid) ⇒ Object
:nodoc:.
-
#disconnect ⇒ Object
:nodoc:.
-
#get_class(name) ⇒ Object
:nodoc:.
-
#get_document(rid) ⇒ Object
:nodoc:.
-
#initialize(options) ⇒ RestClient
constructor
:nodoc:.
-
#query(sql) ⇒ Object
:nodoc:.
-
#server(options = {}) ⇒ Object
:nodoc:.
-
#update_document(doc) ⇒ 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) ⇒ Object
:nodoc:
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() #: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) 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() #: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 raise OrientdbError end process_response(response) end |
#create_document(doc) ⇒ Object
—————————————————————– DOCUMENT
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:
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 |
#disconnect ⇒ Object
: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:
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:
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:
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(={}) #:nodoc: = { :user => :optional, :password => :optional } (, ) u = .include?(:user) ? [:user] : user p = .include?(:password) ? [: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:
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 |