module SsciInc class Request include DSL ADDITIONAL_ITEMS = [:postback_url, :postback_username, :postback_password, :embed_credentials, :ordernotes, :interface, :monthly_income, :monthly_rent] COURT_TYPES = { bankruptcy: "Bankruptcy", federal: "Federal", felony: "Felony", felony_misdemeanor: "Felony Misdemeanor", lower_civil: "Lower Civil", property_search: "Property Search", upper_civil: "Upper Civil", upper_lower_civil: "Upper and Lower Civil" } # BackgroundCheck attr_accessor :config, :base_uri, :user_name, :password # BackgroundSearchPackage attr_accessor :action, :type, :reference_id, :reference # PersonalData attr_accessor :email_address, :telephone # PersonName attr_accessor :given_name, :middle_name, :family_name, :affix # DemographicDetail attr_accessor :government_id, :ssn_country_code, :issuing_authority, :date_of_birth # PostalAddress attr_accessor :country_code, :postal_code, :region, :municipality, :address_line, :street_name, :county, :district # Screenings attr_reader :screenings attr_accessor :use_defaults # AdditionalItems attr_accessor *ADDITIONAL_ITEMS attr_reader :aliases def initialize(h={}) init_defaults h.each do |k,v| send("#{k}=", v) if respond_to?("#{k}=") end yield self if block_given? end def additional_items ADDITIONAL_ITEMS.inject({}) do |h,a| v = send(a) h[a] = send(a) unless v.nil? h end end def ssn=(new_ssn) @government_id = new_ssn @ssn_country_code = "US" @issuing_authority = "SSN" end def name person_name do |p| p.given_name = given_name p.middle_name = middle_name p.family_name = family_name p.affix = affix end end def as_xml Nokogiri::XML::Builder.new do |xml| xml.BackgroundCheck(:userId => user_name, :password => password) { xml.BatchData { xml.DestSiteIdentifier "SOUTHEASTERN" xml.SourceSiteIdentifier site_identifier xml.AcctCode account_code xml.BatchID 1 xml.RecordCount 1 } xml.BackgroundSearchPackage { xml.ReferenceId reference_id xml.Reference reference xml.PersonalData { xml.EmailAddress email_address xml.Telephone telephone xml.parent << name.as_xml if aliases.any? xml.Aliases { aliases.each do |a| xml.parent << a.as_xml end } end xml.DemographicDetail { xml.Ssn government_id xml.DateOfBirth date_of_birth } # DemographicDetail xml.AddressSummary { xml.CountryCode country_code == "Canada" ? "CA" : "US" xml.PostalCode postal_code xml.Region region xml.Municipality municipality xml.AddressLine street_name } # AddressSummary } # PersonalData xml.Screenings { init_default_screenings if use_defaults @screenings.each do |s| xml.parent << s.as_xml end additional_items.each do |k,v| xml.AdditionalItems(:type => "x:#{k}") { xml.Text type.includes? "url" ? xml.CDATA v : v } end } # Screenings } # BackgroundSearchPackage } # BackgroundCheck end end def to_xml as_xml.to_xml end def request_options { body: to_xml, basic_auth: { username: user_name, password: password }, headers: { "Content-Type" => "text/xml"} } end def build_request HTTParty::Request.new(Net::HTTP::Post, base_uri, request_options) end def submit response = build_request.perform data = response.parsed_response SsciInc::Response.new(data) end def config @config || SsciInc.configuration end def base_uri @base_uri || config.base_uri end def site_identifier @site_identifier || config.site_identifier end def account_code @account_code || config.account_code end def user_name @user_name || config.user_name end def password @password || config.password end def postback_url @postback_url || config.postback_url end def postback_username @postback_username || config.postback_username end def postback_password @postback_password || config.postback_password end private def init_defaults @action = "submit" @screenings = [] @aliases = [] end def init_default_screenings @screenings = [Screening::Credit.new, Screening::Criminal.new(qualifier: "county", region: region, county: county, court_type: COURT_TYPES[:upper_lower_civil]), Screening::Criminal.new(qualifier: "county", region: region, county: county, court_type: COURT_TYPES[:felony_misdemeanor]), Screening::Criminal.new(qualifier: "county", region: region, county: "MIDDLE DISTRICT", court_type: COURT_TYPES[:federal])] end end end