Sha256: 421249e23dfe993c51bff0c09df1bab0fd859ee50342214379b5b3018a0a38f7

Contents?: true

Size: 1021 Bytes

Versions: 2

Compression:

Stored size: 1021 Bytes

Contents

class Restfulie::Client::Feature::SerializeBody
  
  def execute(flow, request, response, env)
    
    if should_have_payload?(request.verb)
      
      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 = Restfulie::Common::Converter.content_type_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, response, env)
  end
  
  protected
  
  PAYLOAD_METHODS = {:put=>true,:post=>true,:patch=>true}
  
  def should_have_payload?(method)
    PAYLOAD_METHODS[method]
  end
  
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
restfulie-1.0.0.beta1 lib/restfulie/client/feature/serialize_body.rb
restfulie-0.1.0.beta1 lib/restfulie/client/feature/serialize_body.rb