Sha256: ae20da5271cfe5d465b464939149d4dd19f5950d18a35d11dacd92ad23ae5ec0

Contents?: true

Size: 1.97 KB

Versions: 1

Compression:

Stored size: 1.97 KB

Contents

module WashOut
  # The WashOut::Dispatcher module should be included in a controller acting
  # as a SOAP endpoint. It includes actions for generating WSDL and handling
  # SOAP requests.
  module Dispatcher
    # A SOAPError exception can be raised to return a correct SOAP error
    # response.
    class SOAPError < Exception; end

    # This action generates the WSDL for defined SOAP methods.
    def _wsdl
      @map       = self.class.soap_actions
      @namespace = 'urn:WashOut'
      @name      = controller_path.gsub('/', '_')

      render :template => 'wash_with_soap/wsdl'
    end

    # This action maps the SOAP action to a controller method defined with
    # +soap_action+.
    def _action
      map       = self.class.soap_actions
      method    = request.env['HTTP_SOAPACTION'].gsub(/^\"(.*)\"$/, '\1')
      @_current = map[method]

      raise SoapError, "Method #{method} does not exists" unless @_current

      xml_data = params['Envelope']['Body'][method]

      params = (xml_data || {}).map do |opt, value|
        opt = opt.underscore
        param = @_current[:in].find { |param| param.name == opt }
        raise SOAPError, "unknown parameter #{opt}" unless param
        [ opt, param.load(value) ]
      end
      @_params.merge!(Hash[*params.flatten])

      send(@_current[:to])
    end

    def _render_soap(result, options)
      result = { 'value' => result } unless result.is_a? Hash
      result = HashWithIndifferentAccess.new(result)
      result = Hash[*@_current[:out].map do |param|
        [param, param.store(result[param.name])]
      end.flatten]

      render :template => 'wash_with_soap/response',
             :locals => { :result => result }
    end

    private

    def self.included(controller)
      controller.send :rescue_from, SOAPError, :with => :_render_soap_error
    end

    def _render_soap_error(error)
      render :template => 'wash_with_soap/error', :status => 500,
             :locals => { :error_message => error.message }
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
wash_out-0.2.0 lib/wash_out/dispatcher.rb