# frozen_string_literal: true require 'nokogiri' require 'date' require 'active_support/core_ext/hash/conversions' require 'active_support/inflector/inflections' require_relative './rixml_document' # rubocop:disable Metrics/ClassLength class RIXML class << self def parse_from_file(filename) RIXML.new(File.read(filename)) end end def initialize(data) @data = data @document = Nokogiri::XML(data) @attrs = Hash.from_xml(@document.root.to_s) @document.remove_namespaces! end def research @rixml_document ||= RixmlDocument.parse(@data) end def product_id @attrs.dig('Research', 'Product', 'productID') end def research_id @attrs.dig('Research', 'researchID') end # @deprecated Please use {#status_info} instead def status current_status_info&.dig('statusType')&.downcase&.to_sym || :published end def status_info time_str = current_status_info&.dig('statusDateTime') || DateTime.now.to_s status = current_status_info&.dig('statusType')&.downcase&.to_sym || :published { status_type: status, status_date_time: Time.parse(time_str).to_datetime.to_s } end # @deprecated Please use {#status_info} instead def publication_date time_str = current_status_info&.dig('statusDateTime') || DateTime.now.to_s Time.parse(time_str).to_datetime.to_s end def authors org = @attrs.dig('Research', 'Product', 'Source', 'Organization') || {} if org.is_a? Array org = org.find { |v| v['primaryIndicator'] == 'Yes' } || org.first end authors = org.dig('PersonGroup', 'PersonGroupMember') || [] authors = [authors] unless authors.is_a? Array authors.map { |author| parse_info_from_author(author) } end def report_info content = @attrs.dig('Research', 'Product', 'Content') { title: content['Title'], subtitle: content['SubTitle'], abstract: content['Abstract'], synopsis: content['Synopsis'], file_name: content['Resource']&.dig('Name'), pages: resource_length(content['Resource']) } end def asset_classes assets = @attrs.dig('Research', 'Product', 'Source', 'Organization', 'Expertise', 'AssetClasses', 'AssetClass') assets = [assets] unless assets.is_a?(Array) assets.collect do |asset| asset&.dig('assetClass') end.compact end def context context = @attrs.dig('Research', 'Product', 'Context') || {} { companies: parse_companies_from_context(context), sectors: parse_sectors_from_context(context), countries: parse_countries_from_context(context), category: parse_product_category_from_context(context), publication_date: parse_publication_date_from_context(context) } end def xpath(path) @document.xpath(path) end private def current_status_info info = @attrs.dig('Research', 'Product', 'StatusInfo') info = [info] unless info.is_a? Array info.find { |status| status&.dig('currentStatusIndicator')&.downcase == 'yes' } || info.first end def resource_length(resource) length = resource&.dig('Length') return length.to_i if length.respond_to?(:to_i) end # rubocop:disable Metrics/MethodLength def parse_info_from_author(author) person = author&.dig('Person') { primary: primary?(author.dig('primaryIndicator')), name: person['DisplayName'], first_name: person['GivenName'], middle_name: person['MiddleName'], last_name: person['FamilyName'], job_title: person['JobTitle'], email: person['ContactInfo']&.dig('Email')&.downcase, # TODO: Remove: Use email from contacts contacts: parse_author_contacts(person['ContactInfo']) } end # rubocop:enable Metrics/MethodLength def primary?(primary_indicator) primary_indicator == 'Yes' end def parse_author_contacts(contacts) contacts = [contacts] unless contacts.is_a? Array contacts.map do |contact| { email: contact&.dig('Email')&.downcase, phone: parse_author_contact_phone(contact&.dig('Phone')) } end end def parse_author_contact_phone(phone) [phone['CountryCode'], phone['Number']].join(' ') if phone end def parse_sectors_from_context(context) list = context['ProductClassifications'].try(:[], 'SectorIndustry') return [] if list.nil? list = [list] unless list.is_a? Array list.select { |s| s['classificationType'] == 'GICS' }.map do |v| { code: v['code'].to_i, focus: v['focusLevel'].try(:downcase) == 'yes' } end end def parse_countries_from_context(context) [context['ProductClassifications']&.dig('Country')].flatten.compact.map do |country| { code: country['code'].upcase } end end def parse_companies_from_context(context) companies = [] [context['IssuerDetails']].flatten.compact.each do |issuer| [issuer['Issuer']].flatten.compact.select { |c| c['issuerType'] == 'Corporate' }.each do |company| companies << parse_company_info(company) end end companies.flatten end def parse_product_category_from_context(context) product_category = context.dig('ProductDetails', 'ProductCategory') return product_category['productCategory'] if product_category.is_a?(Hash) product_category end def parse_publication_date_from_context(context) context.dig('ProductDetails', 'publicationDateTime') end def parse_company_info(company) securities = company.dig('SecurityDetails', 'Security') securities = [securities].compact unless securities.is_a? Array securities.map do |security| info = { name: (company.dig('IssuerName') || {})['NameValue'], primary: company['primaryIndicator'] == 'Yes' } info = info.merge(parse_security_info(security)) info.merge(securities: parse_securities(security)) # Keep retrocompatibility adding a new key 'securities' end end def parse_security_info(security) security_ids = security.dig('SecurityID') security_ids = [security_ids].compact unless security_ids.is_a? Array security_ids&.map do |security_id| { security_id['idType'].underscore.to_sym => security_id['idValue'] } end&.reduce({}, :merge) || [] end def parse_securities(security) security_ids = security.dig('SecurityID') security_ids = [security_ids].compact unless security_ids.is_a? Array security_ids&.map do |security_id| { primary: primary?(security.dig('primaryIndicator')), security_id['idType'].underscore.to_sym => security_id['idValue'] } end end end