Class: Soaspec::RestHandler
- Inherits:
-
ExchangeHandler
- Object
- ExchangeHandler
- Soaspec::RestHandler
- Extended by:
- RestExchangeFactory, RestParameters
- Includes:
- ResponseExtractor, RestParametersDefaults
- 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
-
#api_username ⇒ Object
User used in making API calls.
Attributes inherited from ExchangeHandler
Instance Method Summary collapse
-
#convert_to_pascal_case(key) ⇒ Object
Convert snakecase to PascalCase.
-
#found?(response) ⇒ Boolean
@@return [Boolean] Whether the request found the desired value or not.
-
#include_in_body?(response, expected) ⇒ Boolean
Whether response body includes String.
-
#include_key?(response, expected) ⇒ Boolean
Whether response body contains expected key.
-
#include_value?(response, expected) ⇒ Boolean
Whether response contains expected value.
-
#init_merge_options ⇒ Hash
Initialize value of merged options.
-
#initialize(name = self.class.to_s, options = {}) ⇒ RestHandler
constructor
Setup object to handle communicating with a particular SOAP WSDL.
-
#json_path_values_for(response, path, attribute: nil) ⇒ Enumerable
List of values matching JSON path.
-
#make_request(override_parameters) ⇒ RestClient::Response
Used in together with Exchange request that passes such override parameters Following are for the body of the request.
-
#parse_headers ⇒ Hash
Perform ERB on each header value.
- #request(response) ⇒ Object
-
#response_body(response, format: :hash) ⇒ Object
Generic body to be displayed in error messages.
-
#rest_resource_options ⇒ Hash
Add values to here when extending this class to have default REST options.
-
#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.
-
#values_from_path(response, path, attribute: nil) ⇒ Enumerable
List of values returned from path.
-
#xpath_elements_for(response: nil, xpath: nil, attribute: nil) ⇒ Enumerable
Returns the value at the provided xpath.
Methods included from RestParameters
base_url, basic_auth, basic_auth_file, client_id, headers, oauth2, oauth2_file, pascal_keys
Methods included from RestExchangeFactory
Methods included from RestParametersDefaults
#base_url_value, #pascal_keys?, #rest_client_headers
Methods included from ResponseExtractor
Methods inherited from ExchangeHandler
#convert_to_lower?, #default_hash=, #elements, #expected_mandatory_elements, #expected_mandatory_json_values, #expected_mandatory_xpath_values, #set_remove_key, #set_remove_keys, #store, #strip_namespaces?, #to_s, #use
Methods included from HandlerAccessors
#attribute, #convert_to_lower, #default_hash, #element, #mandatory_elements, #mandatory_json_values, #mandatory_xpath_values, #strip_namespaces, #template_name
Constructor Details
#initialize(name = self.class.to_s, options = {}) ⇒ RestHandler
Setup object to handle communicating with a particular SOAP WSDL
28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/soaspec/exchange_handlers/rest_handler.rb', line 28 def initialize(name = self.class.to_s, = {}) raise "Base URL not set! Please set in class with 'base_url' method" unless base_url_value if name.is_a?(Hash) && == {} # If name is not set, use first parameter as the options hash = name name = self.class.to_s end super set_remove_keys(, %i[api_username default_hash template_name]) @init_options = # Call this to verify any issues with options on creating object end |
Instance Attribute Details
#api_username ⇒ Object
User used in making API calls
24 25 26 |
# File 'lib/soaspec/exchange_handlers/rest_handler.rb', line 24 def api_username @api_username end |
Instance Method Details
#convert_to_pascal_case(key) ⇒ Object
Convert snakecase to PascalCase
99 100 101 102 103 |
# File 'lib/soaspec/exchange_handlers/rest_handler.rb', line 99 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 |
#found?(response) ⇒ Boolean
@@return [Boolean] Whether the request found the desired value or not
128 129 130 |
# File 'lib/soaspec/exchange_handlers/rest_handler.rb', line 128 def found?(response) status_code_for(response) != 404 end |
#include_in_body?(response, expected) ⇒ Boolean
Returns Whether response body includes String.
123 124 125 |
# File 'lib/soaspec/exchange_handlers/rest_handler.rb', line 123 def include_in_body?(response, expected) response.body.include? expected end |
#include_key?(response, expected) ⇒ Boolean
Returns Whether response body contains expected key.
138 139 140 |
# File 'lib/soaspec/exchange_handlers/rest_handler.rb', line 138 def include_key?(response, expected) value_from_path(response, expected) end |
#include_value?(response, expected) ⇒ Boolean
Returns Whether response contains expected value.
133 134 135 |
# File 'lib/soaspec/exchange_handlers/rest_handler.rb', line 133 def include_value?(response, expected) extract_hash(response).include_value? expected end |
#init_merge_options ⇒ Hash
Initialize value of merged options
107 108 109 110 111 112 113 114 |
# File 'lib/soaspec/exchange_handlers/rest_handler.rb', line 107 def = .merge! basic_auth_params if respond_to? :basic_auth_params [:headers] ||= {} [:headers].merge! parse_headers [:headers][:authorization] ||= ERB.new('Bearer <%= access_token %>').result(binding) if Soaspec.auto_oauth && respond_to?(:access_token) .merge(@init_options) end |
#json_path_values_for(response, path, attribute: nil) ⇒ Enumerable
Returns List of values matching JSON path.
167 168 169 170 171 172 173 174 175 |
# File 'lib/soaspec/exchange_handlers/rest_handler.rb', line 167 def json_path_values_for(response, path, attribute: nil) raise 'JSON does not support attributes' if attribute if path[0] != '$' path = convert_to_pascal_case(path) if pascal_keys? path = '$..' + path end JsonPath.on(response.body, path) end |
#make_request(override_parameters) ⇒ RestClient::Response
Used in together with Exchange request that passes such override parameters Following are for the body of the request
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/soaspec/exchange_handlers/rest_handler.rb', line 52 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 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 Soaspec::SpecLogger.info(["response_headers: #{response.headers}", "response_body: #{response}"]) response end |
#parse_headers ⇒ Hash
Perform ERB on each header value
90 91 92 93 94 95 96 |
# File 'lib/soaspec/exchange_handlers/rest_handler.rb', line 90 def parse_headers Hash[rest_client_headers.map do |header_name, header_value| raise ArgumentError, "Header '#{header_name}' is null. Headers are #{rest_client_headers}" if header_value.nil? [header_name, ERB.new(header_value).result(binding)] end] end |
#request(response) ⇒ Object
223 224 225 226 227 |
# File 'lib/soaspec/exchange_handlers/rest_handler.rb', line 223 def request(response) return 'Request not yet sent' if response.nil? response.request end |
#response_body(response, format: :hash) ⇒ Object
Returns Generic body to be displayed in error messages.
118 119 120 |
# File 'lib/soaspec/exchange_handlers/rest_handler.rb', line 118 def response_body(response, format: :hash) extract_hash response 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
83 84 85 86 |
# File 'lib/soaspec/exchange_handlers/rest_handler.rb', line 83 def { } end |
#status_code_for(response) ⇒ Integer
Returns HTTP Status code for response.
143 144 145 |
# File 'lib/soaspec/exchange_handlers/rest_handler.rb', line 143 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
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 |
# File 'lib/soaspec/exchange_handlers/rest_handler.rb', line 183 def value_from_path(response, path, attribute: nil) path = path.to_s case Interpreter.response_type_for(response) when :xml result = xpath_elements_for(response: response, xpath: path, attribute: attribute).first raise NoElementAtPath, "No value at Xpath '#{prefix_xpath(path, attribute)}'" unless result return result.inner_text if attribute.nil? return result.attributes[attribute].inner_text when :json paths_to_check = path.split(',') matching_values = paths_to_check.collect do |path_to_check| json_path_values_for(response, path_to_check, attribute: attribute) end.reject(&:empty?) raise NoElementAtPath, "Path '#{path}' not found in '#{response.body}'" if matching_values.empty? matching_values.first.first when :hash response.dig(path.split('.')) # Use path as Hash dig expression separating params via '.' TODO: Unit test else raise NoElementAtPath, 'Response is empty' if response.to_s.empty? response.to_s[/path/] # Perform regular expression using path if not XML nor JSON TODO: Unit test end end |
#values_from_path(response, path, attribute: nil) ⇒ Enumerable
Returns List of values returned from path.
210 211 212 213 214 215 216 217 218 219 220 |
# File 'lib/soaspec/exchange_handlers/rest_handler.rb', line 210 def values_from_path(response, path, attribute: nil) path = path.to_s case Interpreter.response_type_for(response) when :xml xpath_elements_for(response: response, xpath: path, attribute: attribute).map(&:inner_text) when :json json_path_values_for(response, path, attribute: attribute) else raise "Unable to interpret type of #{response.body}" end end |
#xpath_elements_for(response: nil, xpath: nil, attribute: nil) ⇒ Enumerable
Returns the value at the provided xpath
152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/soaspec/exchange_handlers/rest_handler.rb', line 152 def xpath_elements_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 xpath = prefix_xpath(xpath, attribute) temp_doc = Nokogiri.parse(response.body).dup if strip_namespaces? && !xpath.include?(':') temp_doc.remove_namespaces! temp_doc.xpath(xpath) else temp_doc.xpath(xpath, temp_doc.collect_namespaces) end end |