Sha256: a846325553c06566703add7d7b1bbffdbe0c2b096b3a29e0786293a9b12ddf1c
Contents?: true
Size: 1.71 KB
Versions: 3
Compression:
Stored size: 1.71 KB
Contents
# Api response is constructed from this object class Joshua class Response def initialize api @api = api @out = {} @meta = {} @errors = {} end def []= key, value meta key, value end # forward header to rack_response.header def header *args if args.first @api.rack_response.header[args.first] = args[1] if @api.rack_response else @api.rack_response.header end end # human readable response message def message value @message = value end # api meta response, any data is allowed def meta key, value = nil if value @meta[key] = value else @meta[key] end end # add api response error def error *args return @errors unless args[0] desc, code = args.reverse @errors[:code] = code if code @errors[:messages] ||= [] @errors[:messages].push desc unless @errors[:messages].include?(desc) end def error? !!(@errors[:messages] || @errors[:details]) end def error_detail name, desc error '%s (%s)' % [desc, name] @errors[:details] ||= {} @errors[:details][name] = desc end def data value @data ||= value end def data? !@data.nil? end # render full api response def render {}.tap do |out| if @errors.keys.empty? out[:success] = true else out[:success] = false out[:error] = @errors end out[:meta] = @meta out[:message] = @message if @message out[:data] = @data unless @data.nil? out[:status] = error? ? 400 : 200 end end end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
joshua-0.2.2 | ./lib/joshua/response.rb |
joshua-0.2.1 | ./lib/joshua/response.rb |
joshua-0.1.0 | ./lib/joshua/response.rb |