Skip to content

Instantly share code, notes, and snippets.

@robhurring
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robhurring/d5f1ce1d0fd0ded9d6d4 to your computer and use it in GitHub Desktop.
Save robhurring/d5f1ce1d0fd0ded9d6d4 to your computer and use it in GitHub Desktop.
Rails status symbols in javascript (for angular)
/* global angular */
(function(module) {
'use strict';
module.run(['$rootScope', '$state', 'Rails',
function($rootScope, $state, Rails) {
$rootScope.$on('$stateChangeError', function(event, toState, toParams, fromState, fromParams, error) {
switch(error.status) {
// or Rails.status.not_found.code (if using the version with descriptions)
case Rails.status.not_found.:
$state.go('error.404');
break;
case Rails.status.unauthorized:
$state.go('error.401');
break;
default:
$state.go('error.500');
}
});
}]);
})(angular.module('MyApp'));
/* global angular */
(function(module) {
'use strict';
// simple status map
var RAILS_STATUS_MAP = {
"continue": 100,
"switching_protocols": 101,
"processing": 102,
"ok": 200,
"created": 201,
"accepted": 202,
"non_authoritative_information": 203,
"no_content": 204,
"reset_content": 205,
"partial_content": 206,
"multi_status": 207,
"im_used": 226,
"multiple_choices": 300,
"moved_permanently": 301,
"found": 302,
"see_other": 303,
"not_modified": 304,
"use_proxy": 305,
"temporary_redirect": 307,
"bad_request": 400,
"unauthorized": 401,
"payment_required": 402,
"forbidden": 403,
"not_found": 404,
"method_not_allowed": 405,
"not_acceptable": 406,
"proxy_authentication_required": 407,
"request_timeout": 408,
"conflict": 409,
"gone": 410,
"length_required": 411,
"precondition_failed": 412,
"request_entity_too_large": 413,
"request_uri_too_long": 414,
"unsupported_media_type": 415,
"requested_range_not_satisfiable": 416,
"expectation_failed": 417,
"unprocessable_entity": 422,
"locked": 423,
"failed_dependency": 424,
"upgrade_required": 426,
"internal_server_error": 500,
"not_implemented": 501,
"bad_gateway": 502,
"service_unavailable": 503,
"gateway_timeout": 504,
"http_version_not_supported": 505,
"insufficient_storage": 507,
"not_extended": 510
};
module.constant('Rails', {
status: RAILS_STATUS_MAP
});
})(angular.module('MyApp'));
/* global angular */
(function(module) {
'use strict';
// status map with descriptions
var RAILS_STATUS_MAP = {
"continue": {
"code": 100,
"description": "Continue"
},
"switching_protocols": {
"code": 101,
"description": "Switching Protocol"
},
"processing": {
"code": 102,
"description": "Processing"
},
"ok": {
"code": 200,
"description": "OK"
},
"created": {
"code": 201,
"description": "Create"
},
"accepted": {
"code": 202,
"description": "Accepted"
},
"non_authoritative_information": {
"code": 203,
"description": "Non-Authoritative Information"
},
"no_content": {
"code": 204,
"description": "No Content"
},
"reset_content": {
"code": 205,
"description": "Reset Content"
},
"partial_content": {
"code": 206,
"description": "Partial Content"
},
"multi_status": {
"code": 207,
"description": "Multi-Status"
},
"im_used": {
"code": 226,
"description": "IM Use"
},
"multiple_choices": {
"code": 300,
"description": "Multiple Choices"
},
"moved_permanently": {
"code": 301,
"description": "Moved Permanently"
},
"found": {
"code": 302,
"description": "Found"
},
"see_other": {
"code": 303,
"description": "See Other"
},
"not_modified": {
"code": 304,
"description": "Not Modified"
},
"use_proxy": {
"code": 305,
"description": "Use Proxy"
},
"temporary_redirect": {
"code": 307,
"description": "Temporary Redirect"
},
"bad_request": {
"code": 400,
"description": "Bad Request"
},
"unauthorized": {
"code": 401,
"description": "Unauthorized"
},
"payment_required": {
"code": 402,
"description": "Payment Required"
},
"forbidden": {
"code": 403,
"description": "Forbidden"
},
"not_found": {
"code": 404,
"description": "Not Found"
},
"method_not_allowed": {
"code": 405,
"description": "Method Not Allowed"
},
"not_acceptable": {
"code": 406,
"description": "Not Acceptable"
},
"proxy_authentication_required": {
"code": 407,
"description": "Proxy Authentication Required"
},
"request_timeout": {
"code": 408,
"description": "Request Timeout"
},
"conflict": {
"code": 409,
"description": "Conflict"
},
"gone": {
"code": 410,
"description": "Gone"
},
"length_required": {
"code": 411,
"description": "Length Required"
},
"precondition_failed": {
"code": 412,
"description": "Precondition Failed"
},
"request_entity_too_large": {
"code": 413,
"description": "Request Entity Too Large"
},
"request_uri_too_long": {
"code": 414,
"description": "Request-URI Too Long"
},
"unsupported_media_type": {
"code": 415,
"description": "Unsupported Media Type"
},
"requested_range_not_satisfiable": {
"code": 416,
"description": "Requested Range Not Satisfiably"
},
"expectation_failed": {
"code": 417,
"description": "Expectation Failed"
},
"unprocessable_entity": {
"code": 422,
"description": "Unprocessable Entity"
},
"locked": {
"code": 423,
"description": "Locked"
},
"failed_dependency": {
"code": 424,
"description": "Failed Dependency"
},
"upgrade_required": {
"code": 426,
"description": "Upgrade Required"
},
"internal_server_error": {
"code": 500,
"description": "Internal Server Error"
},
"not_implemented": {
"code": 501,
"description": "Not Implemented"
},
"bad_gateway": {
"code": 502,
"description": "Bad Gateway"
},
"service_unavailable": {
"code": 503,
"description": "Service Unavailable"
},
"gateway_timeout": {
"code": 504,
"description": "Gateway Timeout"
},
"http_version_not_supported": {
"code": 505,
"description": "HTTP Version Not Supported"
},
"insufficient_storage": {
"code": 507,
"description": "Insufficient Storage"
},
"not_extended": {
"code": 510,
"description": "Not Extended"
}
};
module.constant('Rails', {
status: RAILS_STATUS_MAP
});
})(angular.module('MyApp'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment