require 'real_page/utils/array_fetcher' require 'real_page/validator/prospects_data' require_relative 'base' module RealPage module DocumentParser # Parse the ProspectSearch response class GuestCards < Base MULTIPLE_CHILDREN = { 'Activities' => { model: Model::Activity, key: 'Activity' }, 'FollowUps' => { model: Model::FollowUp, key: 'FollowUp' }, 'LeaseActions' => { model: Model::LeaseAction, key: 'LeaseAction' }, 'Quotes' => { model: Model::Quote, key: 'Quote' }, 'Screenings' => { model: Model::Screening, key: 'Screening' }, 'UnitsShown' => { model: Model::UnitShown, key: 'UnitShown' } } SINGLE_CHILDREN = %w[Appointment Preferences] private_constant :MULTIPLE_CHILDREN, :SINGLE_CHILDREN private # @param body [Hash] the body of the XML response parsed # into a Hash # @return [Array] the guest_cards contained # in the response # @raise [RealPage::Error::Base] if the response is invalid def parse_body(body) guest_cards(body).map do |guest_card| attrs = guest_card.merge(children(guest_card)) attrs.delete('Amentities') # Remove the misspelled version Model::GuestCard.new(attrs) end end # @return [Hash>] an array of all GuestCard # attributes def guest_cards(body) response = body['prospectsearchResponse'] result = response['prospectsearchResult'] prospect_search = result['ProspectSearch'] if result return [] unless prospect_search guest_cards = prospect_search['GuestCards'] Utils::ArrayFetcher.new(hash: guest_cards, key: 'GuestCard').fetch end # @return [Hash] the guest_card's children # that can have multiple entries def multiple_children(guest_card) Hash[ MULTIPLE_CHILDREN.map do |key, definition| fetcher = Utils::ArrayFetcher.new(definition.merge(hash: guest_card[key])) [key, fetcher.fetch] end ] end # @return [Hash