Class: SugarCRM::Response
- Inherits:
-
Object
- Object
- SugarCRM::Response
- Defined in:
- lib/sugarcrm/response.rb
Instance Attribute Summary collapse
-
#response ⇒ Object
readonly
Returns the value of attribute response.
Class Method Summary collapse
-
.handle(json) ⇒ Object
This class handles the response from the server.
Instance Method Summary collapse
-
#flatten(list) ⇒ Object
Takes a hash like { “first_name” => => “first_name”, “value” => “John”} And flattens it into => “John”.
- #flatten_name_value_list(list) ⇒ Object
-
#initialize(json) ⇒ Response
constructor
A new instance of Response.
- #resolve_module(list) ⇒ Object
- #to_json ⇒ Object
-
#to_obj ⇒ Object
Tries to instantiate and return an object with the values populated from the response.
Constructor Details
#initialize(json) ⇒ Response
Returns a new instance of Response.
19 20 21 |
# File 'lib/sugarcrm/response.rb', line 19 def initialize(json) @response = json end |
Instance Attribute Details
#response ⇒ Object (readonly)
Returns the value of attribute response.
17 18 19 |
# File 'lib/sugarcrm/response.rb', line 17 def response @response end |
Class Method Details
.handle(json) ⇒ Object
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
7 8 9 10 11 12 13 14 |
# File 'lib/sugarcrm/response.rb', line 7 def handle(json) r = new(json) begin return r.to_obj rescue return json end end |
Instance Method Details
#flatten(list) ⇒ Object
Takes a hash like { “first_name” => => “first_name”, “value” => “John”} And flattens it into => “John”
71 72 73 74 75 76 77 78 |
# File 'lib/sugarcrm/response.rb', line 71 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 |
#flatten_name_value_list(list) ⇒ Object
61 62 63 64 65 66 67 |
# File 'lib/sugarcrm/response.rb', line 61 def flatten_name_value_list(list) if list["name_value_list"] return flatten(list["name_value_list"]) else return false end end |
#resolve_module(list) ⇒ Object
57 58 59 |
# File 'lib/sugarcrm/response.rb', line 57 def resolve_module(list) list["module_name"].classify end |
#to_json ⇒ Object
53 54 55 |
# File 'lib/sugarcrm/response.rb', line 53 def to_json @response.to_json end |
#to_obj ⇒ Object
Tries to instantiate and return an object with the values populated from the response
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/sugarcrm/response.rb', line 25 def to_obj objects = [] @response["entry_list"].each do |object| attributes = [] _module = resolve_module(object) id = object["id"] 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(id, attributes) else raise InvalidModule, "#{_module} does not exist, or is not accessible" end end # If we only have one result, just return the object if objects.length == 1 return objects[0] else return objects end end |