Sha256: 4926a24277752a140ed4b3f09058a0a227decff4791728101b5d68acfe926e12

Contents?: true

Size: 940 Bytes

Versions: 3

Compression:

Stored size: 940 Bytes

Contents

module SugarCRM
  # takes a raw JSON response and turns it into a REAL object
  class Response
    
    attr :response, false
    attr :module, false
    attr :attributes, false
    attr :object, false
    attr :id, false
    
    def initialize(json)
      @response   = json
      @module     = @response["entry_list"][0]["module_name"].singularize
      @attributes = flatten(@response["entry_list"][0]["name_value_list"])
      @object     = SugarCRM.const_get(@module).new(@attributes) if SugarCRM.const_get(@module)
    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

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
sugarcrm-0.6.2 lib/sugarcrm/response.rb
sugarcrm-0.6.1 lib/sugarcrm/response.rb
sugarcrm-0.6.0 lib/sugarcrm/response.rb