Sha256: 5a18e425452be3ea5889946d64ae185ac4ec63a74eb65488fe0032185c94de1d

Contents?: true

Size: 759 Bytes

Versions: 5

Compression:

Stored size: 759 Bytes

Contents

require 'rest-core/middleware'

class RestCore::DefaultPayload
  def self.members; [:payload]; end
  include RestCore::Middleware

  def initialize *args
    super
    @payload ||= {}
  end

  def call env, &k
    defaults = merge(@payload, payload(env))

    app.call(env.merge(REQUEST_PAYLOAD =>
      merge(defaults, env[REQUEST_PAYLOAD] || {})), &k)
  end

  # this method is intended to merge payloads if they are non-empty hashes,
  # but prefer the right most one if they are not hashes.
  def merge lhs, rhs
    if rhs.respond_to?(:empty?) && rhs.empty?
      lhs
    elsif lhs.respond_to?(:merge)
      if rhs.respond_to?(:merge)
        string_keys(lhs).merge(string_keys(rhs))
      else
        rhs
      end
    else
      rhs
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
rest-core-2.0.4 lib/rest-core/middleware/default_payload.rb
rest-core-2.0.3 lib/rest-core/middleware/default_payload.rb
rest-core-2.0.2 lib/rest-core/middleware/default_payload.rb
rest-core-2.0.1 lib/rest-core/middleware/default_payload.rb
rest-core-2.0.0 lib/rest-core/middleware/default_payload.rb