Class: SugarCRM::Session
- Inherits:
-
Object
- Object
- SugarCRM::Session
- Defined in:
- lib/sugarcrm/session.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#connection_pool ⇒ Object
readonly
Returns the value of attribute connection_pool.
-
#extensions_path ⇒ Object
readonly
Returns the value of attribute extensions_path.
-
#modules ⇒ Object
Returns the value of attribute modules.
-
#namespace ⇒ Object
readonly
Returns the value of attribute namespace.
-
#namespace_const ⇒ Object
readonly
Returns the value of attribute namespace_const.
Class Method Summary collapse
-
.from_file(path, opts = {}) ⇒ Object
Creates a new session from the credentials present in a file.
-
.from_hash(hash, opts = {}) ⇒ Object
Creates a new session from the credentials in the hash.
-
.parse_config_file(path) ⇒ Object
Returns a hash with the content in the YAML argument file.
Instance Method Summary collapse
- #connect(url = nil, user = nil, pass = nil, opts = {}) ⇒ Object (also: #connect!)
-
#connection ⇒ Object
Returns a connection from the connection pool, if available.
-
#disconnect ⇒ Object
(also: #disconnect!)
log out from SugarCRM and cleanup (deregister modules, remove session, etc.).
- #extensions_folder=(folder, dirstring = nil) ⇒ Object
-
#initialize(url, user, pass, opts = {}) ⇒ Session
constructor
A new instance of Session.
-
#load_config(path, opts = {}) ⇒ Object
load credentials from file, and (re)connect to SugarCRM.
-
#reconnect(url = nil, user = nil, pass = nil, opts = {}) ⇒ Object
(also: #reconnect!, #reload!)
Re-uses this session and namespace if the user wants to connect with different credentials.
- #setup_connection ⇒ Object
-
#sugar_version ⇒ Object
lazy load the SugarCRM version we’re connecting to.
- #update_config(params) ⇒ Object
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={}) = { :debug => false, :register_modules => true }.merge(opts) @modules = [] @namespace = "Namespace#{SugarCRM.used_namespaces.size}" @config = {:base_url => url,:username => user,:password => pass,: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
#config ⇒ Object (readonly)
Returns the value of attribute config.
4 5 6 |
# File 'lib/sugarcrm/session.rb', line 4 def config @config end |
#connection_pool ⇒ Object (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_path ⇒ Object (readonly)
Returns the value of attribute extensions_path.
4 5 6 |
# File 'lib/sugarcrm/session.rb', line 4 def extensions_path @extensions_path end |
#modules ⇒ Object
Returns the value of attribute modules.
5 6 7 |
# File 'lib/sugarcrm/session.rb', line 5 def modules @modules end |
#namespace ⇒ Object (readonly)
Returns the value of attribute namespace.
4 5 6 |
# File 'lib/sugarcrm/session.rb', line 4 def namespace @namespace end |
#namespace_const ⇒ Object (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={}) = (hash) = opts ( = {:connection_pool => }.merge(opts)) if .size > 0 begin session = self.new(hash[:base_url], hash[:username], hash[:password], ) 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={}) = { :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 |
#connection ⇒ Object
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 |
#disconnect ⇒ Object 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.(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_connection ⇒ Object
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_version ⇒ Object
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 |