Class: JSend::Rails::Envelope

Inherits:
Object
  • Object
show all
Defined in:
lib/jsend-rails/envelope.rb

Constant Summary collapse

STATUS =
[:error, :fail, :success]

Class Method Summary collapse

Class Method Details

.compute(options_or_status) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/jsend-rails/envelope.rb', line 6

def self.compute(options_or_status)
  if options_or_status.is_a?(Hash)
    if options_or_status.include?(:error)
      p = {:status => :error, :message => options_or_status[:error]}
      p[:code] = options_or_status[:code] if options_or_status.include?(:code)
      p[:data] = options_or_status[:data] if options_or_status.include?(:data)
      p
    elsif options_or_status.include?(:fail)
      {:status => :fail, :data => options_or_status[:fail]}
    elsif options_or_status.include?(:success)
      {:status => :success, :data => options_or_status[:success]}
    else
      raise ArgumentError.new("one of #{STATUS.join(', ')} required")
    end
  else
    raise ArgumentError.new("one of #{STATUS.join(', ')} required") unless STATUS.include?(options_or_status)
    {:status => options_or_status, :data => {}}
  end
end