Class: SugarCRM::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/sugarcrm/connection/response.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(json, session, opts = {}) ⇒ Response

Returns a new instance of Response.



28
29
30
31
32
33
# File 'lib/sugarcrm/connection/response.rb', line 28

def initialize(json, session, opts={})
  @options  = { :always_return_array => false }.merge! opts
  @response = json
  @response = json.with_indifferent_access if json.is_a? Hash
  @session = session
end

Instance Attribute Details

#responseObject (readonly)

Returns the value of attribute response.



26
27
28
# File 'lib/sugarcrm/connection/response.rb', line 26

def response
  @response
end

Class Method Details

.handle(json, session) ⇒ 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



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/sugarcrm/connection/response.rb', line 6

def handle(json, session)
  r = new(json, session)
  begin
    return r.to_obj
  rescue UninitializedModule => e
    raise e
  rescue InvalidAttribute => e
    raise e
  rescue InvalidAttributeType => e
    raise e
  rescue => e
    if session.connection.debug?
      puts "Failed to process JSON:"
      pp json
    end
    raise e
  end
end

Instance Method Details

#flatten(list) ⇒ Object

Takes a hash like { “first_name” => => “first_name”, “value” => “John”} And flattens it into => “John”

Raises:

  • (ArgumentError)


82
83
84
85
86
87
88
89
90
# File 'lib/sugarcrm/connection/response.rb', line 82

def flatten(list)
  raise ArgumentError, list[0]['value'] if list[0] && list[0]['name'] == 'warning'
  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



72
73
74
75
76
77
78
# File 'lib/sugarcrm/connection/response.rb', line 72

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



68
69
70
# File 'lib/sugarcrm/connection/response.rb', line 68

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

#to_jsonObject



64
65
66
# File 'lib/sugarcrm/connection/response.rb', line 64

def to_json
  @response.to_json
end

#to_objObject

Tries to instantiate and return an object with the values populated from the response



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/sugarcrm/connection/response.rb', line 37

def to_obj
  # If this is not a "entry_list" response, just return
  return @response unless @response && @response["entry_list"]
  
  objects = []
  @response["entry_list"].each do |object|
    attributes = []
    _module    = resolve_module(object)
    attributes = flatten_name_value_list(object)
    namespace = @session.namespace_const
    if namespace.const_get(_module)
      if attributes.length == 0
        raise AttributeParsingError, "response contains objects without attributes!"
      end
      objects << namespace.const_get(_module).new(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 && !@options[:always_return_array]
    return objects[0]
  else
    return objects
  end
end