class Object

Public Instance Methods

error_message(status, err, object=nil) click to toggle source
# File lib/ncc-api.rb, line 29
def error_message(status, err, object=nil)
    status_message = case status
                     when 400
                         "400 Bad Request"
                     when 404
                         "404 Not Found"
                     when 500
                         "500 Internal Server Error"
                     when 503
                         "503 Service Unavailable"
                     end
    status_message ||= status.to_s
    data = { "status" => status_message, "message" => err.message }
    # I can't really just spit the object out here because I don't
    # know what it is--it might be configuration objects that would
    # contain passwords.
    unless object.nil?
        data['original_object'] = {
            "class" => object.class
        }
    end
    if params.has_key? 'details'
        data['details'] = err.backtrace
        data['error'] = err.class
    end
    body = (params.has_key?('pretty') ? (JSON.pretty_generate(data) +
            "\n") : data.to_json)
    logger.error "#{status_message} (#{err.class}) #{err.message}: #{err.backtrace.join("\n")}"
    [status, { "content-type" => "application/json" }, body]
end
ncc() click to toggle source
# File lib/ncc-api.rb, line 25
def ncc
    $ncc ||= NCC.new(nil, :logger => logger)
end
respond(status, header={}) { || ... } click to toggle source
# File lib/ncc-api.rb, line 60
def respond(status, header={}, &block)
    header.merge({ "content-type" => "application/json" })
    begin
        obj = yield
        body = if header["content-type"] == "text/plain"
                   obj
               else
                   params.has_key?('pretty') ? (JSON.pretty_generate(obj) + "\n") : obj.to_json
               end
        return [status, header, body]
    rescue NCC::Error::NotFound => error
        halt error_message(404, error)
    rescue NCC::Error::Cloud => error
        halt error_message(503, error)
    rescue NCC::Error::Client => error
        halt error_message(400, error)
    rescue Exception => error
        halt error_message(500, error, obj)
    end
end