= SugarCRM
REST Bindings for SugarCRM!
== SUMMARY:
A less clunky way to interact with SugarCRM via REST.
== FEATURES/PROBLEMS:
== SYNOPSIS:
require 'sugarcrm'
SugarCRM.connect("http://localhost/sugarcrm", 'user', 'password')
SugarCRM.connection.debug = true
SugarCRM.reload!
SugarCRM.current_user
SugarCRM.modules
users = SugarCRM::User.find_by_user_name("admin")
users.url
u = SugarCRM::User.find_by_first_name_and_last_name("Will", "Westin") u.title = "Sales Manager Central" u.save
u.valid?
u.errors
u.required_attributes
a = SugarCRM::Account.find_by_name("JAB Funds Ltd.") a.delete
SugarCRM::User.find_by_user_name('sarah').email_addresses
SugarCRM::Account.find_by_name("JAB Funds Ltd.").contacts.each do |contact| contact.email_addresses.each do |email| puts "#{email.email_address}" unless email.opt_out end end
c = SugarCRM::Contact.first c.meetings << SugarCRM::Meeting.new({ :name => "Product Introduction", :date_start => DateTime.now, :duration_hours => 1 }) c.save!
a = SugarCRM::Account.find_by_name("JAB Funds Ltd.") c = SugarCRM::Contact.new c.last_name = 'Doe' a.contacts << c a.save # or a.contacts.save
c = SugarCRM::Contact.find_by_last_name("Doe") a = SugarCRM::Account.find_by_name("JAB Funds Ltd.") a.contacts.include?(c)
c = SugarCRM::Contact.find_by_last_name("Doe") a = SugarCRM::Account.find_by_name("JAB Funds Ltd.") a.contacts.delete(c) a.save # or a.contacts.save
SugarCRM::Account.count(:conditions => {:name => "LIKE '%Inc'"})
SugarCRM::Case.first({ :order_by => 'case_number' })
SugarCRM::Account.all({ :conditions => { :billing_address_postalcode => ["> '10000'", "<= '10500'" ] }, :limit => '10', :order_by => 'billing_address_postalcode' })
SugarCRM::Account.all({ :conditions => { :billing_address_postalcode => "<> NULL" } })
SugarCRM::Account.all({ :conditions => { :name => "LIKE '%Fund%'" } # note that SQL operators must be uppercase })
SugarCRM::Account.all{|a| puts a.name } # note: this method will be much quicker than SugarCRM::Account.all.each{|a| puts a.name }
each
# will fetch all records, and only then pass them to the block). This will make a major difference
# in resource use and execution time if you're dealing with many records.
account = SugarCRM::Account.first doc = account.documents.first result = SugarCRM.connection.get_document_revision(doc.document_revision_id) File.open(result['document_revision']['filename'], 'w') do |f| f.write(SugarCRM.connection.b64_decode(result['document_revision']['file'])) }
doc = SugarCRM::Document.new doc.active_date = Date.today doc.id = doc.save! account.documents << doc account.save!
file = File.read(File.join(File.dirname(FILE),"test_excel.xls")) revision_number = 1 SugarCRM.connection.set_document_revision(doc.id, revision_number, { filename: "my_file.xls", file: file })
SugarCRM::Module.find("Accounts").fields
SugarCRM::Module.find("Accounts").link_fields
SugarCRM.connection.get_entry("Users", 1)
SugarCRM.connection.get_entry_list(
"Users",
"users.user_name = \'sarah\'",
{
:link_fields => [
{
"name" => "accounts",
"value" => ["id", "name"]
}
]
}
)
== USING THE GEM WITH RAILS 3
Note: this gem works with Active Support >= 2.3.10, but is optimized for Rails 3.
bundle install
rails g sugarcrm:config
config/sugarcrm.yml
to match your environmentExample apps:
== USING A CONFIGURATION FILE
If you want to use a configuration file instead of always specifying the url, username, and password to connect to SugarCRM, you can add your credentials to one (or more) of
/etc/sugarcrm.yaml
~/.sugarcrm.yaml
(i.e. your home directory on Linux and Mac OSX)sugarcrm.yaml
file at the root of you Windows home directory (execute ENV['USERPROFILE']
in Ruby to see which directory should contain the file)config/sugarcrm.yaml
(will need to be copied each time you upgrade or reinstall the gem)SugarCRM.load_config
followed by the absolute path to your configuration fileIf there are several configuration files, they are loaded sequentially in the order above and will overwrite previous values (if present). This allows you to (e.g.) have a config file in /etc/sugarcrm.yaml
with system-wide configuration information (such as the url where SugarCRM is located) and/or defaults. Each developer/user can then have his personal configuration file in ~/.sugarcrm.yaml
with his own username and password. A developer could also specify a different location for the SugarCRM instance (e.g. a local testing instance) in his configuration file, which will take precedence over the value in /etc/sugarcrm.yaml
.
Your configuration should be in YAML format:
config:
base_url: http://127.0.0.1/sugarcrm
username: admin
password: letmein
An example, accompanied by instructions, can be found in the config/sugarcrm.yaml
file. In addition, a working example used for testing can be found in test/config_test.yaml
== USING THE GEM IN A CONSOLE
Type irb
in your command prompt
Require the gem with require 'sugarcrm'
(Note: Windows users might need to require 'rubygems'
before require 'sugarcrm'
.)
config/sugarcrm.yaml
file, you have been automagically logged in already ;SugarCRM.load_config
followed by the absolute path to your config file. This will log you in automatically ;SugarCRM.connect
and give it the proper arguments (see documentation above)You now have full access to the gem's functionality, e.g. puts SugarCRM::Account.first.name
If you make changes on the SugarCRM server (e.g. adding a field to a module), you can call SugarCRM.reload!
to rebuild the gem's modules and gain access to the new fields
== EXTENDING THE GEM
If you want to extend the gem's capabilities (e.g. to add methods specific to your environment), you can either
drop your *.rb
files in lib/sugarcrm/extensions/
(see the README in that folder)
drop your *.rb
files in any other folder and call SugarCRM.extensions_folder =
followed by the absolute path to the folder containing your extensions
== WORKING WITH SIMULTANEOUS SESSIONS
This gem allows you to work with several SugarCRM session simultaneously: on each SugarCRM.connect
call, a namespace is returned. Make sure you do NOT store this namespace in a reserved name (such as SugarCRM). This namespace can then be used just like you would use the SugarCRM
module. For example:
ServerOne = SugarCRM.connect(URL1,...)
ServerOne::User.first
ServerTwo = SugarCRM.connect(URL2,...)
ServerTwo::User.first
If you have only one active session, calls to SugarCRM are delegated to the active session's namespace, like so
ServerOne = SugarCRM.connect(...)
ServerOne::User.first # this call does
SugarCRM::User.first # the exact same thing as this one
To replace your session to connect with different credentials, use
ServerOne.reconnect(...)
Then your session will be reused (SugarCRM modules will be reloaded).
To disconnect an active session:
ServerOne.disconnect!
== REQUIREMENTS:
== INSTALL:
Note: Windows users might need to require 'rubygems'
before require 'sugarcrm'
.
== TEST:
Put your credentials in a file called test/config.yaml
(which you will have to create). These must point to a SugarCRM test instance with demo data. See an example file in test/config_test.yaml
(leave that file as is).
== Note on Patches/Pull Requests
== Copyright
Copyright (c) 2011 Carl Hicks. See LICENSE for details.