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
}.freeze
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"
}.freeze

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.



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

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



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

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.



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

def redirected_from
  @redirected_from
end

#urlObject

Returns the value of attribute url.



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

def url
  @url
end

Class Method Details

.description_for_code(code) ⇒ Object



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

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

Instance Method Details

#bodyObject



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

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

#codeObject



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

def code
  @http_response.code.to_i
end

#dataObject



67
68
69
# File 'lib/aitch/response.rb', line 67

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

#descriptionObject



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

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

#errorObject



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

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

#error?Boolean

Returns:

  • (Boolean)


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

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

#headersObject



71
72
73
74
75
76
77
# File 'lib/aitch/response.rb', line 71

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)


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

def html?
  content_type =~ /html/
end

#inspectObject Also known as: to_s



93
94
95
# File 'lib/aitch/response.rb', line 93

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

#json?Boolean

Returns:

  • (Boolean)


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

def json?
  content_type =~ /json/
end

#redirect?Boolean

Returns:

  • (Boolean)


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

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

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

Returns:

  • (Boolean)


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

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

#success?Boolean Also known as: ok?

Returns:

  • (Boolean)


38
39
40
# File 'lib/aitch/response.rb', line 38

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

#xml?Boolean

Returns:

  • (Boolean)


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

def xml?
  content_type =~ /xml/
end