Class: Factorix::HTTP::CachedResponse

Inherits:
Object
  • Object
show all
Defined in:
lib/factorix/http/cached_response.rb

Overview

Response wrapper for cached data

Provides a simple response object that can be constructed from a cached body string, without requiring an actual HTTP response. Used by CacheDecorator to return cached content with a uniform interface matching Response objects.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(body, uri: nil) ⇒ CachedResponse

Returns a new instance of CachedResponse.

Parameters:

  • body (String)

    cached response body

  • uri (URI, nil) (defaults to: nil)

    original request URI (not stored in cache, always nil)



19
20
21
22
23
24
# File 'lib/factorix/http/cached_response.rb', line 19

def initialize(body, uri: nil)
  @body = body
  @code = 200
  @headers = {"content-type" => ["application/octet-stream"]}
  @uri = uri
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



12
13
14
# File 'lib/factorix/http/cached_response.rb', line 12

def body
  @body
end

#codeObject (readonly)

Returns the value of attribute code.



13
14
15
# File 'lib/factorix/http/cached_response.rb', line 13

def code
  @code
end

#headersObject (readonly)

Returns the value of attribute headers.



14
15
16
# File 'lib/factorix/http/cached_response.rb', line 14

def headers
  @headers
end

#uriObject (readonly)

Returns the value of attribute uri.



15
16
17
# File 'lib/factorix/http/cached_response.rb', line 15

def uri
  @uri
end

Instance Method Details

#content_lengthInteger

Get content length from body size

Returns:

  • (Integer)

    body size in bytes



37
# File 'lib/factorix/http/cached_response.rb', line 37

def content_length = @body.bytesize

#success?Boolean

Always returns true for cached responses

Since only successful responses are cached, all CachedResponse objects represent successful HTTP interactions.

Returns:

  • (Boolean)

    true



32
# File 'lib/factorix/http/cached_response.rb', line 32

def success? = true