Class: MintHttp::Response
- Inherits:
-
Object
- Object
- MintHttp::Response
- Defined in:
- lib/mint_http/response.rb
Instance Attribute Summary collapse
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
-
#local_address ⇒ Object
readonly
Returns the value of attribute local_address.
-
#mint_request ⇒ Object
readonly
Returns the value of attribute mint_request.
-
#net_request ⇒ Object
readonly
Returns the value of attribute net_request.
-
#net_response ⇒ Object
readonly
Returns the value of attribute net_response.
-
#remote_address ⇒ Object
readonly
Returns the value of attribute remote_address.
-
#status_code ⇒ Object
readonly
Returns the value of attribute status_code.
-
#status_text ⇒ Object
readonly
Returns the value of attribute status_text.
- #time_connected ⇒ Numeric
- #time_connecting ⇒ Numeric
- #time_ended ⇒ Numeric
- #time_started ⇒ Numeric
- #time_total ⇒ Numeric
-
#version ⇒ Object
readonly
Returns the value of attribute version.
Instance Method Summary collapse
- #body ⇒ Object
- #client_error? ⇒ Boolean
- #https? ⇒ Boolean
-
#initialize(net_response, net_request, mint_request, net_http) ⇒ Response
constructor
A new instance of Response.
- #inspect ⇒ Object
- #json ⇒ Object
- #json? ⇒ Boolean
- #not_found? ⇒ Boolean
- #raise! ⇒ Object
- #redirect? ⇒ Boolean
- #server_error? ⇒ Boolean
- #success? ⇒ Boolean
- #unauthenticated? ⇒ Boolean
- #unauthorized? ⇒ Boolean
- #xml? ⇒ Boolean
Constructor Details
#initialize(net_response, net_request, mint_request, net_http) ⇒ Response
Returns a new instance of Response.
65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/mint_http/response.rb', line 65 def initialize(net_response, net_request, mint_request, net_http) @net_response = net_response @net_request = net_request @mint_request = mint_request @version = net_response.http_version @status_code = net_response.code.to_i @status_text = net_response. @headers = Headers.new.merge(net_response.each_header.to_h) tcp_socket = net_http. @local_address = tcp_socket.local_address rescue nil @remote_address = tcp_socket.remote_address rescue nil end |
Instance Attribute Details
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
31 32 33 |
# File 'lib/mint_http/response.rb', line 31 def headers @headers end |
#local_address ⇒ Object (readonly)
Returns the value of attribute local_address.
55 56 57 |
# File 'lib/mint_http/response.rb', line 55 def local_address @local_address end |
#mint_request ⇒ Object (readonly)
Returns the value of attribute mint_request.
15 16 17 |
# File 'lib/mint_http/response.rb', line 15 def mint_request @mint_request end |
#net_request ⇒ Object (readonly)
Returns the value of attribute net_request.
7 8 9 |
# File 'lib/mint_http/response.rb', line 7 def net_request @net_request end |
#net_response ⇒ Object (readonly)
Returns the value of attribute net_response.
11 12 13 |
# File 'lib/mint_http/response.rb', line 11 def net_response @net_response end |
#remote_address ⇒ Object (readonly)
Returns the value of attribute remote_address.
59 60 61 |
# File 'lib/mint_http/response.rb', line 59 def remote_address @remote_address end |
#status_code ⇒ Object (readonly)
Returns the value of attribute status_code.
23 24 25 |
# File 'lib/mint_http/response.rb', line 23 def status_code @status_code end |
#status_text ⇒ Object (readonly)
Returns the value of attribute status_text.
27 28 29 |
# File 'lib/mint_http/response.rb', line 27 def status_text @status_text end |
#time_connected ⇒ Numeric
43 44 45 |
# File 'lib/mint_http/response.rb', line 43 def time_connected @time_connected end |
#time_connecting ⇒ Numeric
51 52 53 |
# File 'lib/mint_http/response.rb', line 51 def time_connecting @time_connecting end |
#time_ended ⇒ Numeric
39 40 41 |
# File 'lib/mint_http/response.rb', line 39 def time_ended @time_ended end |
#time_started ⇒ Numeric
35 36 37 |
# File 'lib/mint_http/response.rb', line 35 def time_started @time_started end |
#time_total ⇒ Numeric
47 48 49 |
# File 'lib/mint_http/response.rb', line 47 def time_total @time_total end |
#version ⇒ Object (readonly)
Returns the value of attribute version.
19 20 21 |
# File 'lib/mint_http/response.rb', line 19 def version @version end |
Instance Method Details
#body ⇒ Object
130 131 132 |
# File 'lib/mint_http/response.rb', line 130 def body net_response.body end |
#client_error? ⇒ Boolean
87 88 89 |
# File 'lib/mint_http/response.rb', line 87 def client_error? (400..499).include?(@status_code) end |
#https? ⇒ Boolean
146 147 148 |
# File 'lib/mint_http/response.rb', line 146 def https? @net_request end |
#inspect ⇒ Object
150 151 152 |
# File 'lib/mint_http/response.rb', line 150 def inspect "#<#{self.class}/#{@version} #{@status_code} #{@status_text} #{@headers.inspect}>" end |
#json ⇒ Object
134 135 136 |
# File 'lib/mint_http/response.rb', line 134 def json @json ||= JSON.parse(body) end |
#json? ⇒ Boolean
138 139 140 |
# File 'lib/mint_http/response.rb', line 138 def json? headers['content-type']&.include?('application/json') end |
#not_found? ⇒ Boolean
99 100 101 |
# File 'lib/mint_http/response.rb', line 99 def not_found? @status_code == 404 end |
#raise! ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/mint_http/response.rb', line 107 def raise! case @status_code when 401 raise AuthenticationError.new('Unauthenticated', self) when 403 raise AuthorizationError.new('Forbidden', self) when 404 raise NotFoundError.new('Not Found', self) when 502 raise BadGatewayError.new('Bad Gateway', self) when 503 raise ServiceUnavailableError.new('Service Unavailable', self) when 504 raise GatewayTimeoutError.new('Gateway Timeout', self) when 400..499 raise ClientError.new('Client Error', self) when 500..599 raise ServerError.new('Server Error', self) else self end end |
#redirect? ⇒ Boolean
83 84 85 |
# File 'lib/mint_http/response.rb', line 83 def redirect? (300..399).include?(@status_code) end |
#server_error? ⇒ Boolean
103 104 105 |
# File 'lib/mint_http/response.rb', line 103 def server_error? (500..599).include?(@status_code) end |
#success? ⇒ Boolean
79 80 81 |
# File 'lib/mint_http/response.rb', line 79 def success? (200..299).include?(@status_code) end |
#unauthenticated? ⇒ Boolean
91 92 93 |
# File 'lib/mint_http/response.rb', line 91 def unauthenticated? @status_code == 401 end |
#unauthorized? ⇒ Boolean
95 96 97 |
# File 'lib/mint_http/response.rb', line 95 def @status_code == 403 end |
#xml? ⇒ Boolean
142 143 144 |
# File 'lib/mint_http/response.rb', line 142 def xml? headers['content-type']&.match?(/\/xml/) end |