Class: Webbed::HTTPVersion

Inherits:
Object
  • Object
show all
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

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.

Examples:

Webbed::HTTPVersion.new(1.1)
Webbed::HTTPVersion.new('HTTP/1.1')

Parameters:

  • http_version (#to_s)

    the HTTP-Version to create



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.

Examples:

version_1_1 = Webbed::HTTPVersion.new(1.1)
version_5_0 = Webbed::HTTPVersion.new('HTTP/5.0')
version_1_1 == version_5_0 # => false
version_5_0 < version_5_0 # => false
version_5_0 > version_1_1 # => true

Parameters:

  • other_http_version (#to_f)

    the other HTTP-Version

Returns:

  • (Fixnum)

    the sign of the comparison (either 1, 0, or -1)



79
80
81
# File 'lib/webbed/http_version.rb', line 79

def <=>(other_http_version)
  to_f <=> other_http_version.to_f
end

#majorFixnum

The major HTTP-Version number.

Examples:

version = Webbed::HTTPVersion.new('HTTP/6.9')
version.major # => 6

Returns:

  • (Fixnum)


90
91
92
93
# File 'lib/webbed/http_version.rb', line 90

def major
  REGEX =~ @http_version
  $1.to_i
end

#minorFixnum

The minor HTTP-Version number.

Examples:

version = Webbed::HTTPVersion.new('HTTP/4.2')
version.minor # => 2

Returns:

  • (Fixnum)


102
103
104
105
# File 'lib/webbed/http_version.rb', line 102

def minor
  REGEX =~ @http_version
  $2.to_i
end

#to_fFloat

Converts the HTTP-Version to a float.

Examples:

version = Webbed::HTTPVersion.new('HTTP/1.1')
version.to_f # => 1.1

Returns:

  • (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_sString

Converts the HTTP-Version to a string according to RFC 2616.

Examples:

version = Webbed::HTTPVersion.new(1.1)
version.to_s # => 'HTTP/1.1'

Returns:

  • (String)


52
53
54
# File 'lib/webbed/http_version.rb', line 52

def to_s
  @http_version
end