Top Level Namespace

Defined Under Namespace

Classes: Hash, NCC

Instance Method Summary collapse

Instance Method Details

#error_message(status, err, object = nil) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# 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

#nccObject



25
26
27
# File 'lib/ncc-api.rb', line 25

def ncc
    $ncc ||= NCC.new(nil, :logger => logger)
end

#respond(status, header = {}, &block) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# 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