sugarcrm 0.6.2 → 0.7.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (59) hide show
  1. data/README.rdoc +13 -8
  2. data/Rakefile +1 -1
  3. data/VERSION +1 -1
  4. data/lib/sugarcrm.rb +7 -139
  5. data/lib/sugarcrm/association_methods.rb +45 -0
  6. data/lib/sugarcrm/attribute_methods.rb +65 -0
  7. data/lib/sugarcrm/base.rb +85 -0
  8. data/lib/sugarcrm/connection.rb +126 -103
  9. data/lib/sugarcrm/{api → connection/api}/get_available_modules.rb +6 -1
  10. data/lib/sugarcrm/{api → connection/api}/get_document_revision.rb +2 -1
  11. data/lib/sugarcrm/{api → connection/api}/get_entries.rb +5 -6
  12. data/lib/sugarcrm/{api → connection/api}/get_entries_count.rb +3 -5
  13. data/lib/sugarcrm/{api → connection/api}/get_entry.rb +6 -6
  14. data/lib/sugarcrm/{api → connection/api}/get_entry_list.rb +6 -6
  15. data/lib/sugarcrm/{api → connection/api}/get_module_fields.rb +2 -1
  16. data/lib/sugarcrm/{api → connection/api}/get_note_attachment.rb +1 -1
  17. data/lib/sugarcrm/connection/api/get_relationships.rb +34 -0
  18. data/lib/sugarcrm/{api → connection/api}/get_report_entries.rb +1 -1
  19. data/lib/sugarcrm/{api → connection/api}/get_server_info.rb +1 -1
  20. data/lib/sugarcrm/{api → connection/api}/get_user_id.rb +2 -2
  21. data/lib/sugarcrm/{api → connection/api}/get_user_team_id.rb +2 -2
  22. data/lib/sugarcrm/{api → connection/api}/login.rb +1 -1
  23. data/lib/sugarcrm/{api → connection/api}/logout.rb +1 -1
  24. data/lib/sugarcrm/{api → connection/api}/seamless_login.rb +1 -1
  25. data/lib/sugarcrm/{api → connection/api}/search_by_module.rb +1 -1
  26. data/lib/sugarcrm/{api → connection/api}/set_campaign_merge.rb +1 -1
  27. data/lib/sugarcrm/{api → connection/api}/set_document_revision.rb +1 -1
  28. data/lib/sugarcrm/{api → connection/api}/set_entries.rb +1 -1
  29. data/lib/sugarcrm/{api → connection/api}/set_entry.rb +1 -1
  30. data/lib/sugarcrm/{api → connection/api}/set_note_attachment.rb +0 -0
  31. data/lib/sugarcrm/{api → connection/api}/set_relationship.rb +1 -1
  32. data/lib/sugarcrm/{api → connection/api}/set_relationships.rb +1 -1
  33. data/lib/sugarcrm/connection/helper.rb +10 -0
  34. data/lib/sugarcrm/exceptions.rb +6 -0
  35. data/lib/sugarcrm/module.rb +105 -6
  36. data/lib/sugarcrm/module_methods.rb +18 -0
  37. data/lib/sugarcrm/request.rb +13 -3
  38. data/lib/sugarcrm/response.rb +75 -25
  39. data/test/connection/test_get_available_modules.rb +12 -0
  40. data/test/connection/test_get_entries.rb +21 -0
  41. data/test/connection/test_get_entry.rb +20 -0
  42. data/test/connection/test_get_entry_list.rb +28 -0
  43. data/test/connection/test_get_module_fields.rb +14 -0
  44. data/test/connection/test_get_relationships.rb +15 -0
  45. data/test/connection/test_get_server_info.rb +12 -0
  46. data/test/connection/test_get_user_id.rb +12 -0
  47. data/test/connection/test_get_user_team_id.rb +12 -0
  48. data/test/connection/test_login.rb +12 -0
  49. data/test/connection/test_logout.rb +12 -0
  50. data/test/helper.rb +4 -1
  51. data/test/test_connection.rb +12 -48
  52. data/test/test_module.rb +14 -0
  53. data/test/test_response.rb +5 -14
  54. data/test/test_sugarcrm.rb +16 -14
  55. metadata +60 -34
  56. data/lib/sugarcrm/api/get_relationship.rb +0 -25
  57. data/lib/sugarcrm/core_ext/attribute.rb +0 -67
  58. data/lib/sugarcrm/core_ext/remove_method.rb +0 -6
  59. data/lib/sugarcrm/core_ext/singleton_class.rb +0 -13
@@ -9,7 +9,12 @@ module SugarCRM; class Connection
9
9
  EOF
10
10
 
11
11
  json.gsub!(/^\s{6}/,'')
12
- get(:get_available_modules, json)["modules"]
12
+ mods = send!(:get_available_modules, json)["modules"]
13
+ modules = []
14
+ mods.each do |mod|
15
+ modules << Module.new(mod)
16
+ end
17
+ modules
13
18
  end
14
19
 
15
20
  alias :get_modules :get_available_modules
@@ -9,6 +9,7 @@ module SugarCRM; class Connection
9
9
  }
10
10
  EOF
11
11
  json.gsub!(/^\s{6}/,'')
12
- get(:get_document_revision, json)
12
+ SugarCRM::Response.handle(send!(:get_document_revision, json))
13
+ #send!(:get_document_revision, json)
13
14
  end
14
15
  end; end
@@ -1,23 +1,22 @@
1
1
  module SugarCRM; class Connection
2
2
  # Retrieve a list of SugarBeans by ID. This method will not
3
3
  # work with the report module.
4
- def get_entries(module_name, ids, options={})
4
+ def get_entries(module_name, ids, opts={})
5
5
  login! unless logged_in?
6
- {
7
- :fields => '',
6
+ options = { :fields => [],
8
7
  :link_fields => [],
9
- }.merge! options
8
+ }.merge! opts
10
9
 
11
10
  json = <<-EOF
12
11
  {
13
12
  \"session\": \"#{@session}\"\,
14
13
  \"module_name\": \"#{module_name}\"\,
15
14
  \"ids\": #{ids.to_json}\,
16
- \"select_fields\": #{options[:fields].to_json}\,
15
+ \"select_fields\": #{resolve_fields(module_name, options[:fields])}\,
17
16
  \"link_name_to_fields_array\": #{options[:link_fields].to_json}\,
18
17
  }
19
18
  EOF
20
19
  json.gsub!(/^\s{6}/,'')
21
- get(:get_entries, json)
20
+ SugarCRM::Response.handle(send!(:get_entries, json))
22
21
  end
23
22
  end; end
@@ -1,10 +1,8 @@
1
1
  module SugarCRM; class Connection
2
2
  # Retrieves the specified number of records in a module.
3
- def get_entries_count(module_name, query, options={})
3
+ def get_entries_count(module_name, query, opts={})
4
4
  login! unless logged_in?
5
- {
6
- :deleted => 0,
7
- }.merge! options
5
+ options = {:deleted => 0}.merge! opts
8
6
 
9
7
  json = <<-EOF
10
8
  {
@@ -15,6 +13,6 @@ def get_entries_count(module_name, query, options={})
15
13
  }
16
14
  EOF
17
15
  json.gsub!(/^\s{6}/,'')
18
- get(:get_entries_count, json)
16
+ send!(:get_entries_count, json)
19
17
  end
20
18
  end; end
@@ -1,22 +1,22 @@
1
1
  module SugarCRM; class Connection
2
2
  # Retrieves a single SugarBean based on the ID.
3
- def get_entry(module_name, id, options={})
3
+ def get_entry(module_name, id, opts={})
4
4
  login! unless logged_in?
5
- { :fields => [],
5
+ options = { :fields => [],
6
6
  :link_fields => [],
7
- }.merge! options
8
-
7
+ }.merge! opts
8
+
9
9
  json = <<-EOF
10
10
  {
11
11
  \"session\": \"#{@session}\"\,
12
12
  \"module_name\": \"#{module_name}\"\,
13
13
  \"id\": \"#{id}\"\,
14
- \"select_fields\": #{options[:fields].to_json}\,
14
+ \"select_fields\": #{resolve_fields(module_name, options[:fields])}\,
15
15
  \"link_name_to_fields_array\": #{options[:link_fields]}\,
16
16
  }
17
17
  EOF
18
18
 
19
19
  json.gsub!(/^\s{6}/,'')
20
- SugarCRM::Response.new(get(:get_entry, json))
20
+ SugarCRM::Response.handle(send!(:get_entry, json))
21
21
  end
22
22
  end; end
@@ -1,16 +1,16 @@
1
1
  module SugarCRM; class Connection
2
2
  # Retrieve a list of SugarBeans. This is the primary method for getting
3
3
  # a list of SugarBeans using the REST API.
4
- def get_entry_list(module_name, query, options={})
4
+ def get_entry_list(module_name, query, opts={})
5
5
  login! unless logged_in?
6
- {
6
+ options = {
7
7
  :order_by => '',
8
8
  :offset => '',
9
- :fields => '',
9
+ :fields => [],
10
10
  :link_fields => [],
11
11
  :max_results => '',
12
12
  :deleted => ''
13
- }.merge! options
13
+ }.merge! opts
14
14
 
15
15
  json = <<-EOF
16
16
  {
@@ -19,13 +19,13 @@ def get_entry_list(module_name, query, options={})
19
19
  \"query\": \"#{query}\"\,
20
20
  \"order_by\": \"#{options[:order_by]}\"\,
21
21
  \"offset\": \"#{options[:offset]}\"\,
22
- \"select_fields\": #{options[:fields].to_json}\,
22
+ \"select_fields\": #{resolve_fields(module_name, options[:fields])}\,
23
23
  \"link_name_to_fields_array\": #{options[:link_fields].to_json}\,
24
24
  \"max_results\": \"#{options[:max_results]}\"\,
25
25
  \"deleted\": #{options[:deleted]}
26
26
  }
27
27
  EOF
28
28
  json.gsub!(/^\s{6}/,'')
29
- get(:get_entry_list, json)
29
+ SugarCRM::Response.handle(send!(:get_entry_list, json))
30
30
  end
31
31
  end; end
@@ -10,7 +10,8 @@ def get_module_fields(module_name)
10
10
  }
11
11
  EOF
12
12
  json.gsub!(/^\s{6}/,'')
13
- get(:get_module_fields, json)
13
+ SugarCRM::Response.handle(send!(:get_module_fields, json))
14
+ #send!(:get_module_fields, json)
14
15
  end
15
16
 
16
17
  alias :get_fields :get_module_fields
@@ -10,6 +10,6 @@ def get_note_attachment(id)
10
10
  }
11
11
  EOF
12
12
  json.gsub!(/^\s{6}/,'')
13
- get(:get_note_attachment, json)
13
+ send!(:get_note_attachment, json)
14
14
  end
15
15
  end; end
@@ -0,0 +1,34 @@
1
+ module SugarCRM; class Connection
2
+ # Retrieves a collection of beans that are related
3
+ # to the specified bean and, optionally, returns
4
+ # relationship data
5
+ def get_relationships(module_name, id, related_to, opts={})
6
+ login! unless logged_in?
7
+ options = {
8
+ :query => '',
9
+ :fields => [],
10
+ :link_fields => [],
11
+ :deleted => ''
12
+ }.merge! opts
13
+
14
+ related_module = related_to.classify
15
+
16
+ json = <<-EOF
17
+ {
18
+ \"session\": \"#{@session}\"\,
19
+ \"module_name\": \"#{module_name}\"\,
20
+ \"module_id\": \"#{id}\"\,
21
+ \"link_field_name\": \"#{related_to.downcase}\"\,
22
+ \"related_module_query\": \"#{options[:query]}\"\,
23
+ \"related_fields\": #{resolve_fields(related_module, options[:fields])}\,
24
+ \"related_module_link_name_to_fields_array\": #{options[:link_fields].to_json}\,
25
+ \"deleted\": #{options[:deleted]}
26
+ }
27
+ EOF
28
+ json.gsub!(/^\s{6}/,'')
29
+ SugarCRM::Response.new(send!(:get_relationships, json)).to_obj
30
+ end
31
+
32
+ alias :get_relationship :get_relationships
33
+
34
+ end; end
@@ -14,6 +14,6 @@ def get_report_entries(ids, options={})
14
14
  }
15
15
  EOF
16
16
  json.gsub!(/^\s{6}/,'')
17
- get(:get_report_entries, json)
17
+ send!(:get_report_entries, json)
18
18
  end
19
19
  end; end
@@ -2,6 +2,6 @@ module SugarCRM; class Connection
2
2
  # Returns server information such as version, flavor, and gmt_time.
3
3
  def get_server_info
4
4
  login! unless logged_in?
5
- connection.get(:get_server_info, "")
5
+ Response.handle(send!(:get_server_info, ""))
6
6
  end
7
7
  end; end
@@ -7,7 +7,7 @@ module SugarCRM; class Connection
7
7
  \"session\": \"#{@session}\"
8
8
  }
9
9
  EOF
10
- json.gsub!(/^\s{8}/,'')
11
- response = get(:get_user_id, json)
10
+ json.gsub!(/^\s{6}/,'')
11
+ send!(:get_user_id, json)
12
12
  end
13
13
  end; end
@@ -8,7 +8,7 @@ module SugarCRM; class Connection
8
8
  \"session\": \"#{@session}\"
9
9
  }
10
10
  EOF
11
- json.gsub!(/^\s{8}/,'')
12
- response = get(:get_user_team_id, json)
11
+ json.gsub!(/^\s{6}/,'')
12
+ send!(:get_user_team_id, json)
13
13
  end
14
14
  end; end
@@ -13,6 +13,6 @@ module SugarCRM; class Connection
13
13
  }
14
14
  EOF
15
15
  json.gsub!(/^\s{8}/,'')
16
- response = get(:login, json)
16
+ response = send!(:login, json)
17
17
  end
18
18
  end; end
@@ -10,6 +10,6 @@ module SugarCRM; class Connection
10
10
  }
11
11
  EOF
12
12
  json.gsub!(/^\s{8}/,'')
13
- response = get(:logout, json)
13
+ send!(:logout, json)
14
14
  end
15
15
  end; end
@@ -8,6 +8,6 @@ module SugarCRM; class Connection
8
8
  }
9
9
  EOF
10
10
  json.gsub!(/^\s{8}/,'')
11
- response = get(:seamless_login, json)
11
+ response = send!(:seamless_login, json)
12
12
  end
13
13
  end; end
@@ -19,6 +19,6 @@ module SugarCRM; class Connection
19
19
  }
20
20
  EOF
21
21
  json.gsub!(/^\s{6}/,'')
22
- get(:search_by_module, json)
22
+ send!(:search_by_module, json)
23
23
  end
24
24
  end; end
@@ -10,6 +10,6 @@ module SugarCRM; class Connection
10
10
  }
11
11
  EOF
12
12
  json.gsub!(/^\s{6}/,'')
13
- get(:set_campaign_merge, json)
13
+ send!(:set_campaign_merge, json)
14
14
  end
15
15
  end; end
@@ -10,6 +10,6 @@ module SugarCRM; class Connection
10
10
  }
11
11
  EOF
12
12
  json.gsub!(/^\s{6}/,'')
13
- get(:set_document_revision, json)
13
+ send!(:set_document_revision, json)
14
14
  end
15
15
  end; end
@@ -10,6 +10,6 @@ module SugarCRM; class Connection
10
10
  }
11
11
  EOF
12
12
  json.gsub!(/^\s{6}/,'')
13
- get(:set_entries, json)
13
+ send!(:set_entries, json)
14
14
  end
15
15
  end; end
@@ -10,6 +10,6 @@ module SugarCRM; class Connection
10
10
  }
11
11
  EOF
12
12
  json.gsub!(/^\s{6}/,'')
13
- get(:set_entry, json)
13
+ send!(:set_entry, json)
14
14
  end
15
15
  end; end
@@ -13,6 +13,6 @@ module SugarCRM; class Connection
13
13
  }
14
14
  EOF
15
15
  json.gsub!(/^\s{6}/,'')
16
- get(:set_relationship, json)
16
+ send!(:set_relationship, json)
17
17
  end
18
18
  end; end
@@ -17,6 +17,6 @@ module SugarCRM; class Connection
17
17
  }
18
18
  EOF
19
19
  json.gsub!(/^\s{6}/,'')
20
- get(:set_relationships, json)
20
+ send!(:set_relationships, json)
21
21
  end
22
22
  end; end
@@ -0,0 +1,10 @@
1
+ module SugarCRM; class Connection
2
+ def resolve_fields(module_name, fields)
3
+ # FIXME: This is to work around a bug in SugarCRM 6.0
4
+ # where no fields are returned if no fields are specified
5
+ if fields.length == 0
6
+ fields = Module.find(module_name).fields.keys
7
+ end
8
+ return fields.to_json
9
+ end
10
+ end; end
@@ -13,4 +13,10 @@ module SugarCRM
13
13
 
14
14
  class InvalidRequest < RuntimeError
15
15
  end
16
+
17
+ class InvalidModule <RuntimeError
18
+ end
19
+
20
+ class AttributeParsingError < RuntimeError
21
+ end
16
22
  end
@@ -1,11 +1,110 @@
1
1
  module SugarCRM
2
- class Base
3
-
4
- # Runs a find against the remote service
5
- def self.find(id)
6
- response = connection.get_entry(self.module_name, id,{:fields => self.module_fields.keys})
7
- response.object
2
+ # A class for handling SugarCRM Modules
3
+ class Module
4
+ attr :name, false
5
+ attr :klass, false
6
+ attr :fields, false
7
+ attr :link_fields, false
8
+
9
+ # Dynamically register objects based on Module name
10
+ # I.e. a SugarCRM Module named Users will generate
11
+ # a SugarCRM::User class.
12
+ def initialize(name)
13
+ @name = name
14
+ @klass = name.classify
15
+ @fields = {}
16
+ @link_fields = {}
17
+ @fields_registered = false
18
+ self
19
+ end
20
+
21
+ def fields
22
+ return @fields if fields?
23
+ all_fields = SugarCRM.connection.get_fields(@name)
24
+ @fields = all_fields["module_fields"]
25
+ @link_fields= all_fields["link_fields"]
26
+ @fields_registered = true
27
+ @fields
28
+ end
29
+
30
+ def fields?
31
+ @fields_registered
32
+ end
33
+
34
+ def link_fields
35
+ self.fields unless link_fields?
36
+ handle_empty_array
37
+ @link_fields
38
+ end
39
+
40
+ def link_fields?
41
+ @fields_registered
42
+ end
43
+
44
+ def handle_empty_array
45
+ if @link_fields.class == Array && @link_fields.length == 0
46
+ @link_fields = {}
47
+ end
48
+ end
49
+
50
+ # Registers a single module by name
51
+ # Adds module to SugarCRM.modules (SugarCRM.modules << Module.new("Users"))
52
+ # Adds module class to SugarCRM parent module (SugarCRM.constants << User)
53
+ # Note, SugarCRM::User.module == Module.find("Users")
54
+ def register
55
+ return self if registered?
56
+ mod_instance = self
57
+ # class Class < SugarCRM::Base
58
+ # module_name = "Accounts"
59
+ # end
60
+ klass = Class.new(SugarCRM::Base) do
61
+ self._module = mod_instance
62
+ end
63
+
64
+ # class Account < SugarCRM::Base
65
+ SugarCRM.const_set self.klass, klass
66
+ self
8
67
  end
9
68
 
69
+ def registered?
70
+ SugarCRM.const_defined? @klass
71
+ end
72
+
73
+ def to_s
74
+ @name
75
+ end
76
+
77
+ def to_class
78
+ SugarCRM.const_get(@klass).new
79
+ end
80
+
81
+ class << self
82
+ @initialized = false
83
+
84
+ # Registers all of the SugarCRM Modules
85
+ def register_all
86
+ SugarCRM.connection.get_modules.each do |m|
87
+ SugarCRM.modules << m.register
88
+ end
89
+ @initialized = true
90
+ true
91
+ end
92
+
93
+ # Finds a module by name, or klass name
94
+ def find(name)
95
+ register_all unless initialized?
96
+ SugarCRM.modules.each do |m|
97
+ return m if m.name == name
98
+ return m if m.klass == name
99
+ end
100
+ false
101
+ end
102
+
103
+ # Class variable to track if we've initialized or not
104
+ def initialized?
105
+ @initialized ||= false
106
+ end
107
+
108
+ end
10
109
  end
11
110
  end