Class: Octarine::Response

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/octarine/response.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(body = [], header = {}, status = 200) ⇒ Response

:call-seq: Response.new(body) -> response Response.new(body, header) -> response Response.new(body, header, status) -> response

Create a new Response instance.



18
19
20
21
22
23
24
# File 'lib/octarine/response.rb', line 18

def initialize(body=[], header={}, status=200)
  status, header = header, status if header.respond_to?(:to_i)
  @body = body
  @header = header
  @status = status.to_i
  header["content-type"] ||= "text/html" unless [204, 304].include?(@status)
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



5
6
7
# File 'lib/octarine/response.rb', line 5

def body
  @body
end

#headerObject Also known as: headers

Returns the value of attribute header.



5
6
7
# File 'lib/octarine/response.rb', line 5

def header
  @header
end

#statusObject

Returns the value of attribute status.



5
6
7
# File 'lib/octarine/response.rb', line 5

def status
  @status
end

Instance Method Details

#[](key) ⇒ Object

:call-seq: response -> value

Get a header.



58
59
60
# File 'lib/octarine/response.rb', line 58

def [](key)
  (key.is_a?(Numeric) ? to_ary : header)[key]
end

#[]=(key, value) ⇒ Object

:call-seq: response = value -> value

Set a header.



66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/octarine/response.rb', line 66

def []=(key, value)
  return header[key] = value unless key.is_a?(Numeric)
  case key
  when 0
    @status = value
  when 1
    @header = value
  when 2
    @body = value
  else
    raise ArgumentError.new("Unexpected key #{key}")
  end
end

#to_aryObject Also known as: to_a

:call-seq: response.to_ary -> array response.to_a -> array

Convert to a Rack response array of [status, headers, body]



85
86
87
# File 'lib/octarine/response.rb', line 85

def to_ary
  [status, header, body.respond_to?(:each) ? body : [body].compact]
end

#update(path = nil, &block) ⇒ Object

:call-seq: response.update {|body| block } -> response response.update(path) {|value| block } -> response

Called without an argument, the block will be supplied the response body, and the response body will be set to the result of the block. The response itself is returned.

When called with an argument the body should be a hash, the body will be traversed accoring to the path supplied, the value of the body will be yielded to the block, and then replaced with the result of the block. Example:

response.body
#=> {"data" => [{"user" => "1234", "message" => "..."}]}

response.update("data.user") {|id| User.find(id).to_hash}

response.body
#=> {"data" => [{"user" => {"id" => "1234", ...}, "message" => "..."}]}


45
46
47
48
49
50
51
52
# File 'lib/octarine/response.rb', line 45

def update(path=nil, &block)
  @body = if body.respond_to?(:to_ary) && path.nil?
    block.call(body)
  else
    apply(body, path, &block)
  end
  self
end