require 'yardi/request_section/authentication' require 'yardi/utils/request_fetcher' require 'yardi/utils/request_generator' module Yardi module Request # Base class for actions that fetch and parse specific Yardi 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 necessary authentication # data 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 attr_reader :connection, :credential, :response # @param credential [Parameter::Credential] contains the PMC-specific # configuration information needed to make a request. We want to fail # noisily if it's missing, so it is separate from the main params hash. # @param params [Hash] the parameters needed to build the # XML request. def initialize(credential:, params:) @credential = credential @connection = params[:connection] || Faraday.new after_initialize(params) end # @return [Yardi::Model|Array] the parsed response # from Yardi # @raise [Yardi::Error] if an error was encountered when validating # the response def perform @response = xml parser.parse(@response) end # @return [String] the XMl response from Yardi def xml if request_test_data? fetch_test_data else Utils::RequestFetcher.new( connection: connection, endpoint: credential.web_service_url, generator: generator ).fetch end end # This makes it easy for us to see what XML we're about to send when # debugging requests def generator Utils::RequestGenerator.new(soap_action, sections, interface) end private # 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 # A hook to 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. def sections { soap_body: soap_body_sections, xml_doc: xml_doc_sections } end def soap_body_sections [] end def xml_doc_sections [] end # Each request must specify its associated SOAP action. def soap_action raise NotImplementedError end # Each request must specify its associated interface. def interface raise NotImplementedError end def request_test_data? params[:test_data] == true end def request_name self.class.name.split('::').last end def fetch_test_data case request_name when 'GetResidents' Utils::TestDataFetcher.residents(params[:property_id]) when 'GetYardiGuestActivity' Utils::TestDataFetcher.guest_card_activity(params[:property_id], params[:prospect]) end end end private_constant :Base end end