lib/soaspec/exchange_handlers/rest_handler.rb in soaspec-0.3.2 vs lib/soaspec/exchange_handlers/rest_handler.rb in soaspec-0.3.3
- old
+ new
@@ -36,55 +36,18 @@
name = self.class.to_s
end
super
set_remove_keys(options, %i[api_username default_hash template_name])
@init_options = options
- init_merge_options # Call this to verify any issues with options on creating object
+ @merged_options ||= init_merge_options # Caches initial merge options
end
- # @return [Boolean] Whether REST method should have a payload
- def payload?(overall_params)
- case overall_params[:method]
- when :post, :patch, :put
- true
- else
- false
- end
- end
-
- # @todo Use this in actually making the request
- # At the moment this is just for one to manually view the parameters
- # that will be used in making a request
- # @return [RestRequest] Parameters used in making a request
+ # @return [Soaspec::RestRequest] Parameters used in making a request
def request_parameters(override_parameters)
- overall_params = interpret_parameters(override_parameters)
- request = { overall: overall_params, options: init_merge_options }
- request[:body] = post_data(overall_params) if payload?(overall_params)
- RestRequest.new(overall_params, init_merge_options, payload?(overall_params) ? post_data(overall_params) : nil)
+ RestRequest.new(override_parameters, @merged_options, self)
end
- # Interpret REST parameters given provided parameters and adding defaults, making
- # transformations
- #
- # @param [Hash] request_parameters Parameters used in making a request
- # @return [Hash] Request parameters merged with default values
- def interpret_parameters(request_parameters)
- request_parameters[:params] ||= {}
- request_parameters[:method] ||= :post
- suburl = request_parameters[:suburl]
- if suburl
- if suburl.is_a? Array
- request_parameters[:suburl] = suburl.collect(&:to_s).join('/')
- else
- request_parameters[:suburl] = suburl.to_s
- end
- end
- # Use q for query parameters. Nested :params is ugly, long and unclear
- request_parameters[:params][:params] = request_parameters[:q] if request_parameters[:q]
- request_parameters
- end
-
# Override this with 'after_response' within class definition to perform an action
# after response is retrieved
# @param [RestClient::Response] _response Response to interpret to perform after block
def after_response(_response, _self); end
@@ -98,24 +61,23 @@
# @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 [RestClient::Response] Response from making request
def make_request(override_parameters)
- @merged_options ||= init_merge_options # TODO: Is this var needed? Can method be passed to resource creation?
- test_values = interpret_parameters override_parameters
+ req_params = request_parameters override_parameters
# In order for ERB to be calculated at correct time, the first time request is made, the resource should be created
@resource ||= RestClient::Resource.new(ERB.new(base_url_value).result(binding), @merged_options)
- @resource_used = test_values[:suburl] ? @resource[test_values[:suburl]] : @resource
+ @resource_used = req_params.suburl ? @resource[req_params.suburl] : @resource
self.exception = nil # Remove any previously stored exception
begin
- response = case test_values[:method]
+ response = case req_params.method
when :post, :patch, :put
- Soaspec::SpecLogger.info("request body: #{post_data(test_values)}")
- @resource_used.send(test_values[:method].to_s, post_data(test_values), test_values[:params])
+ Soaspec::SpecLogger.info("request body: #{req_params.post_data}")
+ @resource_used.send(req_params.method, req_params.post_data, req_params.other_params)
else # :get, :delete
- @resource_used.send(test_values[:method].to_s, test_values[:params])
+ @resource_used.send(req_params.method, req_params.other_params )
end
rescue RestClient::Exception => e
self.exception = e
raise e unless e.respond_to? :response
@@ -124,10 +86,21 @@
Soaspec::SpecLogger.info("response: \n headers: #{response&.headers}\n body: #{response}\n")
after_response(response, self)
response
end
+ # @param [Hash] override_hash Values to override default hash with
+ # @return [Hash] Hash used in REST request based on data conversion
+ def hash_used_in_request(override_hash)
+ request = override_hash ? @default_hash.merge(override_hash) : @default_hash
+ if pascal_keys?
+ request.map { |k, v| [convert_to_pascal_case(k.to_s), v] }.to_h
+ else
+ request
+ end
+ end
+
# Add values to here when extending this class to have default REST options.
# See rest client resource at https://github.com/rest-client/rest-client for details
# It's easier to set headers via 'headers' accessor rather than here
# @return [Hash] Options adding to & overriding defaults
def rest_resource_options
@@ -143,10 +116,11 @@
[header_name, ERB.new(header_value).result(binding)]
end]
end
# Initialize value of merged options
+ # Also called to verify any issues with options on creating object
# @return [Hash] Hash of merged options
def init_merge_options
options = rest_resource_options
options.merge! basic_auth_params if respond_to? :basic_auth_params
options[:headers] ||= {}
@@ -177,10 +151,13 @@
end
# @return [Boolean] Whether response body contains expected key
def include_key?(response, expected)
value_from_path(response, expected)
+ true
+ rescue NoElementAtPath
+ false
end
# @return [Integer] HTTP Status code for response
def status_code_for(response)
response.code
@@ -268,40 +245,16 @@
else
raise "Unable to interpret type of #{response.body}"
end
end
- # @return [RestClient::Request] Request of API call. Either intended request or actual request
+ # @return [RestClient::Request, String] Request of API call. Either intended request or actual request
def request(response)
- return 'Request not yet sent' if response.nil?
+ if response.nil?
+ return 'Request not yet sent. Call :request_parameters' \
+ 'for what will be sent'
+ end
response.request
- end
-
- # Work out data to send based upon payload, template_name, or body
- # @return [String] Payload to send in REST request
- def post_data(test_values)
- data = if @request_option == :hash && !test_values[:payload]
- test_values[:payload] = JSON.generate(hash_used_in_request(test_values[:body])).to_s
- elsif @request_option == :template
- test_values = test_values[:body].dup if test_values[:body]
- test_values = IndifferentHash.new(test_values) # Allow test_values to be either Symbol or String
- Soaspec::TemplateReader.new.render_body(template_name, binding)
- else
- test_values[:payload]
- end
- # Soaspec::SpecLogger.info "Request Empty for '#{@request_option}'" if data.strip.empty?
- data
- end
-
- # @param [Hash] override_hash Values to override default hash with
- # @return [Hash] Hash used in REST request based on data conversion
- def hash_used_in_request(override_hash)
- request = override_hash ? @default_hash.merge(override_hash) : @default_hash
- if pascal_keys?
- request.map { |k, v| [convert_to_pascal_case(k.to_s), v] }.to_h
- else
- request
- end
end
end
end