Sha256: 77464534cce24dfb880ba559ab97096240e472faec4dc863333baf09d0b6b814

Contents?: true

Size: 1.15 KB

Versions: 8

Compression:

Stored size: 1.15 KB

Contents

# 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.
class Restfulie::Client::Feature::SerializeBody
  
  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
  
  protected
  
  PAYLOAD_METHODS = {:put=>true,:post=>true,:patch=>true}
  
  def should_have_payload?(method)
    PAYLOAD_METHODS[method]
  end
  
end

Version data entries

8 entries across 8 versions & 2 rubygems

Version Path
restfulie-nosqlite-1.0.4 lib/restfulie/client/feature/serialize_body.rb
restfulie-1.1.1 lib/restfulie/client/feature/serialize_body.rb
restfulie-1.1.0 lib/restfulie/client/feature/serialize_body.rb
restfulie-nosqlite-1.0.3 lib/restfulie/client/feature/serialize_body.rb
restfulie-1.0.3 lib/restfulie/client/feature/serialize_body.rb
restfulie-1.0.0 lib/restfulie/client/feature/serialize_body.rb
restfulie-1.0.0.beta5 lib/restfulie/client/feature/serialize_body.rb
restfulie-1.0.0.beta4 lib/restfulie/client/feature/serialize_body.rb