Class: Aitch::Response

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

JSON_STR =
"json"
XML_STR =
"xml"
HTML_STR =
"html"
EMPTY_STR =
""
DOUBLE_COLON =
"::"
SPACE_STR =
" "
ERROR_SUFFIX =
"_error"
X_RE =
/^x-/
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.



20
21
22
23
24
25
# File 'lib/aitch/response.rb', line 20

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



86
87
88
89
90
# File 'lib/aitch/response.rb', line 86

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

  super
end

Instance Attribute Details

#content_typeObject

Returns the value of attribute content_type.



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

def content_type
  @content_type
end

#redirected_fromObject

Returns the value of attribute redirected_from.



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

def redirected_from
  @redirected_from
end

#urlObject

Returns the value of attribute url.



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

def url
  @url
end

Class Method Details

.description_for_code(code) ⇒ Object



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

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

Instance Method Details

#bodyObject



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

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

#codeObject



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

def code
  @http_response.code.to_i
end

#dataObject



74
75
76
# File 'lib/aitch/response.rb', line 74

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

#descriptionObject



96
97
98
# File 'lib/aitch/response.rb', line 96

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

#errorObject



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

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

#error?Boolean

Returns:

  • (Boolean)


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

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

#headersObject



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

def headers
  @headers ||= {}.tap do |headers|
    @http_response.each_header do |name, value|
      headers[name.gsub(X_RE, EMPTY_STR)] = value
    end
  end
end

#html?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/aitch/response.rb', line 70

def html?
  content_type.include?(HTML_STR)
end

#inspectObject Also known as: to_s



100
101
102
# File 'lib/aitch/response.rb', line 100

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

#json?Boolean

Returns:

  • (Boolean)


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

def json?
  content_type.include?(JSON_STR)
end

#redirect?Boolean

Returns:

  • (Boolean)


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

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

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

Returns:

  • (Boolean)


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

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

#success?Boolean Also known as: ok?

Returns:

  • (Boolean)


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

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

#xml?Boolean

Returns:

  • (Boolean)


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

def xml?
  content_type.include?(XML_STR)
end