Skip to content

Commit

Permalink
All API methods now try to convert the response into an object.
Browse files Browse the repository at this point in the history
If that operation throws an exception, the hash response is returned
  • Loading branch information
chicks committed Nov 13, 2010
1 parent 710e741 commit 5df505d
Show file tree
Hide file tree
Showing 11 changed files with 97 additions and 91 deletions.
1 change: 0 additions & 1 deletion lib/sugarcrm/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ def establish_connection(url, user, pass, opts={})
# Runs a find against the remote service
def find(id)
response = SugarCRM.connection.get_entry(self._module.name, id,{:fields => self._module.fields.keys})
response.to_obj
end
end

Expand Down
3 changes: 2 additions & 1 deletion lib/sugarcrm/connection/api/get_document_revision.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ def get_document_revision(id)
}
EOF
json.gsub!(/^\s{6}/,'')
send!(:get_document_revision, json)
SugarCRM::Response.handle(send!(:get_document_revision, json))
#send!(:get_document_revision, json)
end
end; end
3 changes: 1 addition & 2 deletions lib/sugarcrm/connection/api/get_entries.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ def get_entries(module_name, ids, options={})
}
EOF
json.gsub!(/^\s{6}/,'')
SugarCRM::Response.new(send!(:get_entries, json))
#send!(:get_entries, json)
SugarCRM::Response.handle(send!(:get_entries, json))
end
end; end
2 changes: 1 addition & 1 deletion lib/sugarcrm/connection/api/get_entry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ def get_entry(module_name, id, options={})
EOF

json.gsub!(/^\s{6}/,'')
SugarCRM::Response.new(send!(:get_entry, json))
SugarCRM::Response.handle(send!(:get_entry, json))
end
end; end
3 changes: 1 addition & 2 deletions lib/sugarcrm/connection/api/get_entry_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ def get_entry_list(module_name, query, opts={})
}
EOF
json.gsub!(/^\s{6}/,'')
SugarCRM::Response.new(send!(:get_entry_list, json))
#send!(:get_entry_list, json)
SugarCRM::Response.handle(send!(:get_entry_list, json))
end
end; end
3 changes: 2 additions & 1 deletion lib/sugarcrm/connection/api/get_module_fields.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ def get_module_fields(module_name)
}
EOF
json.gsub!(/^\s{6}/,'')
send!(:get_module_fields, json)
SugarCRM::Response.handle(send!(:get_module_fields, json))
#send!(:get_module_fields, json)
end

alias :get_fields :get_module_fields
Expand Down
131 changes: 69 additions & 62 deletions lib/sugarcrm/response.rb
Original file line number Diff line number Diff line change
@@ -1,71 +1,78 @@
module SugarCRM
# takes a raw JSON response and turns it into a REAL object
class Response

attr :response, false
#attr :module, false
attr :id, false

def initialize(json)
@response = json
#@module = @response["entry_list"][0]["module_name"].classify
self
module SugarCRM; class Response

class << self
# This class handles the response from the server.
# It tries to convert the response into an object such as User
# or an object collection. If it fails, it just returns the response hash
def handle(json)
r = new(json)
begin
return r.to_obj
rescue
return json
end
end

# Tries to instantiate and return an object with the valutes
# populated from the response
def to_obj
objects = []
@response["entry_list"].each do |object|
attributes = []
_module = resolve_module(object)
begin
attributes = flatten_name_value_list(object)
rescue ArgumentError => e
end
if SugarCRM.const_get(_module)
if attributes.length == 0
pp object
raise AttributeParsingError, "response contains objects without attributes!"
end
objects << SugarCRM.const_get(_module).new(attributes)
else
raise InvalidModule, "#{_module} does not exist, or is not accessible"
end
end

attr :response, false

def initialize(json)
@response = json
end

# Tries to instantiate and return an object with the values
# populated from the response
def to_obj
objects = []
@response["entry_list"].each do |object|
attributes = []
_module = resolve_module(object)
begin
attributes = flatten_name_value_list(object)
rescue ArgumentError => e
end
# If we only have one result, just return the object
if objects.length == 1
return objects[0]
if SugarCRM.const_get(_module)
if attributes.length == 0
pp object
raise AttributeParsingError, "response contains objects without attributes!"
end
objects << SugarCRM.const_get(_module).new(attributes)
else
return objects
raise InvalidModule, "#{_module} does not exist, or is not accessible"
end
end

def to_json
@response.to_json
end

def resolve_module(list)
list["module_name"].classify
# If we only have one result, just return the object
if objects.length == 1
return objects[0]
else
return objects
end

def flatten_name_value_list(list)
if list["name_value_list"]
return flatten(list["name_value_list"])
else
return false
end
end

def to_json
@response.to_json
end

def resolve_module(list)
list["module_name"].classify
end

def flatten_name_value_list(list)
if list["name_value_list"]
return flatten(list["name_value_list"])
else
return false
end

# Takes a hash like { "first_name" => {"name" => "first_name", "value" => "John"}}
# And flattens it into {"first_name" => "John"}
def flatten(list)
raise ArgumentError, 'method parameter must respond to #each_pair' unless list.respond_to? :each_pair
flat_list = {}
list.each_pair do |k,v|
flat_list[k.to_sym] = v["value"]
end
flat_list
end

# Takes a hash like { "first_name" => {"name" => "first_name", "value" => "John"}}
# And flattens it into {"first_name" => "John"}
def flatten(list)
raise ArgumentError, 'method parameter must respond to #each_pair' unless list.respond_to? :each_pair
flat_list = {}
list.each_pair do |k,v|
flat_list[k.to_sym] = v["value"]
end
flat_list
end
end
end; end
12 changes: 6 additions & 6 deletions test/connection/test_get_entries.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ class TestGetEntries < Test::Unit::TestCase
{:fields => ["first_name", "last_name"]}
)
end
should "return a list of entries when sent #get_entries." do
assert @response.response.key? "entry_list"
end
should "return an object when #get_entries#to_obj" do
assert_instance_of Array, @response.to_obj
assert_instance_of SugarCRM::User, @response.to_obj[0]
# should "return a list of entries when sent #get_entries." do
# assert @response.response.key? "entry_list"
# end
should "return an object when #get_entries" do
assert_instance_of Array, @response
assert_instance_of SugarCRM::User, @response[0]
end
end
end
10 changes: 5 additions & 5 deletions test/connection/test_get_entry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ class TestGetEntry < Test::Unit::TestCase
{:fields => ["first_name", "last_name"]}
)
end
should "return a single entry when sent #get_entry." do
assert @response.response.key? "entry_list"
end
should "return an object when #get_entry#to_obj" do
assert_instance_of SugarCRM::User, @response.to_obj
# should "return a single entry when sent #get_entry." do
# assert @response.response. "entry_list"
# end
should "return an object when #get_entry" do
assert_instance_of SugarCRM::User, @response
end
end
end
14 changes: 7 additions & 7 deletions test/connection/test_get_entry_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@ class TestGetEntryList < Test::Unit::TestCase
{:fields => ["first_name", "last_name"]}
)
end
should "return a list of entries when sent #get_entry_list." do
assert @response.response.key? "entry_list"
end
# should "return a list of entries when sent #get_entry_list." do
# assert @response.response.key? "entry_list"
# end
should "return a list of entries when sent #get_entry_list and no fields." do
users = SugarCRM.connection.get_entry_list(
"Users",
"users.deleted = 0"
).to_obj
)
assert_equal "Administrator", users.first.last_name
end
should "return an object when #get_entry_list#to_obj" do
assert_instance_of Array, @response.to_obj
assert_instance_of SugarCRM::User, @response.to_obj[0]
should "return an object when #get_entry_list" do
assert_instance_of Array, @response
assert_instance_of SugarCRM::User, @response[0]
end
end
end
6 changes: 3 additions & 3 deletions test/test_response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ class TestResponse < Test::Unit::TestCase
}],
"relationship_list"=>[]}

@response = SugarCRM::Response.new(@json)
@response = SugarCRM::Response.handle(@json)
end

should "return an instance of a SugarCRM Module when #to_obj" do
assert_instance_of SugarCRM::User, @response.to_obj
should "return an instance of a SugarCRM Module" do
assert_instance_of SugarCRM::User, @response
end

end
Expand Down

0 comments on commit 5df505d

Please sign in to comment.