lib/soaspec/exchange_handlers/rest_handler.rb in soaspec-0.0.89 vs lib/soaspec/exchange_handlers/rest_handler.rb in soaspec-0.1.0
- old
+ new
@@ -53,10 +53,11 @@
name = self.class.to_s
end
super
set_remove_key(options, :api_username)
set_remove_key(options, :default_hash)
+ set_remove_key(options, :template_name)
@init_options = options
end
# Convert snakecase to PascalCase
def convert_to_pascal_case(key)
@@ -69,20 +70,10 @@
# @return Whether to convert to PascalCase
def pascal_keys?
false
end
- # @return [Hash]
- def hash_used_in_request(override_hash)
- request = @default_hash.merge(override_hash)
- if pascal_keys?
- request.map { |k, v| [convert_to_pascal_case(k.to_s), v] }.to_h
- else
- request
- end
- end
-
# Initialize value of merged options
def init_merge_options
options = rest_resource_options
options[:headers] ||= {}
options[:headers].merge! parse_headers
@@ -91,32 +82,30 @@
# Used in together with Exchange request that passes such override parameters
# @param [Hash] override_parameters Params to characterize REST request
# @param_value [params] Extra parameters (E.g. headers)
# @param_value [suburl] URL appended to base_url of class
+ # @param_value [q] Query for REST
# @param_value [method] REST method (:get, :post, etc)
def make_request(override_parameters)
@merged_options ||= init_merge_options
test_values = override_parameters
test_values[:params] ||= {}
test_values[:method] ||= :post
test_values[:suburl] = test_values[:suburl].to_s if test_values[:suburl]
test_values[:params][:params] = test_values[:q] if test_values[:q] # Use q for query parameters. Nested :params is ugly and long
-
# 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
begin
response = case test_values[:method]
when :post, :patch, :put
- unless test_values[:payload]
- test_values[:payload] = JSON.generate(hash_used_in_request(test_values[:body])).to_s if test_values[:body]
- end
- @resource_used.send(test_values[:method].to_s, test_values[:payload], test_values[:params])
- else
+ Soaspec::SpecLogger.info("request body: #{post_data(test_values)}")
+ @resource_used.send(test_values[:method].to_s, post_data(test_values), test_values[:params])
+ else # :get, :delete
@resource_used.send(test_values[:method].to_s, test_values[:params])
end
rescue RestClient::ExceptionWithResponse => e
response = e.response
end
@@ -129,15 +118,16 @@
# @return [Object] Generic body to be displayed in error messages
def response_body(response, format: :hash)
extract_hash response
end
+ # @return [Boolean] Whether response body includes String
def include_in_body?(response, expected)
response.body.include? expected
end
- # Whether the request found the desired value or not
+ # @@return [Boolean] Whether the request found the desired value or not
def found?(response)
status_code_for(response) != 404
end
# Convert XML or JSON response into a Hash
@@ -270,9 +260,34 @@
parser.parse(response.body.to_s)
when :json
JSON.parse(response.body.to_s)
else
raise "Unable to interpret type of #{response.body}"
+ end
+ end
+
+ private
+
+ # Work out data to send based upon payload, template_name
+ # @return [String] Payload to send in REST request
+ def post_data(test_values)
+ if test_values[:body]
+ test_values[:payload] = JSON.generate(hash_used_in_request(test_values[:body])).to_s
+ elsif @request_option == :template
+ request_body = File.read('template/' + template_name)
+ ERB.new(request_body).result(binding)
+ else
+ test_values[:payload]
+ end
+ end
+
+ # @return [Hash] Hash used in REST request based on data conversion
+ def hash_used_in_request(override_hash)
+ request = @default_hash.merge(override_hash)
+ if pascal_keys?
+ request.map { |k, v| [convert_to_pascal_case(k.to_s), v] }.to_h
+ else
+ request
end
end
# Convenience methods for once off usage of a REST request
class << self
\ No newline at end of file