Class: Aitch::Response
- 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
-
#redirected_from ⇒ Object
Returns the value of attribute redirected_from.
-
#url ⇒ Object
Returns the value of attribute url.
Class Method Summary collapse
Instance Method Summary collapse
- #body ⇒ Object
- #code ⇒ Object
- #data ⇒ Object
- #description ⇒ Object
- #error ⇒ Object
- #error? ⇒ Boolean
- #headers ⇒ Object
- #html? ⇒ Boolean
-
#initialize(options, http_response) ⇒ Response
constructor
A new instance of Response.
- #inspect ⇒ Object (also: #to_s)
- #json? ⇒ Boolean
- #method_missing(name, *args, &block) ⇒ Object
- #redirect? ⇒ Boolean
- #respond_to_missing?(name, include_private = false) ⇒ Boolean
- #success? ⇒ Boolean (also: #ok?)
- #xml? ⇒ Boolean
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(, http_response) @options = @http_response = http_response @redirected_from = .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_from ⇒ Object
Returns the value of attribute redirected_from.
7 8 9 |
# File 'lib/aitch/response.rb', line 7 def redirected_from @redirected_from end |
#url ⇒ Object
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
#body ⇒ Object
31 32 33 |
# File 'lib/aitch/response.rb', line 31 def body @body ||= Body.new(@http_response).to_s end |
#code ⇒ Object
27 28 29 |
# File 'lib/aitch/response.rb', line 27 def code @http_response.code.to_i end |
#data ⇒ Object
64 65 66 |
# File 'lib/aitch/response.rb', line 64 def data Aitch::ResponseParser.find(content_type).load(body) end |
#description ⇒ Object
85 86 87 |
# File 'lib/aitch/response.rb', line 85 def description @description ||= self.class.description_for_code(code) end |
#error ⇒ Object
48 49 50 |
# File 'lib/aitch/response.rb', line 48 def error error? && ERRORS.fetch(code) end |
#error? ⇒ Boolean
44 45 46 |
# File 'lib/aitch/response.rb', line 44 def error? code >= 400 && code <= 599 end |
#headers ⇒ Object
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
60 61 62 |
# File 'lib/aitch/response.rb', line 60 def html? content_type =~ /html/ end |
#inspect ⇒ Object 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
52 53 54 |
# File 'lib/aitch/response.rb', line 52 def json? content_type =~ /json/ end |
#redirect? ⇒ 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
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?
35 36 37 |
# File 'lib/aitch/response.rb', line 35 def success? code >= 200 && code <= 399 end |
#xml? ⇒ Boolean
56 57 58 |
# File 'lib/aitch/response.rb', line 56 def xml? content_type =~ /xml/ end |