0

我正在开发一个 api 作为模块化 Sinatra Web 应用程序,并希望标准化返回的响应而不必明确这样做。我认为这可以通过使用中间件来实现,但在大多数情况下都失败了。下面的示例应用程序是我到目前为止所拥有的。

配置.ru

require 'sinatra/base'
require 'active_support'
require 'rack'

class Person
  attr_reader :name, :surname
  def initialize(name, surname)
    @name, @surname = name, surname
  end
end

class MyApp < Sinatra::Base

  enable :dump_errors, :raise_errors
  disable :show_exceptions

  get('/string') do
    "Hello World"
  end

  get('/hash') do
    {"person" => { "name" => "john", "surname" => "smith" }}
  end

  get('/array') do
    [1,2,3,4,5,6,7, "232323", '3245235']
  end

  get('/object') do
    Person.new('simon', 'hernandez')
  end

  get('/error') do
    raise 'Failure of some sort'
  end
end

class ResponseMiddleware
  def initialize(app)
    @app = app
  end

  def call(env)
    begin
      status, headers, body = @app.call(env)
      response = {'status' => 'success', 'data' => body}
      format(status, headers, response)
    rescue ::Exception => e
      response = {'status' => 'error', 'message' => e.message}
      format(500, {'Content-Type' => 'application/json'}, response)
    end
  end

  def format(status, headers, response)
    result = ActiveSupport::JSON.encode(response)
    headers["Content-Length"] = result.length.to_s
    [status, headers, result]
  end
end

use ResponseMiddleware
run MyApp

示例(以 JSON 格式):

/string
  Expected: {"status":"success","data":"Hello World"}
  Actual:   {"status":"success","data":["Hello World"]}

/hash (works)
  Expected: {"status":"success","data":{"person":{"name":"john","surname":"smith"}}}
  Actual:   {"status":"success","data":{"person":{"name":"john","surname":"smith"}}}

/array
  Expected: {"status":"success","data": [1,2,3,4,5,6,7,"232323","3245235"]}
  Actual:   {"status":"error","message":"wrong number of arguments (7 for 1)"}

/object
  Expected: {"status":"success","data":{"name":"simon","surname":"hernandez"}}
  Actual:   {"status":"success","data":[]}

/error (works)
  Expected: {"status":"error","message":"Failure of some sort"}
  Actual:   {"status":"error","message":"Failure of some sort"}

如果您执行代码,您将看到 /hash 和 /error 会返回所需的响应,但其余的则不会。理想情况下,我不想更改 MyApp 类中的任何内容。它目前正在 Sinatra 1.3.3、ActiveSupport 3.2.9 和 Rack 1.4.1 之上构建。

4

1 回答 1

1

在 irc.freenode.org 上的#sinatra 的帮助下,我设法将它归结为我想要的。我将以下内容添加到MyApp

def route_eval
  result = catch(:halt) { super }
  throw :halt, {"result" => result}
end

然后我在 ResponseMiddleware 中更改了以下行:

response = {'status' => 'success', 'data' => body}

response = {'status' => 'success', 'data' => body["result"]}

我所有的测试用例都通过了。

于 2012-12-31T14:49:04.177 回答