Class: Aitch::Response

Inherits:
Object show all
Extended by:
Forwardable
Defined in:
lib/aitch/response.rb,
lib/aitch/response/body.rb,
lib/aitch/response/errors.rb,
lib/aitch/response/description.rb

Defined Under Namespace

Classes: Body

Constant Summary collapse

ERRORS =
{
  400 => BadRequestError,
  401 => UnauthorizedError,
  402 => PaymentRequiredError,
  403 => ForbiddenError,
  404 => NotFoundError,
  405 => MethodNotAllowedError,
  406 => NotAcceptableError,
  407 => ProxyAuthenticationRequiredError,
  408 => RequestTimeOutError,
  409 => ConflictError,
  410 => GoneError,
  411 => LengthRequiredError,
  412 => PreconditionFailedError,
  413 => RequestEntityTooLargeError,
  414 => RequestURITooLongError,
  415 => UnsupportedMediaTypeError,
  416 => RequestedRangeNotSatisfiableError,
  417 => ExpectationFailedError,
  422 => UnprocessableEntityError,
  423 => LockedError,
  424 => FailedDependencyError,
  426 => UpgradeRequiredError,
  428 => PreconditionRequiredError,
  429 => TooManyRequestsError,
  431 => RequestHeaderFieldsTooLargeError,
  500 => InternalServerErrorError,
  501 => NotImplementedError,
  502 => BadGatewayError,
  503 => ServiceUnavailableError,
  504 => GatewayTimeOutError,
  505 => VersionNotSupportedError,
  507 => InsufficientStorageError,
  511 => NetworkAuthenticationRequiredError
}
DESCRIPTION =
{
  100 => "Continue",
  101 => "Switch Protocol",

  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 Time Out",
  409 => "Conflict",
  410 => "Gone",
  411 => "Length Required",
  412 => "Precondition Failed",
  413 => "Request Entity Too Large",
  414 => "Request URIToo Long",
  415 => "Unsupported Media Type",
  416 => "Requested Range Not Satisfiable",
  417 => "Expectation Failed",
  422 => "Unprocessable Entity",
  423 => "Locked",
  424 => "Failed Dependency",
  426 => "Upgrade Required",
  428 => "Precondition Required",
  429 => "Too Many Requests",
  431 => "Request Header Fields Too Large",

  500 => "Internal Server Error",
  501 => "Not Implemented",
  502 => "Bad Gateway",
  503 => "Service Unavailable",
  504 => "Gateway Time Out",
  505 => "Version Not Supported",
  507 => "Insufficient Storage",
  511 => "Network Authentication Required",
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options, http_response) ⇒ Response

Returns a new instance of Response.



13
14
15
16
17
# File 'lib/aitch/response.rb', line 13

def initialize(options, http_response)
  @options = options
  @http_response = http_response
  @redirected_from = options.fetch(:redirected_from, [])
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



76
77
78
79
# File 'lib/aitch/response.rb', line 76

def method_missing(name, *args, &block)
  return headers[name.to_s] if headers.key?(name.to_s)
  super
end

Instance Attribute Details

#redirected_fromObject

Returns the value of attribute redirected_from.



7
8
9
# File 'lib/aitch/response.rb', line 7

def redirected_from
  @redirected_from
end

#urlObject

Returns the value of attribute url.



7
8
9
# File 'lib/aitch/response.rb', line 7

def url
  @url
end

Class Method Details

.description_for_code(code) ⇒ Object



9
10
11
# File 'lib/aitch/response.rb', line 9

def self.description_for_code(code)
  [code, DESCRIPTION[code]].compact.join(" ")
end

Instance Method Details

#bodyObject



31
32
33
# File 'lib/aitch/response.rb', line 31

def body
  @body ||= Body.new(@http_response).to_s
end

#codeObject



27
28
29
# File 'lib/aitch/response.rb', line 27

def code
  @http_response.code.to_i
end

#dataObject



64
65
66
# File 'lib/aitch/response.rb', line 64

def data
  Aitch::ResponseParser.find(content_type).load(body)
end

#descriptionObject



85
86
87
# File 'lib/aitch/response.rb', line 85

def description
  @description ||= self.class.description_for_code(code)
end

#errorObject



48
49
50
# File 'lib/aitch/response.rb', line 48

def error
  error? && ERRORS.fetch(code)
end

#error?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/aitch/response.rb', line 44

def error?
  code >= 400 && code <= 599
end

#headersObject



68
69
70
71
72
73
74
# File 'lib/aitch/response.rb', line 68

def headers
  @headers ||= {}.tap do |headers|
    @http_response.each_header do |name, value|
      headers[name.gsub(/^x-/, "")] = value
    end
  end
end

#html?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/aitch/response.rb', line 60

def html?
  content_type =~ /html/
end

#inspectObject Also known as: to_s



89
90
91
# File 'lib/aitch/response.rb', line 89

def inspect
  "#<#{self.class} #{description} (#{content_type})>"
end

#json?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/aitch/response.rb', line 52

def json?
  content_type =~ /json/
end

#redirect?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/aitch/response.rb', line 40

def redirect?
  code >= 300 && code <= 399
end

#respond_to_missing?(name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/aitch/response.rb', line 81

def respond_to_missing?(name, include_private = false)
  headers.key?(name.to_s)
end

#success?Boolean Also known as: ok?

Returns:

  • (Boolean)


35
36
37
# File 'lib/aitch/response.rb', line 35

def success?
  code >= 200 && code <= 399
end

#xml?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/aitch/response.rb', line 56

def xml?
  content_type =~ /xml/
end