Class: SugarCRM::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/sugarcrm/session.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, user, pass, opts = {}) ⇒ Session

Returns a new instance of Session.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/sugarcrm/session.rb', line 6

def initialize(url, user, pass, opts={})
  options = { 
    :debug  => false,
    :register_modules => true
  }.merge(opts)
  
  @modules    = []
  @namespace  = "Namespace#{SugarCRM.used_namespaces.size}"
  @config     = {:base_url => url,:username => user,:password => pass,:options => options}
  @extensions_path = File.join(File.dirname(__FILE__), 'extensions')
  
  setup_connection
  register_namespace
  connect(@config[:base_url], @config[:username], @config[:password], @config[:options])
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



4
5
6
# File 'lib/sugarcrm/session.rb', line 4

def config
  @config
end

#connection_poolObject (readonly)

Returns the value of attribute connection_pool.



4
5
6
# File 'lib/sugarcrm/session.rb', line 4

def connection_pool
  @connection_pool
end

#extensions_pathObject (readonly)

Returns the value of attribute extensions_path.



4
5
6
# File 'lib/sugarcrm/session.rb', line 4

def extensions_path
  @extensions_path
end

#modulesObject

Returns the value of attribute modules.



5
6
7
# File 'lib/sugarcrm/session.rb', line 5

def modules
  @modules
end

#namespaceObject (readonly)

Returns the value of attribute namespace.



4
5
6
# File 'lib/sugarcrm/session.rb', line 4

def namespace
  @namespace
end

#namespace_constObject (readonly)

Returns the value of attribute namespace_const.



4
5
6
# File 'lib/sugarcrm/session.rb', line 4

def namespace_const
  @namespace_const
end

Class Method Details

.from_file(path, opts = {}) ⇒ Object

Creates a new session from the credentials present in a file



23
24
25
26
# File 'lib/sugarcrm/session.rb', line 23

def self.from_file(path, opts={})
  config_values = self.parse_config_file(path)
  self.from_hash(config_values[:config], opts)
end

.from_hash(hash, opts = {}) ⇒ Object

Creates a new session from the credentials in the hash



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/sugarcrm/session.rb', line 29

def self.from_hash(hash, opts={})
  pool_options = parse_connection_pool_options(hash)
  options = opts
  (options = {:connection_pool => pool_options}.merge(opts)) if pool_options.size > 0
  
  begin
    session = self.new(hash[:base_url], hash[:username], hash[:password], options)
  rescue MissingCredentials => e
    return false
  end
  session.namespace_const
end

.parse_config_file(path) ⇒ Object

Returns a hash with the content in the YAML argument file



43
44
45
46
47
48
# File 'lib/sugarcrm/session.rb', line 43

def self.parse_config_file(path)
  self.validate_path path
  contents = YAML.load_file(path)
  return {} unless contents
  self.symbolize_keys(contents)
end

Instance Method Details

#connect(url = nil, user = nil, pass = nil, opts = {}) ⇒ Object Also known as: connect!



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/sugarcrm/session.rb', line 55

def connect(url=nil, user=nil, pass=nil, opts={})
  options = { 
    :debug  => @config[:options][:debug],
    :register_modules => true
  }.merge(opts)
  
  # store the params used to connect
  {:base_url => url, :username => user, :password => pass}.each{|k,v|
    @config[k] = v  unless v.nil?
  }
  
  SugarCRM::Module.deregister_all(self)
  @connection_pool = SugarCRM::ConnectionPool.new(self)
  SugarCRM::Module.register_all(self)
  SugarCRM.add_session(self)
  load_extensions
  true
end

#connectionObject

Returns a connection from the connection pool, if available



94
95
96
# File 'lib/sugarcrm/session.rb', line 94

def connection
  @connection_pool.connection
end

#disconnectObject Also known as: disconnect!

log out from SugarCRM and cleanup (deregister modules, remove session, etc.)



84
85
86
87
88
89
90
# File 'lib/sugarcrm/session.rb', line 84

def disconnect
  @connection_pool.disconnect!
  SugarCRM::Module.deregister_all(self)
  namespace = @namespace
  SugarCRM.instance_eval{ remove_const namespace } # remove NamespaceX from SugarCRM
  SugarCRM.remove_session(self)
end

#extensions_folder=(folder, dirstring = nil) ⇒ Object



98
99
100
101
102
# File 'lib/sugarcrm/session.rb', line 98

def extensions_folder=(folder, dirstring=nil)
  path = File.expand_path(folder, dirstring)
  @extensions_path = path
  load_extensions
end

#load_config(path, opts = {}) ⇒ Object

load credentials from file, and (re)connect to SugarCRM



105
106
107
108
109
110
111
# File 'lib/sugarcrm/session.rb', line 105

def load_config(path, opts = {})
  opts.reverse_merge! :reconnect => true
  new_config = self.class.parse_config_file(path)
  update_config(new_config[:config]) if new_config and new_config[:config]
  reconnect(@config[:base_url], @config[:username], @config[:password]) if opts[:reconnect] and connection_info_loaded?
  @config
end

#reconnect(url = nil, user = nil, pass = nil, opts = {}) ⇒ Object Also known as: reconnect!, reload!

Re-uses this session and namespace if the user wants to connect with different credentials



76
77
78
79
# File 'lib/sugarcrm/session.rb', line 76

def reconnect(url=nil, user=nil, pass=nil, opts={})
  @connection_pool.disconnect!
  connect(url, user, pass, opts)
end

#setup_connectionObject

Raises:



50
51
52
53
# File 'lib/sugarcrm/session.rb', line 50

def setup_connection
  load_config_files unless connection_info_loaded?
  raise MissingCredentials, "Missing login credentials. Make sure you provide the SugarCRM URL, username, and password" unless connection_info_loaded?
end

#sugar_versionObject

lazy load the SugarCRM version we’re connecting to



119
120
121
# File 'lib/sugarcrm/session.rb', line 119

def sugar_version
  @version ||= connection.get_server_info["version"]
end

#update_config(params) ⇒ Object



113
114
115
116
# File 'lib/sugarcrm/session.rb', line 113

def update_config(params)
  params.each{ |k,v| @config[k.to_sym] = v }
  @config
end