Method: ActiveResource::Errors#from_json

Defined in:
lib/active_resource/validations.rb

#from_json(json, save_cache = false) ⇒ Object

Grabs errors from a json response.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/active_resource/validations.rb', line 54

def from_json(json, save_cache = false)
  decoded = ActiveSupport::JSON.decode(json) || {} rescue {}
  if decoded.kind_of?(Hash) && (decoded.has_key?("errors") || decoded.empty?)
    errors = decoded["errors"] || {}
    if errors.kind_of?(Array)
      # 3.2.1-style with array of strings
      ActiveResource.deprecator.warn("Returning errors as an array of strings is deprecated.")
      from_array errors, save_cache
    else
      # 3.2.2+ style
      from_hash errors, save_cache
    end
  else
    # <3.2-style respond_with - lacks 'errors' key
    ActiveResource.deprecator.warn('Returning errors as a hash without a root "errors" key is deprecated.')
    from_hash decoded, save_cache
  end
end