Sha256: 60772a00ff7109f48c31e5717941f46d5836893a5927898de6434bc22fb5fbd5
Contents?: true
Size: 1.86 KB
Versions: 2
Compression:
Stored size: 1.86 KB
Contents
module Lanes module API module FormattedReply # json methods # constructs a Hash with success, messages, and data keys and # populates them appropriately def std_api_reply(type, data, options) success = options[:success].nil? ? true : options[:success] json = {} if data.is_a?(ActiveRecord::Base) && data.errors.any? json[:errors] = {} success = false data.errors.each{ | attr, message | json[:errors][attr] = message } end if options[:total_count] json[:total] = options.delete(:total_count) end json.merge( success: success, message: options[:messsage] || json_status_str(data, type.to_s.capitalize, success), data: success ? records_for_reply(data, type, options) : [] ) end # @return Array<Array> returns either an array of fields def records_for_reply(data, type, options) return [] if :destroy == type if options[:format] == 'array' data.pluck( *requested_fields ) else data.as_json(options) end end def json_status_str(record, type, success) if success type + " succeeded" elsif record msg = type + " failed" if record.is_a?(ActiveRecord::Base) msg += ": " + record.errors.full_messages.join("; ") end else return "Record not found" end end end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
lanes-0.4.0 | lib/lanes/api/formatted_reply.rb |
lanes-0.3.0 | lib/lanes/api/formatted_reply.rb |