Sha256: fb43135ad28bf97ec99f71ae943337e9b41b28ef126cbc306e079f69ef061715

Contents?: true

Size: 1.23 KB

Versions: 2

Compression:

Stored size: 1.23 KB

Contents

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
    value = value.to_i if key == :status
    @meta[key.to_s] = value
  end

  def error key, data=nil
    if data
      @error_hash ||= {}
      @error_hash[key.to_s] = data
      data
    else
      @errors ||= []
      @errors.push key unless @errors.include?(key)
      key
    end
  end

  def message what
    @message = what
  end

  def redirect url
    @meta['location'] = url
  end

  def errors?
    (@error_hash || @errors) ? true : false
  end

  def render
    output = {}

    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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
lux-fw-0.2.3 ./lib/lux/api/lib/response.rb
lux-fw-0.2.1 ./lib/lux/api/lib/response.rb