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, 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, options={}) #: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, options.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(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)
    @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.message
    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(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 ::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

#disconnectObject

: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:

Raises:

  • (ArgumentError)


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