Class: Hurley::Response
- Inherits:
-
Object
- Object
- Hurley::Response
- Defined in:
- lib/hurley/client.rb
Constant Summary collapse
- STATUS_TYPES =
[:success, :redirection, :client_error, :server_error]
- STATUS_FORCE_GET =
Set.new([301, 302, 303])
- STATUS_REDIRECTION =
STATUS_FORCE_GET + [307, 308]
Instance Attribute Summary collapse
-
#body ⇒ Object
Returns the value of attribute body.
-
#header ⇒ Object
readonly
Returns the value of attribute header.
-
#request ⇒ Object
readonly
Returns the value of attribute request.
-
#status_code ⇒ Object
Returns the value of attribute status_code.
- #via ⇒ Object
Instance Method Summary collapse
- #automatically_redirect?(previous_requests = nil) ⇒ Boolean
- #client_error? ⇒ Boolean
- #complete! ⇒ Object
-
#initialize(request, status_code = nil, header = nil) ⇒ Response
constructor
A new instance of Response.
- #inspect ⇒ Object
- #location ⇒ Object
- #ms ⇒ Object
- #receive_body(chunk) ⇒ Object
- #redirection? ⇒ Boolean
- #server_error? ⇒ Boolean
- #status_type ⇒ Object
- #success? ⇒ Boolean
Constructor Details
#initialize(request, status_code = nil, header = nil) ⇒ Response
Returns a new instance of Response.
243 244 245 246 247 248 249 250 251 252 253 254 255 |
# File 'lib/hurley/client.rb', line 243 def initialize(request, status_code = nil, header = nil) @request = request @status_code = status_code @header = header || Header.new @body = nil @receiver = nil @timing = nil @started_at = Time.now.to_f if block_given? yield self complete! end end |
Instance Attribute Details
#body ⇒ Object
Returns the value of attribute body.
239 240 241 |
# File 'lib/hurley/client.rb', line 239 def body @body end |
#header ⇒ Object (readonly)
Returns the value of attribute header.
238 239 240 |
# File 'lib/hurley/client.rb', line 238 def header @header end |
#request ⇒ Object (readonly)
Returns the value of attribute request.
237 238 239 |
# File 'lib/hurley/client.rb', line 237 def request @request end |
#status_code ⇒ Object
Returns the value of attribute status_code.
240 241 242 |
# File 'lib/hurley/client.rb', line 240 def status_code @status_code end |
#via ⇒ Object
264 265 266 |
# File 'lib/hurley/client.rb', line 264 def via @via ||= [] end |
Instance Method Details
#automatically_redirect?(previous_requests = nil) ⇒ Boolean
296 297 298 299 300 |
# File 'lib/hurley/client.rb', line 296 def automatically_redirect?(previous_requests = nil) return false unless redirection? limit = request..redirection_limit.to_i limit > 0 && Array(previous_requests).size < limit end |
#client_error? ⇒ Boolean
288 289 290 |
# File 'lib/hurley/client.rb', line 288 def client_error? status_code > 399 && status_code < 500 end |
#complete! ⇒ Object
257 258 259 260 261 262 |
# File 'lib/hurley/client.rb', line 257 def complete! @ended_at = Time.now.to_f if @receiver.respond_to?(:join) @body = @receiver.join end end |
#inspect ⇒ Object
321 322 323 324 325 326 327 328 329 330 |
# File 'lib/hurley/client.rb', line 321 def inspect "#<%s %s %s == %d%s %dms>" % [ self.class.name, @request.verb.to_s.upcase, @request.url.to_s, @status_code.to_i, @body ? " (#{@body.bytesize} bytes)" : nil, ms, ] end |
#location ⇒ Object
268 269 270 271 272 273 274 |
# File 'lib/hurley/client.rb', line 268 def location @location ||= begin return unless loc = @header[:location] verb = STATUS_FORCE_GET.include?(status_code) ? :get : request.verb Request.new(verb, request.url.join(Url.parse(loc)), request.header, request.body, request., request.) end end |
#ms ⇒ Object
317 318 319 |
# File 'lib/hurley/client.rb', line 317 def ms @timing ||= ((@ended_at - @started_at) * 1000).to_i end |
#receive_body(chunk) ⇒ Object
302 303 304 305 306 307 308 309 310 311 312 313 314 315 |
# File 'lib/hurley/client.rb', line 302 def receive_body(chunk) return if chunk.nil? if @receiver.nil? statuses, receiver = request.send(:body_receiver) @receiver = if statuses && !statuses.include?(@status_code) BodyReceiver.new else receiver end end @receiver.call(self, chunk) end |
#redirection? ⇒ Boolean
280 281 282 |
# File 'lib/hurley/client.rb', line 280 def redirection? STATUS_REDIRECTION.include?(status_code) end |
#server_error? ⇒ Boolean
292 293 294 |
# File 'lib/hurley/client.rb', line 292 def server_error? status_code > 499 && status_code < 600 end |
#status_type ⇒ Object
276 277 278 |
# File 'lib/hurley/client.rb', line 276 def status_type @status_type ||= STATUS_TYPES.detect { |t| send("#{t}?") } || :other end |
#success? ⇒ Boolean
284 285 286 |
# File 'lib/hurley/client.rb', line 284 def success? status_code > 199 && status_code < 300 end |