Top Level Namespace
Defined Under Namespace
Instance Method Summary collapse
- #error_message(status, err, object = nil) ⇒ Object
- #ncc ⇒ Object
- #respond(status, header = {}, &block) ⇒ Object
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 (status, err, object=nil) = 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.to_s data = { "status" => , "message" => err. } # 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 "#{} (#{err.class}) #{err.}: #{err.backtrace.join("\n")}" [status, { "content-type" => "application/json" }, body] end |
#ncc ⇒ Object
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 (404, error) rescue NCC::Error::Cloud => error halt (503, error) rescue NCC::Error::Client => error halt (400, error) rescue Exception => error halt (500, error, obj) end end |