Class: RestCore::DefaultPayload

Inherits:
Object
  • Object
show all
Includes:
Middleware
Defined in:
lib/rest-core/middleware/default_payload.rb

Constant Summary

Constants included from Middleware

Middleware::UNRESERVED

Constants included from RestCore

ASYNC, CLIENT, DRY, FAIL, HIJACK, LOG, PROMISE, REQUEST_HEADERS, REQUEST_METHOD, REQUEST_PATH, REQUEST_PAYLOAD, REQUEST_QUERY, RESPONSE_BODY, RESPONSE_HEADERS, RESPONSE_SOCKET, RESPONSE_STATUS, Simple, TIMER, Universal, VERSION

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Middleware

contain_binary?, #contain_binary?, escape, #escape, #fail, #id, included, #log, #percent_encode, percent_encode, request_uri, #request_uri, #run, #string_keys, string_keys

Methods included from RestCore

eagerload, id

Constructor Details

#initialize(*args) ⇒ DefaultPayload

Returns a new instance of DefaultPayload.



8
9
10
11
# File 'lib/rest-core/middleware/default_payload.rb', line 8

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

Class Method Details

.membersObject



5
# File 'lib/rest-core/middleware/default_payload.rb', line 5

def self.members; [:payload]; end

Instance Method Details

#call(env, &k) ⇒ Object



13
14
15
16
17
18
# File 'lib/rest-core/middleware/default_payload.rb', line 13

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

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

#merge(lhs, rhs) ⇒ Object

this method is intended to merge payloads if they are non-empty hashes, but prefer the right most one if they are not hashes.



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/rest-core/middleware/default_payload.rb', line 22

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