./lib/lux/api/lib/response.rb in lux-fw-0.1.35 vs ./lib/lux/api/lib/response.rb in lux-fw-0.2.1
- old
+ new
@@ -1,11 +1,28 @@
class Lux::Api::Response
+ def self.error text
+ response = new
+ response.error text
+ response.render
+ end
+
+ ###
+
attr_accessor :data
attr_accessor :message
+ def initialize
+ @meta = {}
+ end
+
+ def status num=nil
+ meta 'http_status', num.to_i if num && !@meta['http_status']
+ @meta['http_status']
+ end
+
def meta key, value
- @meta ||= {}
+ value = value.to_i if key == :status
@meta[key.to_s] = value
end
def error key, data=nil
if data
@@ -21,25 +38,33 @@
def message what
@message = what
end
+ def redirect url
+ @meta['location'] = url
+ end
+
def errors?
(@error_hash || @errors) ? true : false
end
def render
output = {}
- output[:data] = @data if @data.present?
- output[:meta] = @meta if @meta.present?
- output[:message] = @message if @message.present?
-
if errors?
+ status 400
+
output[:error] ||= {}
output[:error][:messages] = @errors if @errors
output[:error][:hash] = @error_hash if @error_hash
end
+
+ Lux.current.response.status status
+
+ output[:data] = @data if @data.present?
+ output[:meta] = @meta if @meta.present?
+ output[:message] = @message if @message.present?
output
end
alias :to_hash :render
end
\ No newline at end of file