Class: Soaspec::RestHandler
- Inherits:
-
ExchangeHandler
- Object
- ExchangeHandler
- Soaspec::RestHandler
- Extended by:
- RestAccessors
- Defined in:
- lib/soaspec/exchange_handlers/rest_handler.rb
Overview
Wraps around Savon client defining default values dependent on the soap request
Instance Attribute Summary collapse
-
#client ⇒ Object
Savon client used to make SOAP calls.
-
#operation ⇒ Object
SOAP Operation to use by default.
Attributes inherited from ExchangeHandler
Instance Method Summary collapse
-
#base_url_value ⇒ Object
Set through following method.
-
#convert_to_pascal_case(key) ⇒ Object
Convert snakecase to PascalCase.
-
#extract_hash(response) ⇒ Hash
Convert XML or JSON response into a Hash.
-
#found?(response) ⇒ Boolean
Whether the request found the desired value or not.
- #hash_used_in_request(override_hash) ⇒ Hash
- #include_in_body?(response, expected) ⇒ Boolean
-
#include_key?(response, expected) ⇒ Boolean
Whether response body contains expected key.
-
#include_value?(response, expected) ⇒ Boolean
Whether response contains expected value.
-
#init_merge_options ⇒ Object
Initialize value of merged options.
-
#initialize(name = self.class.to_s, options = {}) ⇒ RestHandler
constructor
Setup object to handle communicating with a particular SOAP WSDL.
-
#make_request(override_parameters) ⇒ Object
Used in together with Exchange request that passes such override parameters.
-
#mandatory_elements ⇒ Array
Override this to specify elements that must be present in the response Will be used in ‘success_scenarios’ shared examples.
-
#mandatory_xpath_values ⇒ Hash
Override this to specify xpath results that must be present in the response Will be used in ‘success_scenarios’ shared examples.
-
#parse_headers ⇒ Hash
Perform ERB on each header value.
-
#pascal_keys? ⇒ Boolean
Whether to convert each key in the request to PascalCase It will also auto convert simple XPath, JSONPath where ‘//’ or ‘..’ not specified.
-
#response_body(response, _format: :hash) ⇒ Object
Generic body to be displayed in error messages.
-
#rest_client_headers ⇒ Object
Headers used in RestClient.
-
#rest_resource_options ⇒ Hash
Add values to here when extending this class to have default REST options.
-
#root_attributes ⇒ Object
Attributes set at the root XML element of SOAP request.
-
#status_code_for(response) ⇒ Integer
HTTP Status code for response.
-
#value_from_path(response, path, attribute: nil) ⇒ String
Based on a exchange, return the value at the provided xpath If the path does not begin with a ‘/’, a ‘//’ is added to it.
-
#xpath_value_for(response: nil, xpath: nil, attribute: nil) ⇒ String
Returns the value at the provided xpath.
Methods included from RestAccessors
base_url, headers, oauth2, oauth2_file, pascal_keys
Methods inherited from ExchangeHandler
#default_hash=, #elements, #expected_mandatory_elements, #expected_mandatory_json_values, #expected_mandatory_xpath_values, #set_remove_key, #store, #to_s, #use
Methods included from HandlerAccessors
#attribute, #element, #mandatory_json_values
Constructor Details
#initialize(name = self.class.to_s, options = {}) ⇒ RestHandler
Setup object to handle communicating with a particular SOAP WSDL
135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/soaspec/exchange_handlers/rest_handler.rb', line 135 def initialize(name = self.class.to_s, = {}) raise "Base URL not set! Please set in class with 'base_url' method" unless base_url_value @default_hash = {} if name.is_a?(Hash) && == {} # If name is not set = name name = self.class.to_s end super set_remove_key(, :default_hash) @init_options = end |
Instance Attribute Details
#client ⇒ Object
Savon client used to make SOAP calls
104 105 106 |
# File 'lib/soaspec/exchange_handlers/rest_handler.rb', line 104 def client @client end |
#operation ⇒ Object
SOAP Operation to use by default
106 107 108 |
# File 'lib/soaspec/exchange_handlers/rest_handler.rb', line 106 def operation @operation end |
Instance Method Details
#base_url_value ⇒ Object
Set through following method. Base URL in REST requests.
109 110 111 |
# File 'lib/soaspec/exchange_handlers/rest_handler.rb', line 109 def base_url_value nil end |
#convert_to_pascal_case(key) ⇒ Object
Convert snakecase to PascalCase
148 149 150 151 |
# File 'lib/soaspec/exchange_handlers/rest_handler.rb', line 148 def convert_to_pascal_case(key) return key if /[[:upper:]]/ =~ key[0] # If first character already capital, don't do conversion key.split('_').map(&:capitalize).join end |
#extract_hash(response) ⇒ Hash
Convert XML or JSON response into a Hash
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 |
# File 'lib/soaspec/exchange_handlers/rest_handler.rb', line 232 def extract_hash(response) raise ArgumentError("Empty Body. Can't assert on it") if response.body.empty? case Interpreter.response_type_for response when :json converted = JSON.parse(response.body) return converted.transform_keys_to_symbols if converted.is_a? Hash return converted.map!(&:transform_keys_to_symbols) if converted.is_a? Array raise 'Incorrect Type prodcued ' + converted.class when :xml parser = Nori.new(convert_tags_to: lambda { |tag| tag.snakecase.to_sym }) parser.parse(response.body) else raise "Neither XML nor JSON detected. It is #{type}. Don't know how to parse It is #{response.body}" end end |
#found?(response) ⇒ Boolean
Whether the request found the desired value or not
225 226 227 |
# File 'lib/soaspec/exchange_handlers/rest_handler.rb', line 225 def found?(response) status_code_for(response) != 404 end |
#hash_used_in_request(override_hash) ⇒ Hash
161 162 163 164 165 166 167 168 |
# File 'lib/soaspec/exchange_handlers/rest_handler.rb', line 161 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 |
#include_in_body?(response, expected) ⇒ Boolean
220 221 222 |
# File 'lib/soaspec/exchange_handlers/rest_handler.rb', line 220 def include_in_body?(response, expected) response.body.include? expected end |
#include_key?(response, expected) ⇒ Boolean
Returns Whether response body contains expected key.
254 255 256 |
# File 'lib/soaspec/exchange_handlers/rest_handler.rb', line 254 def include_key?(response, expected) value_from_path(response, expected) end |
#include_value?(response, expected) ⇒ Boolean
Returns Whether response contains expected value.
249 250 251 |
# File 'lib/soaspec/exchange_handlers/rest_handler.rb', line 249 def include_value?(response, expected) extract_hash(response).include_value? expected end |
#init_merge_options ⇒ Object
Initialize value of merged options
171 172 173 174 175 176 |
# File 'lib/soaspec/exchange_handlers/rest_handler.rb', line 171 def = [:headers] ||= {} [:headers].merge! parse_headers .merge(@init_options) end |
#make_request(override_parameters) ⇒ Object
Used in together with Exchange request that passes such override parameters
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
# File 'lib/soaspec/exchange_handlers/rest_handler.rb', line 183 def make_request(override_parameters) @merged_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 @resource_used.send(test_values[:method].to_s, test_values[:params]) end rescue RestClient::ExceptionWithResponse => e response = e.response end Soaspec::SpecLogger.add_to('response_headers: ' + response.headers.to_s) Soaspec::SpecLogger.add_to('response_body: ' + response.to_s) response end |
#mandatory_elements ⇒ Array
Override this to specify elements that must be present in the response Will be used in ‘success_scenarios’ shared examples
266 267 268 |
# File 'lib/soaspec/exchange_handlers/rest_handler.rb', line 266 def mandatory_elements [] end |
#mandatory_xpath_values ⇒ Hash
Override this to specify xpath results that must be present in the response Will be used in ‘success_scenarios’ shared examples
273 274 275 |
# File 'lib/soaspec/exchange_handlers/rest_handler.rb', line 273 def mandatory_xpath_values {} end |
#parse_headers ⇒ Hash
Perform ERB on each header value
129 130 131 |
# File 'lib/soaspec/exchange_handlers/rest_handler.rb', line 129 def parse_headers Hash[rest_client_headers.map { |k, header| [k, ERB.new(header).result(binding)] }] end |
#pascal_keys? ⇒ Boolean
Whether to convert each key in the request to PascalCase It will also auto convert simple XPath, JSONPath where ‘//’ or ‘..’ not specified
156 157 158 |
# File 'lib/soaspec/exchange_handlers/rest_handler.rb', line 156 def pascal_keys? false end |
#response_body(response, _format: :hash) ⇒ Object
Returns Generic body to be displayed in error messages.
216 217 218 |
# File 'lib/soaspec/exchange_handlers/rest_handler.rb', line 216 def response_body(response, _format: :hash) extract_hash response end |
#rest_client_headers ⇒ Object
Headers used in RestClient
114 115 116 |
# File 'lib/soaspec/exchange_handlers/rest_handler.rb', line 114 def rest_client_headers {} end |
#rest_resource_options ⇒ Hash
Add values to here when extending this class to have default REST options. See rest client resource at github.com/rest-client/rest-client for details It’s easier to set headers via ‘headers’ accessor rather than here
122 123 124 125 |
# File 'lib/soaspec/exchange_handlers/rest_handler.rb', line 122 def { } end |
#root_attributes ⇒ Object
Attributes set at the root XML element of SOAP request
278 279 280 |
# File 'lib/soaspec/exchange_handlers/rest_handler.rb', line 278 def root_attributes nil end |
#status_code_for(response) ⇒ Integer
Returns HTTP Status code for response.
259 260 261 |
# File 'lib/soaspec/exchange_handlers/rest_handler.rb', line 259 def status_code_for(response) response.code end |
#value_from_path(response, path, attribute: nil) ⇒ String
Based on a exchange, return the value at the provided xpath If the path does not begin with a ‘/’, a ‘//’ is added to it
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 |
# File 'lib/soaspec/exchange_handlers/rest_handler.rb', line 308 def value_from_path(response, path, attribute: nil) path = path.to_s case Interpreter.response_type_for(response) when :xml path = "//*[@#{attribute}]" unless attribute.nil? if path[0] != '/' path = convert_to_pascal_case(path) if pascal_keys? path = '//' + path end xpath_value_for(response: response, xpath: path, attribute: attribute) when :json raise 'JSON does not support attributes' if attribute if path[0] != '$' path = convert_to_pascal_case(path) if pascal_keys? path = '$..' + path end matching_values = JsonPath.on(response.body, path) raise NoElementAtPath, "Element in #{response.body} not found with path '#{path}'" if matching_values.empty? matching_values.first when :hash response.dig(path.split('.')) # Use path as Hash dig expression separating params via '.' TODO: Unit test else response.to_s[/path/] # Perform regular expression using path if not XML nor JSON TODO: Unit test end end |
#xpath_value_for(response: nil, xpath: nil, attribute: nil) ⇒ String
Returns the value at the provided xpath
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 |
# File 'lib/soaspec/exchange_handlers/rest_handler.rb', line 286 def xpath_value_for(response: nil, xpath: nil, attribute: nil) raise ArgumentError unless response && xpath raise "Can't perform XPATH if response is not XML" unless Interpreter.response_type_for(response) == :xml result = if Soaspec.strip_namespaces? && !xpath.include?(':') temp_doc = Nokogiri.parse response.body temp_doc.remove_namespaces! temp_doc.xpath(xpath).first else Nokogiri.parse(response.body).xpath(xpath).first end raise NoElementAtPath, "No value at Xpath '#{xpath}'" unless result return result.inner_text if attribute.nil? result.attributes[attribute].inner_text end |