lib/appfuel/response.rb in appfuel-0.5.2 vs lib/appfuel/response.rb in appfuel-0.5.3
- old
+ new
@@ -8,13 +8,32 @@
# Convience method for creating a successfull response
#
# @param result Hash the successfull resultset
# @reuturn Response
def ok(result = nil)
+ result = ok_data(result) if ok_key?(result)
self.new(ok: result)
end
+ def ok_key?(data)
+ return false unless data.is_a?(Hash)
+ data.key?(:ok) || data.key?("ok")
+ end
+
+ def error_key?(data)
+ return false unless data.is_a?(Hash)
+ data.key?(:errors) || data.key?("errors")
+ end
+
+ def ok_data(data)
+ data[:ok] || data['ok']
+ end
+
+ def error_data(data)
+ data[:errors] || data['errors']
+ end
+
# Convience method for creating an error response. It understands
# how to handle a SpCore::Error object. Any thing that
# is not a hash or can't be converted to a hash is assumed to be
# a string and converted into a general_error
#
@@ -43,21 +62,19 @@
# @param data [Hash]
# @return [Response]
def initialize(data = {})
result = format_result_hash(data)
-
- # when no ok key and no errors key the assume
+ # when no ok key and no errors key then assume
# it is a successfull response
- if !result.key?(:ok) && !result.key?(:errors)
- result = {ok: result}
- end
-
- @ok = result[:ok]
+ @ok = nil
@errors = nil
- if result.key?(:errors)
- @ok = nil
- @errors = Errors.new(result[:errors])
+ if self.class.ok_key?(result)
+ @ok = self.class.ok_data(result)
+ elsif self.class.error_key?(result)
+ @errors = Errors.new(self.class.error_data(result))
+ else
+ @ok = result
end
end
def errors?
!ok?