Class: Spore::Middleware::Format

Inherits:
Spore::Middleware show all
Defined in:
lib/spore/middleware/format.rb

Defined Under Namespace

Classes: UnsupportedFormat

Instance Method Summary collapse

Methods inherited from Spore::Middleware

#initialize, #process_request

Constructor Details

This class inherits a constructor from Spore::Middleware

Instance Method Details

#expected_paramsObject



13
14
15
# File 'lib/spore/middleware/format.rb', line 13

def expected_params
  [ :format ]
end

#process_response(resp, env) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/spore/middleware/format.rb', line 17

def process_response(resp, env)
  return if resp.nil?

  if resp.code.to_s.match(/^2\d\d/)
    
    # empty string is considered nil object
    if resp.body.content.length == 0
      resp.body = nil
      return resp
    end

    # non-empty string are deserialized accordingly
    if self.format.downcase == 'json'
      resp.body = JSON.parse(resp.body.content)
    elsif self.format.match(/yaml/)
      resp.body = YAML.load(resp.body.content)
    else
      raise UnsupportedFormat, "don't know how to handle this format '#{self.format}'"
    end
    return resp
  end

  return resp
end