Module: Clutterbuck::Response

Defined in:
lib/clutterbuck/response.rb

Instance Method Summary collapse

Instance Method Details

#add_header(name, val) ⇒ Object

Add a header to the response, possibly duplicating an existing header

If you want to be sure that your header is the only one that will be sent in the response, overriding any previous values for the same header, you want to use set_header rather than this method.

Parameters:

  • name (String)

    the name of the header to add.

  • val (String)

    the value of the header to add.

Returns:

  • void



29
30
31
32
33
# File 'lib/clutterbuck/response.rb', line 29

def add_header(name, val)
  @headers ||= []

  @headers << [name.downcase, val]
end

#callObject

:nodoc:

Override the default call method which (we hope) the application is using. Due to Ruby's way of handling overriding with mixed-in modules, the original call method must not be defined on the class which this module is being included in. It must be defined in a parent class, or else a module which is included before this one.



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/clutterbuck/response.rb', line 86

def call
  s, h, b = if self.class.ancestors.any? { |k| k.respond_to?(:call) }
    super
  else
    [nil, [], []]
  end

  if @status
    s = @status
  end

  if @headers
    h = h.to_a
    h += @headers
  end

  [s, h, b]
end

#clear_header(name) ⇒ Object

Remove all instances of the specified header from the response headers.

Parameters:

  • name (String)

    the name of the header to remove.

Returns:

  • void



58
59
60
61
62
# File 'lib/clutterbuck/response.rb', line 58

def clear_header(name)
  @headers ||= []
  name = name.downcase
  @headers = @headers.delete_if { |h| h[0] == name }
end

#get_header(name) ⇒ Array<String>

Retrieve the current value(s) of the specified header

Values for all instances of the header will be returned in an array.

Parameters:

  • name (String)

Returns:

  • (Array<String>)


72
73
74
75
76
# File 'lib/clutterbuck/response.rb', line 72

def get_header(name)
  @headers ||= []
  name = name.downcase
  @headers.select { |h| h[0] == name }.map { |h| h[1] }
end

#set_header(name, val) ⇒ Object

Set a header in the response

This will override any previous value(s) for the same header previously set. Sometimes this is what you want, and sometimes it isn't. You're expected to know which is which.

Parameters:

  • name (String)

    the name of the header to set.

  • val (String)

    the value to set for the header.

Returns:

  • void



47
48
49
50
# File 'lib/clutterbuck/response.rb', line 47

def set_header(name, val)
  clear_header(name)
  add_header(name, val)
end

#status(s) ⇒ Object Also known as: status=



4
5
6
7
8
9
10
11
12
13
# File 'lib/clutterbuck/response.rb', line 4

def status(s)
  s = s.to_i

  if s < 100 || s > 599
    raise ArgumentError,
          "Status code must be between 100-599 inclusive"
  end

  @status = s
end