Class: Restfulie::Client::Feature::SerializeBody

Inherits:
Object
  • Object
show all
Defined in:
lib/restfulie/client/feature/serialize_body.rb

Overview

Feature responsible for marshalling the entity into the request body. It will use the request content type header to select the proper marshaller using Medie.

Instance Method Summary collapse

Instance Method Details

#execute(flow, request, env = {}) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/restfulie/client/feature/serialize_body.rb', line 5

def execute(flow, request, env = {})
  
  if should_have_payload?(request.verb)
    
    env = env.dup
    payload = env[:payload] = env[:body]
    if payload && !(payload.kind_of?(String) && payload.empty?)
      type = request.headers['Content-Type']
      raise Restfulie::Common::Error::RestfulieError, "Missing content type related to the data to be submitted" unless type
    
      marshaller = Medie.registry.for(type)
      raise Restfulie::Common::Error::RestfulieError, "Missing content type for #{type} related to the data to be submitted" unless marshaller

      rel = request.respond_to?(:rel) ? request.rel : ""
      env[:body] = marshaller.marshal(payload, { :rel => rel, :recipe => env[:recipe] })
    end
    
  end

  flow.continue(request, env)
end