module Soaspec # Convenience methods for once off usage of a REST request module RestExchangeFactory # Make REST Exchange with 'post' method within this Handler context # @param [Hash, String] params Exchange parameters. If String is used it will be for the request payload # @option override_parameters [Hash] :params Extra parameters (E.g. headers) # @option override_parameters [String] suburl URL appended to base_url of class # @option override_parameters [Hash] :q Query for REST # Following are for the body of the request # @option override_parameters [Hash] :body Hash to be converted to JSON in request body # @option override_parameters [String] :payload String to be passed directly in request body # @option override_parameters [String] :template_name Path to file to be read via ERB and passed in request body # @return [Exchange] Instance of Exchange class. Assertions are made by default on the response body def post(params = {}) # This is a stub, used for indexing, source code below end # Make REST Exchange with 'patch' method within this Handler context # @param [Hash, String] params Exchange parameters. If String is used it will be for the request payload # @option override_parameters [Hash] :params Extra parameters (E.g. headers) # @option override_parameters [String] suburl URL appended to base_url of class # @option override_parameters [Hash] :q Query for REST # Following are for the body of the request # @option override_parameters [Hash] :body Hash to be converted to JSON in request body # @option override_parameters [String] :payload String to be passed directly in request body # @option override_parameters [String] :template_name Path to file to be read via ERB and passed in request body # @return [Exchange] Instance of Exchange class. Assertions are made by default on the response body def patch(params) # This is a stub, used for indexing, source code below end # Make REST Exchange with 'put' method within this Handler context # @param [Hash, String] params Exchange parameters. If String is used it will be for the request payload # @option override_parameters [Hash] :params Extra parameters (E.g. headers) # @option override_parameters [String] suburl URL appended to base_url of class # @option override_parameters [Hash] :q Query for REST # Following are for the body of the request # @option override_parameters [Hash] :body Hash to be converted to JSON in request body # @option override_parameters [String] :payload String to be passed directly in request body # @option override_parameters [String] :template_name Path to file to be read via ERB and passed in request body # @return [Exchange] Instance of Exchange class. Assertions are made by default on the response body def put(params = {}) # This is a stub, used for indexing, source code below end # Make REST Exchange with 'get' method within this Handler context. # If merely a string is passed it will be used as the URL appended to base_url. Otherwise a Hash is expected # @param [Hash, String] params Exchange parameters. If String is used it will be for suburl # @option override_parameters [Hash] :params Extra parameters (E.g. headers) # @option override_parameters [String] suburl URL appended to base_url of class # @option override_parameters [Hash] :q Query for REST # Following are for the body of the request # @option override_parameters [Hash] :body Hash to be converted to JSON in request body # @option override_parameters [String] :payload String to be passed directly in request body # @option override_parameters [String] :template_name Path to file to be read via ERB and passed in request body # @return [Exchange] Instance of Exchange class. Assertions are made by default on the response body def get(params = {}) # This is a stub, used for indexing, source code below end # Make REST Exchange with 'delete' method within this Handler context. # If merely a string is passed it will be used as the URL appended to base_url. Otherwise a Hash is expected # @param [Hash, String] params Exchange parameters. If String is used it will be for suburl # @option override_parameters [Hash] :params Extra parameters (E.g. headers) # @option override_parameters [String] suburl URL appended to base_url of class # @option override_parameters [Hash] :q Query for REST # Following are for the body of the request # @option override_parameters [Hash] :body Hash to be converted to JSON in request body # @option override_parameters [String] :payload String to be passed directly in request body # @option override_parameters [String] :template_name Path to file to be read via ERB and passed in request body # @return [Exchange] Instance of Exchange class. Assertions are made by default on the response body def delete(params = {}) # This is a stub, used for indexing, source code below end methods = %w[post patch put get delete] methods.each do |rest_method| # Make REST Exchange within this Handler context # @param [Hash, String] params Exchange parameters. # @return [Exchange] Instance of Exchange class. Assertions are made by default on the response body define_method(rest_method) do |params = {}| unless params.is_a? Hash params = case rest_method when 'get', 'delete' { suburl: params.to_s } when 'post', 'put', 'patch' { payload: params.to_s } else raise "'#{params}' needs to be a 'Hash' but is a #{params.class}" end end params[:name] ||= rest_method exchange_params = { name: params[:name] } if params[:template_name] exchange_params[:template_name] = params[:template_name] params.delete :template_name end new(exchange_params) Exchange.new(params[:name], method: rest_method.to_sym, **params) end end end end