Module: Sinatra::APIResponse

Defined in:
lib/extensions/api_response.rb

Instance Method Summary collapse

Instance Method Details

#api_response(&block) ⇒ Object

This makes the api very DRY by taking care of API errors and converting the object or value passed by the block to json.



5
6
7
8
9
10
11
12
13
# File 'lib/extensions/api_response.rb', line 5

def api_response(&block)
  raise 'No block given' unless block_given?

  if errors.any?
    return { errors: errors }.to_json
  else
    block.call.tap {|response| return response.to_json }
  end
end