lib/soaspec/rest_handler.rb in soaspec-0.0.25 vs lib/soaspec/rest_handler.rb in soaspec-0.0.26

- old
+ new

@@ -1,56 +1,57 @@ require_relative 'tester' require_relative 'hash_methods' require_relative 'xpath_not_found' +require_relative 'accessors' require 'json' require 'nori' module Soaspec + + # Accessors specific to REST handler + module RestAccessors + + # Defines method 'base_url_value' containing base URL used in REST requests + # @param [String] url Base Url to use in REST requests. Suburl is appended to this + def base_url(url) + define_method('base_url_value') do + url + end + end + end + # Wraps around Savon client defining default values dependent on the soap request class RestHandler < Tester + extend Soaspec::RestAccessors + extend Soaspec::Accessors + # Savon client used to make SOAP calls attr_accessor :client # SOAP Operation to use by default attr_accessor :operation - # Options to log xml request and response - def logging_options - { - # See request and response. (Put this in traffic file) - } + # Set through following method. Base URL in REST requests. + def base_url_value + nil end - # Default Savon options. See https://github.com/rest-client/rest-client for details - # @return [Hash] Default Savon options for all BasicSoapHandler - def default_options - { - # method: :get - # headers: { content_type: 'text/plain' } - } - end - - # Override in class - def base_url - '' - end - # Add values to here when extending this class to have default REST options. - # See rest client resource for details + # See rest client resource at https://github.com/rest-client/rest-client for details # @return [Hash] Options adding to & overriding defaults def rest_resource_options { } end # Setup object to handle communicating with a particular SOAP WSDL # @param [Hash] specific_options Options defining SOAP request. WSDL, authentication def initialize(name, specific_options = {}) - options = default_options.merge logging_options - options.merge! rest_resource_options + raise "Base URL not set! Please set in class with 'base_url' method" unless base_url_value + options = rest_resource_options options.merge!(specific_options) - @resource = RestClient::Resource.new(base_url, options) # @resource[url_extension].get + @resource = RestClient::Resource.new(base_url_value, options) # @resource[url_extension].get super end def name(name) @test_values = {} @@ -90,10 +91,12 @@ def include_in_body?(response, expected) response.body.include? expected end # Convert XML or JSON response into a Hash + # @param [String] response Response as a String (either in XML or JSON) + # @return [Hash] def extract_hash(response) raise "Empty Body. Can't assert on it" if response.body.empty? type = case response.body[0] when '<' :xml @@ -105,10 +108,10 @@ case type when :json JSON.parse(response.body).transform_keys_to_symbols when :xml - parser = Nori.new(:convert_tags_to => lambda { |tag| tag.snakecase.to_sym }) + 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 \ No newline at end of file