Class: Webbed::HTTPVersion
- Inherits:
-
Object
- Object
- Webbed::HTTPVersion
- Includes:
- Comparable
- Defined in:
- lib/webbed/http_version.rb
Overview
Representation of an HTTP HTTP Version.
Webbed supports both primary versions of HTTP, HTTP/1.0 and HTTP/1.1. Although the use of HTTP/1.1 has been strongly encouraged since its creation in 1999, it remains relatively common for older command line tools (such as wget) and some search engines. Webbed can also be extended in the future to support new versions of HTTP, should one ever come into existence.
HTTPVersion is a small abstraction on top of the HTTP-Version as defined in RFC 2616. According to the RFC, its simple format is:
HTTP-Version = "HTTP" "/" 1*DIGIT "." 1*DIGIT
While this is perhaps the simplest of all the abstractions in Webbed, it does offer some nice helper methods for treating the version string more Ruby-like.
HTTP/1.0 and HTTP/1.1 HTTPVersion's are cached. In every case I can think of, you will not have to create a new HTTPVersion, just use the constants ONE_POINT_OH and ONE_POINT_ONE when creating messages.
Constant Summary collapse
- REGEX =
/^HTTP\/(\d+)\.(\d+)$/
- ONE_POINT_ONE =
HTTPVersion.new(1.1)
- ONE_POINT_OH =
HTTPVersion.new(1.0)
Instance Method Summary collapse
-
#<=>(other_http_version) ⇒ Fixnum
Compares the HTTP-Version to another HTTP-Version.
-
#initialize(http_version) ⇒ HTTPVersion
constructor
Creates a new HTTP-Version.
-
#major ⇒ Fixnum
The major HTTP-Version number.
-
#minor ⇒ Fixnum
The minor HTTP-Version number.
-
#to_f ⇒ Float
Converts the HTTP-Version to a float.
-
#to_s ⇒ String
Converts the HTTP-Version to a string according to RFC 2616.
Constructor Details
#initialize(http_version) ⇒ HTTPVersion
Creates a new HTTP-Version.
Only HTTP/1.0 and HTTP/1.1 versions are cached. All other versions will be created at runtime each time this method is called.
37 38 39 40 41 42 43 |
# File 'lib/webbed/http_version.rb', line 37 def initialize(http_version) if REGEX =~ http_version.to_s @http_version = http_version.to_s else @http_version = "HTTP/#{http_version}" end end |
Instance Method Details
#<=>(other_http_version) ⇒ Fixnum
Compares the HTTP-Version to another HTTP-Version.
79 80 81 |
# File 'lib/webbed/http_version.rb', line 79 def <=>(other_http_version) to_f <=> other_http_version.to_f end |
#major ⇒ Fixnum
The major HTTP-Version number.
90 91 92 93 |
# File 'lib/webbed/http_version.rb', line 90 def major REGEX =~ @http_version $1.to_i end |
#minor ⇒ Fixnum
The minor HTTP-Version number.
102 103 104 105 |
# File 'lib/webbed/http_version.rb', line 102 def minor REGEX =~ @http_version $2.to_i end |
#to_f ⇒ Float
Converts the HTTP-Version to a float.
63 64 65 66 |
# File 'lib/webbed/http_version.rb', line 63 def to_f REGEX =~ @http_version "#{$1}.#{$2}".to_f end |
#to_s ⇒ String
Converts the HTTP-Version to a string according to RFC 2616.
52 53 54 |
# File 'lib/webbed/http_version.rb', line 52 def to_s @http_version end |