Class: Alephant::Broker::Response::Base

Inherits:
Object
  • Object
show all
Includes:
Logger
Defined in:
lib/alephant/broker/response/base.rb

Direct Known Subclasses

Asset, Batch, NotFound, ServerError, Status

Constant Summary collapse

STATUS_CODE_MAPPING =
{
  200 => "ok",
  202 => "Accepted",
  304 => "",
  404 => "Not found",
  500 => "Error retrieving content"
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(status = 200, content_type = "text/html", request_env = nil) ⇒ Base



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/alephant/broker/response/base.rb', line 23

def initialize(status = 200, content_type = "text/html", request_env = nil)
  @content = STATUS_CODE_MAPPING[status]
  @headers = {
    "Content-Type"                 => content_type,
    "Access-Control-Allow-Headers" => "If-None-Match, If-Modified-Since",
    "Access-Control-Allow-Origin"  => "*"
  }
  @headers.merge!(Broker.config[:headers]) if Broker.config.key?(:headers)
  @status = status

  add_no_cache_headers if should_add_no_cache_headers?(status)
  add_etag_allow_header if headers.key?("ETag")
  setup if status == 200

  @content = "" if self.class.options?(request_env)
end

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



13
14
15
# File 'lib/alephant/broker/response/base.rb', line 13

def content
  @content
end

#headersObject (readonly)

Returns the value of attribute headers.



13
14
15
# File 'lib/alephant/broker/response/base.rb', line 13

def headers
  @headers
end

#statusObject (readonly)

Returns the value of attribute status.



13
14
15
# File 'lib/alephant/broker/response/base.rb', line 13

def status
  @status
end

Class Method Details

.allow_not_modified_response_statusObject



83
84
85
# File 'lib/alephant/broker/response/base.rb', line 83

def self.allow_not_modified_response_status
  Broker.config[:allow_not_modified_response_status] || false
end

.component_not_modified(headers, request_env) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/alephant/broker/response/base.rb', line 67

def self.component_not_modified(headers, request_env)
  return false unless allow_not_modified_response_status
  return false if request_env.post?
  return false if request_env.if_modified_since.nil? && request_env.if_none_match.nil?

  last_modified_match = !request_env.if_modified_since.nil? && headers["Last-Modified"] == request_env.if_modified_since
  etag_match          = !request_env.if_none_match.nil? &&
                        unquote_etag(headers["ETag"]) == unquote_etag(request_env.if_none_match)

  last_modified_match || etag_match
end

.options?(request_env) ⇒ Boolean



63
64
65
# File 'lib/alephant/broker/response/base.rb', line 63

def self.options?(request_env)
  request_env && request_env.respond_to?(:options?) && request_env.options?
end

.unquote_etag(etag) ⇒ Object



79
80
81
# File 'lib/alephant/broker/response/base.rb', line 79

def self.unquote_etag(etag)
  etag.to_s.gsub(/\A"|"\Z/, "")
end