Sha256: ff7407cfdd2ca59b71097b597b9d171a91815ab4d4821cea5fd4e344539660b4

Contents?: true

Size: 411 Bytes

Versions: 9

Compression:

Stored size: 411 Bytes

Contents

module Sinatra
  module APIResponse
    # This makes the api very DRY by taking care of API errors and converting the object
    # or value passed by the block to json.
    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
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
memcached-manager-1.0.0 lib/extensions/api_response.rb
memcached-manager-0.4.0 lib/extensions/api_response.rb
memcached-manager-0.3.0 lib/extensions/api_response.rb
memcached-manager-0.2.2 lib/extensions/api_response.rb
memcached-manager-0.2.1 lib/extensions/api_response.rb
memcached-manager-0.2.0 lib/extensions/api_response.rb
memcached-manager-0.1.0 lib/extensions/api_response.rb
memcached-manager-0.0.1 lib/extensions/api_response.rb
memcached-manager-0.0.0 lib/extensions/api_response.rb