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.



95
96
97
# File 'lib/octarine/response.rb', line 95

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

#[]=(key, value) ⇒ Object

:call-seq: response = value -> value

Set a header.



103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/octarine/response.rb', line 103

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]



122
123
124
# File 'lib/octarine/response.rb', line 122

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

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

:call-seq: response.update {|body| block } -> response response.update(path[, opts]) {|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 a path 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" => "..."}]}

Additional options can be passed as a hash, the options available are:

remove

The full path to the element that should be removed if the block returns nil. Must be a parent of the element targeted by the main path argument

remove_if

If supplied along with the remove option the result of the block will be tested againt this value (using ===) rather than nil

link

Should be supplied with a value of a hash, in which the key is a path to an element to be updated, and the value is an array of a an element and a method from which to derive a value

Example:

response.body
#=> {"data" => [1, 2, 3], "total" => 3}

user_names = {1 => "Arthur", 2 => "Ford"}

total_to_length = {"total" => ["data", :length]}
response.update("data.", remove: "data.", link: total_to_length) do |id|
  user_names[id]
end

response.body
# {"data" => ["Arthur", "Ford"], "total" => 2}


70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/octarine/response.rb', line 70

def update(path=nil, options={}, &block)
  @body = if body.respond_to?(:to_ary) && path.nil?
    block.call(body)
  else
    path = nil if path == "."
    remove_path = options[:remove]
    remove_if = remove_path ? options[:remove_if] : -> x {false}
    apply(body, path, remove_path, remove_if, &block)
  end
  (options[:link] || []).each do |dest, (source_path, source_method)|
    update(dest) do |val|
      source = body
      source_path.split(".").each do |part|
        source = source[part]
      end
      source.send(source_method)
    end
  end
  self
end