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.
-
#extract_hash(response) ⇒ Hash
Convert XML or JSON response into a Hash.
-
#found?(response) ⇒ Boolean
Whether the request found the desired value or not.
- #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.
-
#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 ⇒ Object
-
#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
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
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/soaspec/exchange_handlers/rest_handler.rb', line 113 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) = [:headers] ||= {} [:headers].merge! parse_headers .merge!() @resource = RestClient::Resource.new(base_url_value, ) # @resource[url_extension].get end |
Instance Attribute Details
#client ⇒ Object
Savon client used to make SOAP calls
83 84 85 |
# File 'lib/soaspec/exchange_handlers/rest_handler.rb', line 83 def client @client end |
#operation ⇒ Object
SOAP Operation to use by default
85 86 87 |
# File 'lib/soaspec/exchange_handlers/rest_handler.rb', line 85 def operation @operation end |
Instance Method Details
#base_url_value ⇒ Object
Set through following method. Base URL in REST requests.
88 89 90 |
# File 'lib/soaspec/exchange_handlers/rest_handler.rb', line 88 def base_url_value nil end |
#extract_hash(response) ⇒ Hash
Convert XML or JSON response into a Hash
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/soaspec/exchange_handlers/rest_handler.rb', line 177 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
170 171 172 |
# File 'lib/soaspec/exchange_handlers/rest_handler.rb', line 170 def found?(response) status_code_for(response) != 404 end |
#include_in_body?(response, expected) ⇒ Boolean
165 166 167 |
# File 'lib/soaspec/exchange_handlers/rest_handler.rb', line 165 def include_in_body?(response, expected) response.body.include? expected end |
#include_key?(response, expected) ⇒ Boolean
Returns Whether response body contains expected key.
199 200 201 |
# File 'lib/soaspec/exchange_handlers/rest_handler.rb', line 199 def include_key?(response, expected) value_from_path(response, expected) end |
#include_value?(response, expected) ⇒ Boolean
Returns Whether response contains expected value.
194 195 196 |
# File 'lib/soaspec/exchange_handlers/rest_handler.rb', line 194 def include_value?(response, expected) extract_hash(response).include_value? expected end |
#make_request(override_parameters) ⇒ Object
Used in together with Exchange request that passes such override parameters
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/soaspec/exchange_handlers/rest_handler.rb', line 134 def make_request(override_parameters) test_values = override_parameters test_values[:params] ||= {} test_values[:suburl] = test_values[:suburl].to_s if test_values[:suburl] @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(@default_hash.merge(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
211 212 213 |
# File 'lib/soaspec/exchange_handlers/rest_handler.rb', line 211 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
218 219 220 |
# File 'lib/soaspec/exchange_handlers/rest_handler.rb', line 218 def mandatory_xpath_values {} end |
#parse_headers ⇒ Object
106 107 108 109 |
# File 'lib/soaspec/exchange_handlers/rest_handler.rb', line 106 def parse_headers # rest_client_headers.map { |h| ERB.new(h).result(binding) } Hash[rest_client_headers.map { |k, header| [k, ERB.new(header).result(binding)] }] end |
#response_body(response, format: :hash) ⇒ Object
Returns Generic body to be displayed in error messages.
161 162 163 |
# File 'lib/soaspec/exchange_handlers/rest_handler.rb', line 161 def response_body(response, format: :hash) extract_hash response end |
#rest_client_headers ⇒ Object
Headers used in RestClient
93 94 95 |
# File 'lib/soaspec/exchange_handlers/rest_handler.rb', line 93 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
101 102 103 104 |
# File 'lib/soaspec/exchange_handlers/rest_handler.rb', line 101 def { } end |
#root_attributes ⇒ Object
Attributes set at the root XML element of SOAP request
223 224 225 |
# File 'lib/soaspec/exchange_handlers/rest_handler.rb', line 223 def root_attributes nil end |
#status_code_for(response) ⇒ Integer
Returns HTTP Status code for response.
204 205 206 |
# File 'lib/soaspec/exchange_handlers/rest_handler.rb', line 204 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
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 |
# File 'lib/soaspec/exchange_handlers/rest_handler.rb', line 253 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? path = '//' + path if path[0] != '/' xpath_value_for(response: response, xpath: path, attribute: attribute) when :json raise 'JSON does not support attributes' if attribute path = '$..' + path if path[0] != '$' 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
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 |
# File 'lib/soaspec/exchange_handlers/rest_handler.rb', line 231 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 |