sugarcrm 0.6.2 → 0.7.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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
@@ -0,0 +1,18 @@
1
+ module SugarCRM
2
+
3
+ @@connection = nil
4
+ def self.connection
5
+ @@connection
6
+ end
7
+ def self.connection=(connection)
8
+ @@connection = connection
9
+ end
10
+
11
+ @@modules = []
12
+ def self.modules
13
+ @@modules
14
+ end
15
+ def self.modules=(modules)
16
+ @@modules = modules
17
+ end
18
+ end
@@ -6,18 +6,28 @@ class Request
6
6
  attr :url, true
7
7
  attr :method, true
8
8
  attr :json, true
9
+ attr :http_method
9
10
 
10
11
  def initialize(url, method, json, debug=false)
11
- @request = url.path.dup
12
- @request << '?method=' << method.to_s
12
+ @url = url
13
+ @method = method
14
+ @json = json
15
+
16
+ @request = 'method=' << @method.to_s
13
17
  @request << '&input_type=JSON'
14
18
  @request << '&response_type=JSON'
15
- @request << '&rest_data=' << json
19
+ @request << '&rest_data=' << @json
16
20
 
17
21
  if debug
18
22
  puts "#{method}: Request:"
19
23
  pp @request
24
+ puts "\n"
20
25
  end
26
+ self
27
+ end
28
+
29
+ def length
30
+ self.to_s.length
21
31
  end
22
32
 
23
33
  def to_s
@@ -1,29 +1,79 @@
1
- module SugarCRM
2
- # takes a raw JSON response and turns it into a REAL object
3
- class Response
4
-
5
- attr :response, false
6
- attr :module, false
7
- attr :attributes, false
8
- attr :object, false
9
- attr :id, false
10
-
11
- def initialize(json)
12
- @response = json
13
- @module = @response["entry_list"][0]["module_name"].singularize
14
- @attributes = flatten(@response["entry_list"][0]["name_value_list"])
15
- @object = SugarCRM.const_get(@module).new(@attributes) if SugarCRM.const_get(@module)
1
+ module SugarCRM; class Response
2
+
3
+ class << self
4
+ # This class handles the response from the server.
5
+ # It tries to convert the response into an object such as User
6
+ # or an object collection. If it fails, it just returns the response hash
7
+ def handle(json)
8
+ r = new(json)
9
+ begin
10
+ return r.to_obj
11
+ rescue
12
+ return json
13
+ end
16
14
  end
17
-
18
- # Takes a hash like { "first_name" => {"name" => "first_name", "value" => "John"}}
19
- # And flattens it into {"first_name" => "John"}
20
- def flatten(list)
21
- raise ArgumentError, 'method parameter must respond to #each_pair' unless list.respond_to? :each_pair
22
- flat_list = {}
23
- list.each_pair do |k,v|
24
- flat_list[k.to_sym] = v["value"]
15
+ end
16
+
17
+ attr :response, false
18
+
19
+ def initialize(json)
20
+ @response = json
21
+ end
22
+
23
+ # Tries to instantiate and return an object with the values
24
+ # populated from the response
25
+ def to_obj
26
+ objects = []
27
+ @response["entry_list"].each do |object|
28
+ attributes = []
29
+ _module = resolve_module(object)
30
+ id = object["id"]
31
+ begin
32
+ attributes = flatten_name_value_list(object)
33
+ rescue ArgumentError => e
25
34
  end
26
- flat_list
35
+ if SugarCRM.const_get(_module)
36
+ if attributes.length == 0
37
+ pp object
38
+ raise AttributeParsingError, "response contains objects without attributes!"
39
+ end
40
+ objects << SugarCRM.const_get(_module).new(id, attributes)
41
+ else
42
+ raise InvalidModule, "#{_module} does not exist, or is not accessible"
43
+ end
44
+ end
45
+ # If we only have one result, just return the object
46
+ if objects.length == 1
47
+ return objects[0]
48
+ else
49
+ return objects
50
+ end
51
+ end
52
+
53
+ def to_json
54
+ @response.to_json
55
+ end
56
+
57
+ def resolve_module(list)
58
+ list["module_name"].classify
59
+ end
60
+
61
+ def flatten_name_value_list(list)
62
+ if list["name_value_list"]
63
+ return flatten(list["name_value_list"])
64
+ else
65
+ return false
66
+ end
67
+ end
68
+
69
+ # Takes a hash like { "first_name" => {"name" => "first_name", "value" => "John"}}
70
+ # And flattens it into {"first_name" => "John"}
71
+ def flatten(list)
72
+ raise ArgumentError, 'method parameter must respond to #each_pair' unless list.respond_to? :each_pair
73
+ flat_list = {}
74
+ list.each_pair do |k,v|
75
+ flat_list[k.to_sym] = v["value"]
27
76
  end
77
+ flat_list
28
78
  end
29
- end
79
+ end; end
@@ -0,0 +1,12 @@
1
+ require 'helper'
2
+
3
+ class TestGetAvailableModules < Test::Unit::TestCase
4
+ context "A SugarCRM.connection" do
5
+ setup do
6
+ SugarCRM::Connection.new(URL, USER, PASS)
7
+ end
8
+ should "return an array of modules when #get_modules" do
9
+ assert_instance_of SugarCRM::Module, SugarCRM.connection.get_modules[0]
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,21 @@
1
+ require 'helper'
2
+
3
+ class TestGetEntries < Test::Unit::TestCase
4
+ context "A SugarCRM.connection" do
5
+ setup do
6
+ SugarCRM::Connection.new(URL, USER, PASS, {:register_modules => true, :debug => false})
7
+ @response = SugarCRM.connection.get_entries(
8
+ "Users",
9
+ [1,"seed_sally_id"],
10
+ {:fields => ["first_name", "last_name"]}
11
+ )
12
+ end
13
+ # should "return a list of entries when sent #get_entries." do
14
+ # assert @response.response.key? "entry_list"
15
+ # end
16
+ should "return an object when #get_entries" do
17
+ assert_instance_of Array, @response
18
+ assert_instance_of SugarCRM::User, @response[0]
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,20 @@
1
+ require 'helper'
2
+
3
+ class TestGetEntry < Test::Unit::TestCase
4
+ context "A SugarCRM.connection" do
5
+ setup do
6
+ SugarCRM::Connection.new(URL, USER, PASS, {:debug => false })
7
+ @response = SugarCRM.connection.get_entry(
8
+ "Users",
9
+ 1,
10
+ {:fields => ["first_name", "last_name"]}
11
+ )
12
+ end
13
+ # should "return a single entry when sent #get_entry." do
14
+ # assert @response.response. "entry_list"
15
+ # end
16
+ should "return an object when #get_entry" do
17
+ assert_instance_of SugarCRM::User, @response
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,28 @@
1
+ require 'helper'
2
+
3
+ class TestGetEntryList < Test::Unit::TestCase
4
+ context "A SugarCRM.connection" do
5
+ setup do
6
+ SugarCRM::Connection.new(URL, USER, PASS, {:register_modules => true, :debug => false})
7
+ @response = SugarCRM.connection.get_entry_list(
8
+ "Users",
9
+ "users.deleted = 0",
10
+ {:fields => ["first_name", "last_name"]}
11
+ )
12
+ end
13
+ # should "return a list of entries when sent #get_entry_list." do
14
+ # assert @response.response.key? "entry_list"
15
+ # end
16
+ should "return a list of entries when sent #get_entry_list and no fields." do
17
+ users = SugarCRM.connection.get_entry_list(
18
+ "Users",
19
+ "users.deleted = 0"
20
+ )
21
+ assert_equal "Administrator", users.first.last_name
22
+ end
23
+ should "return an object when #get_entry_list" do
24
+ assert_instance_of Array, @response
25
+ assert_instance_of SugarCRM::User, @response[0]
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,14 @@
1
+ require 'helper'
2
+
3
+ class TestModuleFields < Test::Unit::TestCase
4
+ context "A SugarCRM.connection" do
5
+ setup do
6
+ SugarCRM::Connection.new(URL, USER, PASS, {:debug => false, :register_modules => false})
7
+ end
8
+ should "return a hash of module fields when #get_module_fields" do
9
+ fields = SugarCRM.connection.get_module_fields("Users")
10
+ assert_instance_of Hash, fields
11
+ assert "address_city", fields.keys[0]
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,15 @@
1
+ require 'helper'
2
+
3
+ class TestGetRelationships < Test::Unit::TestCase
4
+ context "A SugarCRM.connection" do
5
+ setup do
6
+ SugarCRM::Connection.new(URL, USER, PASS, {:register_modules => false, :debug => false})
7
+ end
8
+ should "return a list of email_addresses when sent #get_relationship and a user_id" do
9
+ email_address = SugarCRM.connection.get_relationships(
10
+ "Users",1,"email_addresses"
11
+ )
12
+ assert_instance_of SugarCRM::EmailAddress, email_address
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,12 @@
1
+ require 'helper'
2
+
3
+ class TestGetServerInfo < Test::Unit::TestCase
4
+ context "A SugarCRM.connection" do
5
+ setup do
6
+ SugarCRM::Connection.new(URL, USER, PASS, {:register_modules => false})
7
+ end
8
+ should "get server info" do
9
+ assert true, SugarCRM.connection.get_server_info
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,12 @@
1
+ require 'helper'
2
+
3
+ class TestGetUserID < Test::Unit::TestCase
4
+ context "A SugarCRM.connection" do
5
+ setup do
6
+ SugarCRM::Connection.new(URL, USER, PASS, {:register_modules => false})
7
+ end
8
+ should "get the ID of the logged in user" do
9
+ assert true, SugarCRM.connection.get_user_id
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,12 @@
1
+ require 'helper'
2
+
3
+ class TestGetUserTeamID < Test::Unit::TestCase
4
+ context "A SugarCRM.connection" do
5
+ setup do
6
+ SugarCRM::Connection.new(URL, USER, PASS, {:register_modules => false})
7
+ end
8
+ should "get the team ID of the logged in user" do
9
+ assert true, SugarCRM.connection.get_user_team_id
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,12 @@
1
+ require 'helper'
2
+
3
+ class TestLogin < Test::Unit::TestCase
4
+ context "A SugarCRM.connection" do
5
+ setup do
6
+ SugarCRM::Connection.new(URL, USER, PASS, {:register_modules => false})
7
+ end
8
+ should "login and set session id" do
9
+ assert_not_nil SugarCRM.connection.session
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,12 @@
1
+ require 'helper'
2
+
3
+ class TestLogin < Test::Unit::TestCase
4
+ context "A SugarCRM.connection" do
5
+ setup do
6
+ SugarCRM::Connection.new(URL, USER, PASS, {:register_modules => false})
7
+ end
8
+ should "logout and not be able to login with the old session id" do
9
+ assert true, SugarCRM.connection.logout
10
+ end
11
+ end
12
+ end
@@ -11,5 +11,8 @@ class Test::Unit::TestCase
11
11
  URL = "http://valet/sugarcrm6"
12
12
  USER = "admin"
13
13
  PASS = 'letmein'
14
- CRM = SugarCRM::Base.establish_connection(URL, USER, PASS, {:debug => true})
14
+
15
+ def setup_connection
16
+ SugarCRM::Base.establish_connection(URL, USER, PASS, {:debug => false})
17
+ end
15
18
  end
@@ -1,60 +1,24 @@
1
1
  require 'helper'
2
- require "test/unit"
3
- require "pp"
4
2
 
5
- class TestSugarcrm < Test::Unit::TestCase
3
+ #require 'connection/test_login'
4
+ #require 'connection/test_get_available_modules'
5
+ #require 'connection/test_get_module_fields'
6
+ #require 'connection/test_get_entry'
7
+ #require 'connection/test_get_entries'
8
+ #require 'connection/test_get_entry_list'
9
+
10
+ class TestConnection < Test::Unit::TestCase
6
11
  context "A SugarCRM::Connection instance" do
7
-
8
12
  setup do
9
- @connection = SugarCRM::Connection.new(URL, USER, PASS, false)
10
- end
11
-
12
- should "login and set session id" do
13
- assert_not_nil @connection.session
13
+ @connection = SugarCRM::Connection.new(URL, USER, PASS)
14
14
  end
15
-
16
15
  should "retrieve the list of available modules" do
17
- assert_instance_of Array, @connection.modules
16
+ assert_instance_of Array, SugarCRM.modules
17
+ assert_instance_of SugarCRM::Module, SugarCRM.modules[0]
18
18
  end
19
-
20
19
  should "create sub-classes by module name" do
20
+ @connection = SugarCRM::Connection.new(URL, USER, PASS, {:register_modules => true})
21
21
  assert SugarCRM.const_defined? "User"
22
22
  end
23
-
24
- should "return a single entry when sent #get_entry." do
25
- response = @connection.get_entry(
26
- "Users",
27
- 1,
28
- {:fields => ["first_name", "last_name"]}
29
- )
30
- assert response.response.key? "entry_list"
31
- end
32
-
33
- should "return a list of entries when sent #get_entries." do
34
- response = @connection.get_entries(
35
- "Users",
36
- [1],
37
- {:fields => ["first_name", "last_name"]}
38
- )
39
- assert response.key? "entry_list"
40
- end
41
-
42
- should "return a list of entries when sent #get_entry_list." do
43
- response = @connection.get_entry_list(
44
- "Users",
45
- "users.user_name = \'#{USER}\'",
46
- {
47
- :fields => ["first_name", "last_name"],
48
- :link_fields => [
49
- {
50
- "name" => "accounts",
51
- "value" => ["id", "name"]
52
- }
53
- ]
54
- }
55
- )
56
- assert response.key? "entry_list"
57
- end
58
-
59
23
  end
60
24
  end
@@ -0,0 +1,14 @@
1
+ require 'helper'
2
+
3
+ class TestModule < Test::Unit::TestCase
4
+ context "A SugarCRM::Module instance" do
5
+
6
+ setup do
7
+ @connection = SugarCRM::Connection.new(URL, USER, PASS)
8
+ end
9
+
10
+ should "respond to #fields" do
11
+ assert_respond_to SugarCRM.modules[0], :fields
12
+ end
13
+ end
14
+ end
@@ -1,10 +1,9 @@
1
1
  require 'helper'
2
- require "test/unit"
3
- require "pp"
4
2
 
5
- class TestSugarcrm < Test::Unit::TestCase
3
+ class TestResponse < Test::Unit::TestCase
6
4
  context "A SugarCRM::Response instance" do
7
5
  setup do
6
+ @connection = SugarCRM::Connection.new(URL, USER, PASS)
8
7
  @json = {"entry_list"=> [{
9
8
  "name_value_list"=> {
10
9
  "address_city" => {"name"=>"address_city", "value"=>""},
@@ -17,19 +16,11 @@ class TestSugarcrm < Test::Unit::TestCase
17
16
  }],
18
17
  "relationship_list"=>[]}
19
18
 
20
- @response = SugarCRM::Response.new(@json)
21
- end
22
-
23
- should "set the module name" do
24
- assert_equal "User", @response.module
25
- end
26
-
27
- should "flatten the name_value_list into an attributes hash" do
28
- assert_equal "never", @response.attributes[:pwd_last_changed]
19
+ @response = SugarCRM::Response.handle(@json)
29
20
  end
30
21
 
31
- should "return an instance of a SugarCRM Module when #object" do
32
- assert_instance_of SugarCRM::User, @response.object
22
+ should "return an instance of a SugarCRM Module" do
23
+ assert_instance_of SugarCRM::User, @response
33
24
  end
34
25
 
35
26
  end