Class: OdooClient::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/odoo_client/client.rb

Defined Under Namespace

Classes: AuthenticationError

Instance Method Summary collapse

Constructor Details

#initialize(url, database, username, password) ⇒ Client

Returns a new instance of Client.



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/odoo_client/client.rb', line 9

def initialize(url, database, username, password)
  @url = url
  @common = XMLRPC::Client.new2("#{@url}/xmlrpc/2/common")
  @db = database
  @password = password
  
  @common.call('version')
  @uid = @common.call('authenticate', @db, username, @password, {})

  raise AuthenticationError.new if @uid == false
end

Instance Method Details

#count_records(model_name, filters = []) ⇒ Object



29
30
31
# File 'lib/odoo_client/client.rb', line 29

def count_records(model_name, filters=[])
  models.execute_kw(@db, @uid, @password, model_name, 'search_count', [filters], {})
end

#create_record(model_name, params) ⇒ Object



44
45
46
# File 'lib/odoo_client/client.rb', line 44

def create_record(model_name, params)
  models.execute_kw(@db, @uid, @password, model_name, 'create', [params])
end

#delete_records(model_name, params) ⇒ Object



56
57
58
# File 'lib/odoo_client/client.rb', line 56

def delete_records(model_name, params)
  models.execute_kw(@db, @uid, @password, model_name, 'unlink', [params])
end

#find(model_name, id, select_fields = []) ⇒ Object



64
65
66
67
# File 'lib/odoo_client/client.rb', line 64

def find(model_name, id, select_fields=[])
  result = read_records(model_name, [["id", "=", id]], select_fields)
  result[0] unless result.empty?
end

#list_records(model_name, filters = []) ⇒ Object



33
34
35
# File 'lib/odoo_client/client.rb', line 33

def list_records(model_name, filters=[])
  models.execute_kw(@db, @uid, @password, model_name, 'search', [filters], {})
end

#meObject



25
26
27
# File 'lib/odoo_client/client.rb', line 25

def me
  @uid
end

#model_attributes(model_name, info = ['string', 'help', 'type']) ⇒ Object



60
61
62
# File 'lib/odoo_client/client.rb', line 60

def model_attributes(model_name, info=['string', 'help', 'type'])
  models.execute_kw(@db, @uid, @password, model_name, 'fields_get', [], {'attributes': info})
end

#read_records(model_name, filters = [], select_fields = [], offset = nil, limit = nil) ⇒ Object



37
38
39
40
41
42
# File 'lib/odoo_client/client.rb', line 37

def read_records(model_name, filters=[], select_fields=[], offset=nil, limit=nil)
  optional_params = {fields: select_fields}
  optional_params[:offset] = offset unless offset.nil?
  optional_params[:limit] = limit unless limit.nil?
  models.execute_kw(@db, @uid, @password, model_name, 'search_read', [filters], optional_params)
end

#update_record(model_name, id, params) ⇒ Object



48
49
50
# File 'lib/odoo_client/client.rb', line 48

def update_record(model_name, id, params)
  update_records(model_name, [id], params)
end

#update_records(model_name, record_ids, params) ⇒ Object



52
53
54
# File 'lib/odoo_client/client.rb', line 52

def update_records(model_name, record_ids, params)
  models.execute_kw(@db, @uid, @password, model_name, 'write', [record_ids, params])
end

#versionObject



21
22
23
# File 'lib/odoo_client/client.rb', line 21

def version
  @common.call('version')["server_version"]
end