forked from chicks/sugarcrm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
All API methods now try to convert the response into an object.
If that operation throws an exception, the hash response is returned
- Loading branch information
Showing
11 changed files
with
97 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters