Class: SugarCRM::Connection
- Inherits:
-
Object
- Object
- SugarCRM::Connection
- Defined in:
- lib/sugarcrm/connection.rb,
lib/sugarcrm/connection/helper.rb,
lib/sugarcrm/connection/api/login.rb,
lib/sugarcrm/connection/api/logout.rb,
lib/sugarcrm/connection/api/get_entry.rb,
lib/sugarcrm/connection/api/set_entry.rb,
lib/sugarcrm/connection/api/get_entries.rb,
lib/sugarcrm/connection/api/get_user_id.rb,
lib/sugarcrm/connection/api/set_entries.rb,
lib/sugarcrm/connection/api/get_entry_list.rb,
lib/sugarcrm/connection/api/seamless_login.rb,
lib/sugarcrm/connection/api/get_server_info.rb,
lib/sugarcrm/connection/api/get_user_team_id.rb,
lib/sugarcrm/connection/api/search_by_module.rb,
lib/sugarcrm/connection/api/set_relationship.rb,
lib/sugarcrm/connection/api/get_entries_count.rb,
lib/sugarcrm/connection/api/get_module_fields.rb,
lib/sugarcrm/connection/api/get_relationships.rb,
lib/sugarcrm/connection/api/set_relationships.rb,
lib/sugarcrm/connection/api/get_report_entries.rb,
lib/sugarcrm/connection/api/set_campaign_merge.rb,
lib/sugarcrm/connection/api/get_note_attachment.rb,
lib/sugarcrm/connection/api/set_note_attachment.rb,
lib/sugarcrm/connection/api/get_available_modules.rb,
lib/sugarcrm/connection/api/get_document_revision.rb,
lib/sugarcrm/connection/api/set_document_revision.rb
Constant Summary collapse
- URL =
"/service/v2/rest.php"
- DONT_SHOW_DEBUG_FOR =
[]
- RESPONSE_IS_NOT_JSON =
[:get_user_id, :get_user_team_id]
Instance Attribute Summary collapse
-
#connection ⇒ Object
Returns the value of attribute connection.
-
#options ⇒ Object
Returns the value of attribute options.
-
#pass ⇒ Object
readonly
Returns the value of attribute pass.
-
#request ⇒ Object
Returns the value of attribute request.
-
#response ⇒ Object
Returns the value of attribute response.
-
#session ⇒ Object
Returns the value of attribute session.
-
#url ⇒ Object
Returns the value of attribute url.
-
#user ⇒ Object
readonly
Returns the value of attribute user.
Instance Method Summary collapse
-
#connect! ⇒ Object
Connect.
-
#connected? ⇒ Boolean
Check to see if we are connected.
- #debug=(debug) ⇒ Object
-
#get_available_modules ⇒ Object
(also: #get_modules)
Retrieves the list of modules available to the current user logged into the system.
-
#get_document_revision(id) ⇒ Object
Downloads a particular revision of a document.
-
#get_entries(module_name, ids, opts = {}) ⇒ Object
work with the report module.
-
#get_entries_count(module_name, query, opts = {}) ⇒ Object
Retrieves the specified number of records in a module.
-
#get_entry(module_name, id, opts = {}) ⇒ Object
Retrieves a single SugarBean based on the ID.
-
#get_entry_list(module_name, query, opts = {}) ⇒ Object
a list of SugarBeans using the REST API.
-
#get_module_fields(module_name) ⇒ Object
(also: #get_fields)
Retrieves the vardef information on fields of the specified bean.
-
#get_note_attachment(id) ⇒ Object
Retrieves an attachment from a note.
-
#get_relationships(module_name, id, related_to, opts = {}) ⇒ Object
(also: #get_relationship)
relationship data.
-
#get_report_entries(ids, options = {}) ⇒ Object
Retrieves a list of report entries based on specified report IDs.
-
#get_server_info ⇒ Object
Returns server information such as version, flavor, and gmt_time.
-
#get_user_id ⇒ Object
Returns the ID of the user who is logged into the current session.
-
#get_user_team_id ⇒ Object
Retrieves the ID of the default team of the user who is logged into the current session.
-
#initialize(url, user, pass, options = {}) ⇒ Connection
constructor
This is the singleton connection class.
-
#logged_in? ⇒ Boolean
Check to see if we are logged in.
-
#login ⇒ Object
Logs the user into the Sugar application.
-
#login! ⇒ Object
Login.
-
#logout ⇒ Object
Logs out of the Sugar user session.
- #resolve_fields(module_name, fields) ⇒ Object
-
#seamless_login ⇒ Object
Performs a seamless login during synchronization.
-
#search_by_module(search_string, modules, options = {}) ⇒ Object
Returns the ID, module name and fields for specified modules.
-
#send!(method, json) ⇒ Object
Send a GET request to the Sugar Instance.
-
#set_campaign_merge(targets, campaign_id) ⇒ Object
Performs a mail merge for the specified campaign.
-
#set_document_revision(id, revision) ⇒ Object
Sets a new revision for a document.
-
#set_entries(module_name, name_value_lists) ⇒ Object
Creates or updates a list of SugarBeans.
-
#set_entry(module_name, name_value_list) ⇒ Object
Creates or updates a single SugarBean.
-
#set_relationship(module_name, module_id, link_field_name, related_ids) ⇒ Object
Sets a single relationship between two SugarBeans.
-
#set_relationships(module_names, module_ids, link_field_names, related_ids) ⇒ Object
Sets multiple relationships between two SugarBeans.
Constructor Details
#initialize(url, user, pass, options = {}) ⇒ Connection
This is the singleton connection class.
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/sugarcrm/connection.rb', line 27 def initialize(url, user, pass, ={}) @options = { :debug => false, :register_modules => true }.merge() @url = URI.parse(url) @user = user @pass = pass @request = "" @response = "" resolve_url login! self end |
Instance Attribute Details
#connection ⇒ Object
Returns the value of attribute connection.
21 22 23 |
# File 'lib/sugarcrm/connection.rb', line 21 def connection @connection end |
#options ⇒ Object
Returns the value of attribute options.
22 23 24 |
# File 'lib/sugarcrm/connection.rb', line 22 def @options end |
#pass ⇒ Object (readonly)
Returns the value of attribute pass.
19 20 21 |
# File 'lib/sugarcrm/connection.rb', line 19 def pass @pass end |
#request ⇒ Object
Returns the value of attribute request.
23 24 25 |
# File 'lib/sugarcrm/connection.rb', line 23 def request @request end |
#response ⇒ Object
Returns the value of attribute response.
24 25 26 |
# File 'lib/sugarcrm/connection.rb', line 24 def response @response end |
#session ⇒ Object
Returns the value of attribute session.
20 21 22 |
# File 'lib/sugarcrm/connection.rb', line 20 def session @session end |
#url ⇒ Object
Returns the value of attribute url.
17 18 19 |
# File 'lib/sugarcrm/connection.rb', line 17 def url @url end |
#user ⇒ Object (readonly)
Returns the value of attribute user.
18 19 20 |
# File 'lib/sugarcrm/connection.rb', line 18 def user @user end |
Instance Method Details
#connect! ⇒ Object
Connect
66 67 68 69 70 71 72 73 |
# File 'lib/sugarcrm/connection.rb', line 66 def connect! @connection = Net::HTTP.new(@url.host, @url.port) if @url.scheme == "https" @connection.use_ssl = true @connection.verify_mode = OpenSSL::SSL::VERIFY_NONE end @connection.start end |
#connected? ⇒ Boolean
Check to see if we are connected
59 60 61 62 63 |
# File 'lib/sugarcrm/connection.rb', line 59 def connected? return false unless @connection return false unless @connection.started? true end |
#debug=(debug) ⇒ Object
86 87 88 |
# File 'lib/sugarcrm/connection.rb', line 86 def debug=(debug) [:debug] = debug end |
#get_available_modules ⇒ Object Also known as: get_modules
Retrieves the list of modules available to the current user logged into the system.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/sugarcrm/connection/api/get_available_modules.rb', line 3 def get_available_modules login! unless logged_in? json = <<-EOF { \"session\": \"#{@session}\" } EOF json.gsub!(/^\s{6}/,'') mods = send!(:get_available_modules, json)["modules"] modules = [] mods.each do |mod| modules << Module.new(mod) end modules end |
#get_document_revision(id) ⇒ Object
Downloads a particular revision of a document.
3 4 5 6 7 8 9 10 11 12 13 14 |
# File 'lib/sugarcrm/connection/api/get_document_revision.rb', line 3 def get_document_revision(id) login! unless logged_in? json = <<-EOF { \"session\": \"#{@session}\"\, \"id\": #{id} } EOF json.gsub!(/^\s{6}/,'') SugarCRM::Response.handle(send!(:get_document_revision, json)) #send!(:get_document_revision, json) end |
#get_entries(module_name, ids, opts = {}) ⇒ Object
work with the report module.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/sugarcrm/connection/api/get_entries.rb', line 4 def get_entries(module_name, ids, opts={}) login! unless logged_in? = { :fields => [], :link_fields => [], }.merge! opts json = <<-EOF { \"session\": \"#{@session}\"\, \"module_name\": \"#{module_name}\"\, \"ids\": #{ids.to_json}\, \"select_fields\": #{resolve_fields(module_name, [:fields])}\, \"link_name_to_fields_array\": #{[:link_fields].to_json}\, } EOF json.gsub!(/^\s{6}/,'') SugarCRM::Response.handle(send!(:get_entries, json)) end |
#get_entries_count(module_name, query, opts = {}) ⇒ Object
Retrieves the specified number of records in a module.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/sugarcrm/connection/api/get_entries_count.rb', line 3 def get_entries_count(module_name, query, opts={}) login! unless logged_in? = {:deleted => 0}.merge! opts json = <<-EOF { \"session\": \"#{@session}\"\, \"module_name\": \"#{module_name}\"\, \"query\": \"#{query}\"\, \"deleted\": #{[:deleted]} } EOF json.gsub!(/^\s{6}/,'') send!(:get_entries_count, json) end |
#get_entry(module_name, id, opts = {}) ⇒ Object
Retrieves a single SugarBean based on the ID.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/sugarcrm/connection/api/get_entry.rb', line 3 def get_entry(module_name, id, opts={}) login! unless logged_in? = { :fields => [], :link_fields => [], }.merge! opts json = <<-EOF { \"session\": \"#{@session}\"\, \"module_name\": \"#{module_name}\"\, \"id\": \"#{id}\"\, \"select_fields\": #{resolve_fields(module_name, [:fields])}\, \"link_name_to_fields_array\": #{[:link_fields]}\, } EOF json.gsub!(/^\s{6}/,'') SugarCRM::Response.handle(send!(:get_entry, json)) end |
#get_entry_list(module_name, query, opts = {}) ⇒ Object
a list of SugarBeans using the REST API.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/sugarcrm/connection/api/get_entry_list.rb', line 4 def get_entry_list(module_name, query, opts={}) login! unless logged_in? = { :order_by => '', :offset => '', :fields => [], :link_fields => [], :max_results => '', :deleted => '' }.merge! opts json = <<-EOF { \"session\": \"#{@session}\"\, \"module_name\": \"#{module_name}\"\, \"query\": \"#{query}\"\, \"order_by\": \"#{[:order_by]}\"\, \"offset\": \"#{[:offset]}\"\, \"select_fields\": #{resolve_fields(module_name, [:fields])}\, \"link_name_to_fields_array\": #{[:link_fields].to_json}\, \"max_results\": \"#{[:max_results]}\"\, \"deleted\": #{[:deleted]} } EOF json.gsub!(/^\s{6}/,'') SugarCRM::Response.handle(send!(:get_entry_list, json)) end |
#get_module_fields(module_name) ⇒ Object Also known as: get_fields
Retrieves the vardef information on fields of the specified bean.
4 5 6 7 8 9 10 11 12 13 14 15 |
# File 'lib/sugarcrm/connection/api/get_module_fields.rb', line 4 def get_module_fields(module_name) login! unless logged_in? json = <<-"EOF" { \"session\": \"#{@session}\"\, \"module_name": \"#{module_name}" } EOF json.gsub!(/^\s{6}/,'') SugarCRM::Response.handle(send!(:get_module_fields, json)) #send!(:get_module_fields, json) end |
#get_note_attachment(id) ⇒ Object
Retrieves an attachment from a note.
3 4 5 6 7 8 9 10 11 12 13 14 |
# File 'lib/sugarcrm/connection/api/get_note_attachment.rb', line 3 def (id) login! unless logged_in? json = <<-EOF { \"session\": \"#{@session}\"\, \"id\": #{id}\ } EOF json.gsub!(/^\s{6}/,'') send!(:get_note_attachment, json) end |
#get_relationships(module_name, id, related_to, opts = {}) ⇒ Object Also known as: get_relationship
relationship data
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/sugarcrm/connection/api/get_relationships.rb', line 5 def get_relationships(module_name, id, , opts={}) login! unless logged_in? = { :query => '', :fields => [], :link_fields => [], :deleted => '' }.merge! opts = .classify json = <<-EOF { \"session\": \"#{@session}\"\, \"module_name\": \"#{module_name}\"\, \"module_id\": \"#{id}\"\, \"link_field_name\": \"#{.downcase}\"\, \"related_module_query\": \"#{[:query]}\"\, \"related_fields\": #{resolve_fields(, [:fields])}\, \"related_module_link_name_to_fields_array\": #{[:link_fields].to_json}\, \"deleted\": #{[:deleted]} } EOF json.gsub!(/^\s{6}/,'') SugarCRM::Response.new(send!(:get_relationships, json)).to_obj end |
#get_report_entries(ids, options = {}) ⇒ Object
Retrieves a list of report entries based on specified report IDs.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/sugarcrm/connection/api/get_report_entries.rb', line 3 def get_report_entries(ids, ={}) login! unless logged_in? { :select_fields => '', }.merge! json = <<-EOF { \"session\": \"#{@session}\"\, \"ids\": #{ids.to_json}\, \"select_fields\": \"#{select_fields}\" } EOF json.gsub!(/^\s{6}/,'') send!(:get_report_entries, json) end |
#get_server_info ⇒ Object
Returns server information such as version, flavor, and gmt_time.
3 4 5 6 |
# File 'lib/sugarcrm/connection/api/get_server_info.rb', line 3 def get_server_info login! unless logged_in? Response.handle(send!(:get_server_info, "")) end |
#get_user_id ⇒ Object
Returns the ID of the user who is logged into the current session.
3 4 5 6 7 8 9 10 11 12 |
# File 'lib/sugarcrm/connection/api/get_user_id.rb', line 3 def get_user_id login! unless logged_in? json = <<-EOF { \"session\": \"#{@session}\" } EOF json.gsub!(/^\s{6}/,'') send!(:get_user_id, json) end |
#get_user_team_id ⇒ Object
Retrieves the ID of the default team of the user who is logged into the current session.
4 5 6 7 8 9 10 11 12 13 |
# File 'lib/sugarcrm/connection/api/get_user_team_id.rb', line 4 def get_user_team_id login! unless logged_in? json = <<-EOF { \"session\": \"#{@session}\" } EOF json.gsub!(/^\s{6}/,'') send!(:get_user_team_id, json) end |
#logged_in? ⇒ Boolean
Check to see if we are logged in
45 46 47 |
# File 'lib/sugarcrm/connection.rb', line 45 def logged_in? @session ? true : false end |
#login ⇒ Object
Logs the user into the Sugar application.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/sugarcrm/connection/api/login.rb', line 3 def login connect! unless connected? json = <<-EOF { \"user_auth\": { \"user_name\": \"#{@user}\"\, \"password\": \"#{OpenSSL::Digest::MD5.new(@pass)}\"\, \"version\": \"2\"\, }, \"application\": \"\" } EOF json.gsub!(/^\s{8}/,'') response = send!(:login, json) end |
#login! ⇒ Object
Login
50 51 52 53 54 55 56 |
# File 'lib/sugarcrm/connection.rb', line 50 def login! @session = login["id"] raise SugarCRM::LoginError, "Invalid Login" unless logged_in? SugarCRM.connection = self SugarCRM::Base.connection = self Module.register_all if @options[:register_modules] end |
#logout ⇒ Object
Logs out of the Sugar user session.
3 4 5 6 7 8 9 10 11 12 13 14 |
# File 'lib/sugarcrm/connection/api/logout.rb', line 3 def logout login! unless logged_in? json = <<-EOF { \"user_auth\": { \"session\": \"#{@session}\" } } EOF json.gsub!(/^\s{8}/,'') send!(:logout, json) end |
#resolve_fields(module_name, fields) ⇒ Object
2 3 4 5 6 7 8 9 |
# File 'lib/sugarcrm/connection/helper.rb', line 2 def resolve_fields(module_name, fields) # FIXME: This is to work around a bug in SugarCRM 6.0 # where no fields are returned if no fields are specified if fields.length == 0 fields = Module.find(module_name).fields.keys end return fields.to_json end |
#seamless_login ⇒ Object
Performs a seamless login during synchronization.
3 4 5 6 7 8 9 10 11 12 |
# File 'lib/sugarcrm/connection/api/seamless_login.rb', line 3 def seamless_login login! unless logged_in? json = <<-EOF { \"session\": \"#{@session}\" } EOF json.gsub!(/^\s{8}/,'') response = send!(:seamless_login, json) end |
#search_by_module(search_string, modules, options = {}) ⇒ Object
Returns the ID, module name and fields for specified modules. Supported modules are Accounts, Bugs, Calls, Cases, Contacts, Leads, Opportunities, Projects, Project Tasks, and Quotes.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/sugarcrm/connection/api/search_by_module.rb', line 5 def search_by_module(search_string, modules, ={}) login! unless logged_in? { :offset => nil, :max_results => nil, }.merge! json = <<-EOF { \"session\": \"#{@session}\"\, \"search_string\": \"#{search_string}\"\, \"modules\": \"#{modules}\"\, \"offset\": #{[:offset]}\, \"max_results\": #{[:max_results]} } EOF json.gsub!(/^\s{6}/,'') send!(:search_by_module, json) end |
#send!(method, json) ⇒ Object
Send a GET request to the Sugar Instance
76 77 78 79 80 81 82 83 84 |
# File 'lib/sugarcrm/connection.rb', line 76 def send!(method, json) @request = SugarCRM::Request.new(@url, method, json, @options[:debug]) if @request.length > 3900 @response = @connection.post(@url.path, @request) else @response = @connection.get(@url.path.dup + "?" + @request.to_s) end handle_response end |
#set_campaign_merge(targets, campaign_id) ⇒ Object
Performs a mail merge for the specified campaign.
3 4 5 6 7 8 9 10 11 12 13 14 |
# File 'lib/sugarcrm/connection/api/set_campaign_merge.rb', line 3 def set_campaign_merge(targets, campaign_id) login! unless logged_in? json = <<-EOF { \"session\": \"#{@session}\"\, \"targets\": #{targets.to_json}\, \"campaign-id\": \"#{campaign_id}\" } EOF json.gsub!(/^\s{6}/,'') send!(:set_campaign_merge, json) end |
#set_document_revision(id, revision) ⇒ Object
Sets a new revision for a document.
3 4 5 6 7 8 9 10 11 12 13 14 |
# File 'lib/sugarcrm/connection/api/set_document_revision.rb', line 3 def set_document_revision(id, revision) login! unless logged_in? json = <<-EOF { \"session\": \"#{@session}\"\, \"document_revision\": \"#{revision}\"\, \"id\": #{id} } EOF json.gsub!(/^\s{6}/,'') send!(:set_document_revision, json) end |
#set_entries(module_name, name_value_lists) ⇒ Object
Creates or updates a list of SugarBeans.
3 4 5 6 7 8 9 10 11 12 13 14 |
# File 'lib/sugarcrm/connection/api/set_entries.rb', line 3 def set_entries(module_name, name_value_lists) login! unless logged_in? json = <<-EOF { \"session\": \"#{@session}\"\, \"module_name\": \"#{module_name}\"\, \"name_value_list\": #{name_value_lists.to_json} } EOF json.gsub!(/^\s{6}/,'') send!(:set_entries, json) end |
#set_entry(module_name, name_value_list) ⇒ Object
Creates or updates a single SugarBean.
3 4 5 6 7 8 9 10 11 12 13 14 |
# File 'lib/sugarcrm/connection/api/set_entry.rb', line 3 def set_entry(module_name, name_value_list) login! unless logged_in? json = <<-EOF { \"session\": \"#{@session}\"\, \"module_name\": \"#{module_name}\"\, \"name_value_list\": #{name_value_list.to_json} } EOF json.gsub!(/^\s{6}/,'') send!(:set_entry, json) end |
#set_relationship(module_name, module_id, link_field_name, related_ids) ⇒ Object
Sets a single relationship between two SugarBeans.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/sugarcrm/connection/api/set_relationship.rb', line 3 def set_relationship(module_name, module_id, link_field_name, ) login! unless logged_in? raise ArgumentError, "related_ids must be an Array" unless .class == Array json = <<-EOF { \"session\": \"#{@session}\"\, \"module_name\": \"#{module_name}\"\, \"module_id\": #{module_id}\, \"link_field_name\": #{link_field_name}\, \"related_ids\": #{link_field_name.to_json} } EOF json.gsub!(/^\s{6}/,'') send!(:set_relationship, json) end |
#set_relationships(module_names, module_ids, link_field_names, related_ids) ⇒ Object
Sets multiple relationships between two SugarBeans.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/sugarcrm/connection/api/set_relationships.rb', line 3 def set_relationships(module_names, module_ids, link_field_names, ) login! unless logged_in? [module_names, module_ids, link_field_names, ].each do |arg| raise ArgumentError, "argument must be an Array" unless arg.class == Array end json = <<-EOF { \"session\": \"#{@session}\"\, \"module_names\": \"#{module_names.to_json}\"\, \"module_ids\": #{module_ids.to_json}\, \"link_field_names\": #{link_field_names.to_json}\, \"related_ids\": #{link_field_name.to_json} } EOF json.gsub!(/^\s{6}/,'') send!(:set_relationships, json) end |