Sha256: b5834b5e37c267490f8fe20173fa4944772ef9b6a3a0348586bf41310038a347

Contents?: true

Size: 758 Bytes

Versions: 11

Compression:

Stored size: 758 Bytes

Contents

require 'rest-core/middleware'
require 'rest-core/util/json'

class RestCore::JsonResponse
  def self.members; [:json_response]; end
  include RestCore::Middleware

  JSON_RESPONSE_HEADER = {'Accept' => 'application/json'}.freeze

  def call env, &k
    return app.call(env, &k) if env[DRY]
    return app.call(env, &k) unless json_response(env)

    app.call(env.merge(REQUEST_HEADERS =>
      JSON_RESPONSE_HEADER.merge(env[REQUEST_HEADERS]||{}))){ |response|
        yield(process(response))
      }
  end

  def process response
    response.merge(RESPONSE_BODY =>
      Json.decode("[#{response[RESPONSE_BODY]}]").first)
      # [this].first is not needed for yajl-ruby
  rescue Json.const_get(:ParseError) => error
    fail(response, error)
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
rest-core-3.1.1 lib/rest-core/middleware/json_response.rb
rest-core-3.1.0 lib/rest-core/middleware/json_response.rb
rest-core-3.0.0 lib/rest-core/middleware/json_response.rb
rest-core-2.1.2 lib/rest-core/middleware/json_response.rb
rest-core-2.1.1 lib/rest-core/middleware/json_response.rb
rest-core-2.1.0 lib/rest-core/middleware/json_response.rb
rest-core-2.0.4 lib/rest-core/middleware/json_response.rb
rest-core-2.0.3 lib/rest-core/middleware/json_response.rb
rest-core-2.0.2 lib/rest-core/middleware/json_response.rb
rest-core-2.0.1 lib/rest-core/middleware/json_response.rb
rest-core-2.0.0 lib/rest-core/middleware/json_response.rb