Class: HyperSpec::Response

Inherits:
Struct
  • Object
show all
Defined in:
lib/hyperspec.rb

Constant Summary collapse

STATI =
{
  # RFC 2616 - HTTP 1.1
  ## Informational
  100 => :continue,
  101 => :switching_protocols,

  ## Successful 2xx
  200 => :ok,
  201 => :created,
  202 => :accepted,
  203 => :non_authoritative_information,
  204 => :no_content,
  205 => :reset_content,
  206 => :partial_content,

  ## Redirection 3xx
  300 => :multiple_choices,
  301 => :moved_permanently,
  302 => :found,
  303 => :see_other,
  304 => :not_modified,
  305 => :use_proxy,
# 306 => :(Unused),
  307 => :temporary_redirect,

  ## Client Error 4xx,
  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,

  ## Server Error 5xx
  500 => :internal_server_error,
  501 => :not_implemented,
  502 => :bad_gateway,
  503 => :service_unavailable,
  504 => :gateway_timeout,
  505 => :http_version_not_supported,

  # RFC 2324
  418 => :im_a_teapot,

  # RFC 4918 - WebDav
  207 => :multi_status,
  422 => :unprocessable_entity,
  423 => :locked,
  424 => :failed_dependency,
  507 => :insufficient_storage,

  # RFC 6585 - Additional HTTP Status Codes
  428 => :precondition_required,
  429 => :too_many_requests,
  431 => :request_header_fields_too_large,
  511 => :network_authentication_required,
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#bodyObject

Returns the value of attribute body

Returns:

  • (Object)

    the current value of body



139
140
141
# File 'lib/hyperspec.rb', line 139

def body
  @body
end

#headersObject

Returns the value of attribute headers

Returns:

  • (Object)

    the current value of headers



139
140
141
# File 'lib/hyperspec.rb', line 139

def headers
  @headers
end

#status_codeObject

Returns the value of attribute status_code

Returns:

  • (Object)

    the current value of status_code



139
140
141
# File 'lib/hyperspec.rb', line 139

def status_code
  @status_code
end

Class Method Details

.from_net_http_response(http) ⇒ Object



210
211
212
213
214
215
216
217
218
# File 'lib/hyperspec.rb', line 210

def self.from_net_http_response(http)
  status_code = http.code.to_i
  body        = http.body

  headers =
    CaseInsensitiveHash.from_hash(http.to_hash)

  new(status_code, headers, body)
end

Instance Method Details

#content_charsetObject



224
225
226
# File 'lib/hyperspec.rb', line 224

def content_charset
  (md = headers['Content-Type'].match(/;charset=(.*)/)) && md[1]
end

#content_typeObject



220
221
222
# File 'lib/hyperspec.rb', line 220

def content_type
  headers['Content-Type'].split(';').first
end

#statusObject



228
229
230
# File 'lib/hyperspec.rb', line 228

def status
  STATI[status_code]
end