require 'amsi/request_section/auth' require 'amsi/utils/request_fetcher' require 'amsi/utils/request_generator' module Amsi module Request # Base class for actions that fetch and parse specific AMSI SOAP # actions. # # When subclassing this base class, you may override the following methods # to extend functionality: # # * after_initialize(params) - a hook into the initializer to give # subclasses access to the parameters passed in. If the subclass uses # any parameters that the base class is agnostic to, this is the place # the set these values to instance variables # * sections (returns Array) - add additional request # sections to the request XML document. The 'auth' section will always # be inserted into the XML document by this class, so there is no need # to add it to the response of this method. # # Additionally, this base class provides the #soap_action method, which # returns the CamelCased name of the SOAP action being requested. This # method assumes that the subclass will be named the same as the action. class Base # Required initialization parameters: # @param user_id [String] AMSI account username # @param password [String] AMSI account password # @param portfolio_name [String] unique identifier for property in AMSI # @param web_service_url [String] AMSI Url to the leasing.asmx resource def initialize( user_id:, password:, portfolio_name:, web_service_url:, conn: Faraday.new, **other_args ) @user_id = user_id @password = password @portfolio_name = portfolio_name @web_service_url = web_service_url @conn = conn after_initialize(other_args) end # @return [Amsi::Model|Array] the parsed response # from AMSI # @raise [Amsi::Error] an error when validating the response def perform parser.parse(xml) end private attr_reader :user_id, :password, :portfolio_name, :web_service_url, :conn # @return [String] the XMl response from Amsi def xml generator = Utils::RequestGenerator.new( sections: [auth_section, *sections], soap_action: soap_action, url: web_service_url ) Utils::RequestFetcher.new(generator: generator, conn: conn).fetch end # A hook into the initializer to give subclasses access to the parameters # passed in. If the subclass uses any parameters that the base class is # agnostic to, this is the place the set these values to instance # variables def after_initialize(params) # No-op, this method is a call back for subclasses end def parser # No-op, this method is a call back for subclasses end # A hook to add additonal request sections to the request XML document. # The 'auth' section will always be inserted into the XML document by this # class, so there is no need to add it to the response of this method. def sections [] end # The CamelCased name of the SOAP action def soap_action self.class.name.split('::').last end def auth_section RequestSection::Auth.new( user_id: user_id, password: password, portfolio_name: portfolio_name ) end end private_constant :Base end end