module JsonResponses
Public Instance Methods
http_status_codes()
click to toggle source
TODO: Error codes & messages list
1xx -> Informational 2xx -> Success 3xx -> Redirection 4xx -> Client Error 5xx -> Server Error
# File lib/exosinatra/json_responses.rb, line 159 def http_status_codes { 100 => 'Continue', 101 => 'Switching Protocols', 102 => 'Processing', 200 => 'OK', 201 => 'Created', 202 => 'Accepted', 203 => 'Non-authoritative Information', 204 => 'No Content', 205 => 'Reset Content', 206 => 'Partial Content', 207 => 'Multi-Status', 208 => 'Already Reported', 226 => 'IM Used', 300 => 'Multiple Choices', 301 => 'Moved Permanently', 302 => 'Found', 303 => 'See Other', 304 => 'Not Modified', 305 => 'Use Proxy', 307 => 'Temporary Redirect', 308 => 'Permanent Redirect', 400 => 'Bad Request', 401 => 'Unauthorized', 402 => 'Payment Required', 403 => 'Forbidden', 404 => 'Not Found', 405 => 'Method Not Allowed', 406 => 'Not Acceptable', 407 => 'Proxy Authentication Required', 408 => 'Request Timeout', 409 => 'Conflict', 410 => 'Gone', 411 => 'Length Required', 412 => 'Precondition Failed', 413 => 'Payload Too Large', 414 => 'Request-URI Too Long', 415 => 'Unsupported Media Type', 416 => 'Requested Range Not Satisfiable', 417 => 'Expectation Failed', 418 => "I'm a teapot", 421 => 'Misdirected Request', 422 => 'Unprocessable Entity', 423 => 'Locked', 424 => 'Failed Dependency', 426 => 'Upgrade Required', 428 => 'Precondition Required', 429 => 'Too Many Requests', 431 => 'Request Header Fields Too Large', 444 => 'Connection Closed Without Response', 451 => 'Unavailable For Legal Reasons', 499 => 'Client Closed Request', 500 => 'Internal Server Error', 501 => 'Not Implemented', 502 => 'Bad Gateway', 503 => 'Service Unavailable', 504 => 'Gateway Timeout', 505 => 'HTTP Version Not Supported', 506 => 'Variant Also Negotiates', 507 => 'Insufficient Storage', 508 => 'Loop Detected', 510 => 'Not Extended', 511 => 'Network Authentication Required', 599 => 'Network Connect Timeout Error' } end
json(resp)
click to toggle source
resp: response, Hash or Array
# File lib/exosinatra/json_responses.rb, line 9 def json(resp) headers['Content-Type'] = 'application/json' resp.to_json end
json_bool(ret)
click to toggle source
ret: return value, Boolean
# File lib/exosinatra/json_responses.rb, line 144 def json_bool(ret) json({ :success => ret }) end
json_error(code, errors='')
click to toggle source
code: error code, Int errors: specific errors, String or Array of strings (merged as string -with commas- in output)
# File lib/exosinatra/json_responses.rb, line 19 def json_error(code, errors='') halt(code, {'Content-Type' => 'application/json'}, { :code => code, :message => http_status_codes[code], :errors => errors.class == Array ? errors.join(', ') : errors }.to_json) end
json_http_return(ret)
click to toggle source
ret: return value, Hash
# File lib/exosinatra/json_responses.rb, line 105 def json_http_return(ret) r = ret[:http_return] status = status_of(ret[:http_return]) case status when :info json_info(r) when :success_payload json(ret[:body]) when :success_no_payload json_warning(r, ret[:warnings]) when :error json_error(r, ret[:errors]) else json(ret) end end
json_info(code)
click to toggle source
code: status code, Int
# File lib/exosinatra/json_responses.rb, line 49 def json_info(code) [ code, {'Content-Type' => 'application/json'}, { :code => code, :message => http_status_codes[code] }.to_json ] end
json_result(ret, err_code)
click to toggle source
ret: return value, Hash
# File lib/exosinatra/json_responses.rb, line 132 def json_result(ret, err_code) r = ret[:success] if r json(ret) else json_error(err_code, ret[:errors]) end end
json_return(ret)
click to toggle source
ret: return value, Hash
# File lib/exosinatra/json_responses.rb, line 63 def json_return(ret) r = ret[:return] case r when -1 json_error(400, ret[:errors]) when -2 json_error(500, ret[:errors]) when 0 json_warning(204, ret[:warnings]) else json(ret[:body]) end end
json_warning(code, warnings='')
click to toggle source
code: status code, Int warnings: specific warnings,
String or Array of strings (merged as string -with commas- in output)
# File lib/exosinatra/json_responses.rb, line 34 def json_warning(code, warnings='') [ code, {'Content-Type' => 'application/json'}, { :code => code, :message => http_status_codes[code], :warnings => warnings.class == Array ? warnings.join(', ') : warnings }.to_json ] end
status_of(code)
click to toggle source
code: status code, Int
# File lib/exosinatra/json_responses.rb, line 80 def status_of(code) case code when 100..199 :info when 200, 201 :success_payload when 202..399 :success_no_payload when 400..599 :error else :other end end
status_of_http_return(ret)
click to toggle source
ret: return value, Hash
# File lib/exosinatra/json_responses.rb, line 98 def status_of_http_return(ret) status_of(ret[:http_return]) end
status_of_result(ret)
click to toggle source
ret: return value, Hash
# File lib/exosinatra/json_responses.rb, line 125 def status_of_result(ret) ret[:success] end