`
orcl_zhang
  • 浏览: 232401 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

Rails HTTP Status Code to Symbol Mapping

阅读更多
http状态码http://zh.wikipedia.org/zh/HTTP%E7%8A%B6%E6%80%81%E7%A0%81
rails目录下,actionpack/lib/action_controller/status_codes.rb
rails把状态码和状态信息完全对应起来了,非常好。
module ActionController
  module StatusCodes #:nodoc:
    # Defines the standard HTTP status codes, by integer, with their
    # corresponding default message texts.
    # Source: http://www.iana.org/assignments/http-status-codes
    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",
      226 => "IM Used",

      300 => "Multiple Choices",
      301 => "Moved Permanently",
      302 => "Found",
      303 => "See Other",
      304 => "Not Modified",
      305 => "Use Proxy",
      307 => "Temporary 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 => "Request Entity Too Large",
      414 => "Request-URI Too Long",
      415 => "Unsupported Media Type",
      416 => "Requested Range Not Satisfiable",
      417 => "Expectation Failed",
      422 => "Unprocessable Entity",
      423 => "Locked",
      424 => "Failed Dependency",
      426 => "Upgrade Required",

      500 => "Internal Server Error",
      501 => "Not Implemented",
      502 => "Bad Gateway",
      503 => "Service Unavailable",
      504 => "Gateway Timeout",
      505 => "HTTP Version Not Supported",
      507 => "Insufficient Storage",
      510 => "Not Extended"
    }

    # Provides a symbol-to-fixnum lookup for converting a symbol (like
    # :created or :not_implemented) into its corresponding HTTP status
    # code (like 200 or 501).
    SYMBOL_TO_STATUS_CODE = STATUS_CODES.inject({}) do |hash, (code, message)|
      hash[message.gsub(/ /, "").underscore.to_sym] = code
      hash
    end

    # Given a status parameter, determine whether it needs to be converted
    # to a string. If it is a fixnum, use the STATUS_CODES hash to lookup
    # the default message. If it is a symbol, use the SYMBOL_TO_STATUS_CODE
    # hash to convert it.
    def interpret_status(status)
      case status
      when Fixnum then
        "#{status} #{STATUS_CODES[status]}".strip
      when Symbol then
        interpret_status(SYMBOL_TO_STATUS_CODE[status] ||
          "500 Unknown Status #{status.inspect}")
      else
        status.to_s
      end
    end
    private :interpret_status

  end
end
分享到:
评论

相关推荐

    codemirror-rails:将CodeMirror与Rails一起使用

    Codemirror-rails 为您的Rails应用程序连接资产。 入门 如果您使用的是Bundler,则可以将codemirror-rails添加到Gemfile中: gem 'codemirror-rails' 或手动安装codemirror-rails gem: gem install codemirror-...

    rails 5 test prescriptions build a healthy codebase

    With Rails examples, use testing to enable your code to respond better to future change. Plus, see how to handle real-world testing situations. This new edition has been updated to Rails 5.2 and ...

    Rails Recipes英文版(清晰文字pdf+源码)

    Ruby三神书之一(其余的两本是Agile.Web.Development.with.Rails和Ruby For Rails,在我的资源列表也有) ... Owning "Rails Recipes" is like having the best Rails programmers sitting next to you while you code.

    Bootstrap for Rails (2015)

    Summary 94 Chapter 6: Creating Navigation Bars 95 Getting started with a navigation bar 95 Navigation bar helper classes 102 Adding a navigation bar to the Rails application 103 Summary 105 Chapter 7:...

    Learning Rails 5(高清文字pdf版)

    You’ll learn how to create something visible with Rails’ view layer before diving into the more difficult inner layers: the database models and controller code. All you need to begin your Rails ...

    Rails.5.Revealed.For.those.Upgrading.to.Version.5

    This short early adopter book details both how you'll upgrade existing web and other applications from Rails 4.2 to 5 and how to create new applications in Rails 5. The headline features, Rails API ...

    Ajax on Rails

    This book teaches intermediate to advanced web developers how to use both Ajax and Rails to quickly build high-performance, scalable applications without being overwhelmed with thousands of lines of ...

    Learning Rails.pdf

    Connect forms to models by setting up a database, and use Rails' ActiveRecord to create code that maps to database structures • Use Rails scaffolding to build applications from a view-centric ...

    [Rails] Crafting Rails Applications (英文版)

    This pioneering book is the first resource that deep dives into the new Rails 3 APIs and shows you how use them to write better web applications and make your day-to-day work with Rails more ...

    Rails.Angular.Postgres.and.Bootstrap.2nd.Edition

    Also new is Webpack coverage, to develop the front-end code for your Rails application. Create a usable and attractive login form using Bootstrap's styles, while ensuring the database table backing ...

    rails code

    Agile Web Development with Rails 3nd Edition source code

    Rails 5 Revealed(Apress,2016)

    This short early adopter book details both how you’ll upgrade existing web and other applications from Rails 4.2 to 5 and how to create new applications in Rails 5. The headline features, Rails API ...

    Agile Web Development with Rails 4

    You concentrate on creating the application, and Rails takes care of the details., Tens of thousands of developers have used this award-winning book to learn Rails. It’s a broad, far-reaching ...

    [Rails] Rails Devise 学习教程 (英文版)

    Use Devise to make your Rails application accessible, user friendly, and secure Overview Use Devise to implement an e-mail-based sign-in process in a few minutes Override Devise controllers to allow...

    The Rails3 Way, 2nd Edition

    Ruby on Rails strips complexity from the development process, enabling professional developers to focus on what matters most: delivering business value via clean and maintainable code. The Rails™ 3 ...

    [Rails] 敏捷网络应用开发 (Rails 4 实现) (英文版)

    Rails 4 introduces a number of user-facing changes, and the ebook has been updated to match all the latest changes and new best practices in Rails. This includes full support for Ruby 2.0, controller ...

    Ruby on Rails Guides_ A Guide to Testing Rails Applications.pdf

    Ruby on Rails Guides_ A Guide to Testing Rails Applications.pdf

    Beginning Ruby on Rails

    You'll learn to build Rails applications, connect to databases, perform necessary testing, and put the whole thing together to create real-world applications such as shopping carts and online ...

    The Rails 3 Way(2nd)

    Ruby on Rails strips complexity from the development process, enabling professional developers to focus on what matters most: delivering business value via clean and maintainable code. The Rails™ 3 ...

    Head First Rails A Learner's Companion to Ruby on Railsa.pdf

    Head First Rails A Learner's Companion to Ruby on Railsa。 英文版的, 感觉非常不错,强烈推荐。

Global site tag (gtag.js) - Google Analytics