lib/rixml.rb in rixml-0.0.5 vs lib/rixml.rb in rixml-0.0.6

- old
+ new

@@ -1,11 +1,11 @@ +# frozen_string_literal: true require 'nokogiri' require 'date' require 'active_support/core_ext/hash/conversions' class RIXML - def initialize(document) @attrs = Hash.from_xml(document.to_s) end def product_id @@ -19,10 +19,12 @@ def publication_date time_str = @attrs.dig('Research', 'Product', 'StatusInfo', 'statusDateTime') || DateTime.now.to_s DateTime.strptime(time_str) end + # rubocop:disable Metrics/AbcSize + # rubocop:disable Metrics/MethodLength def authors org = @attrs.dig('Research', 'Product', 'Source', 'Organization') || {} if org.is_a? Array org = org.find { |v| v['primaryIndicator'] == 'Yes' } || org.first end @@ -39,50 +41,50 @@ job_title: person['JobTitle'], email: person['ContactInfo']&.dig('Email')&.downcase } end end + # rubocop:enable Metrics/AbcSize + # rubocop:enable Metrics/MethodLength def report_info content = @attrs.dig('Research', 'Product', 'Content') { title: content['Title'], abstract: content['Abstract'], + synopsis: content['Synopsis'], file_name: content['Resource']&.dig('Name'), pages: content['Resource']&.dig('Length').to_i } end def context context = @attrs.dig('Research', 'Product', 'Context') - context_info = { - companies: RIXML.extract_companies_from_context(context), - sectors: RIXML.extract_sectors_from_context(context) - } + { companies: RIXML.extract_companies_from_context(context), sectors: RIXML.extract_sectors_from_context(context) } end - def self.parse data + def self.parse(data) RIXML.new(Nokogiri::XML(data).root) end - def self.parse_from_file filename - self.parse self.read_file(filename) + def self.parse_from_file(filename) + parse read_file(filename) end - private - - def self.read_file filename - body = '' + def self.read_file(filename) + body = ''.dup File.open(filename, 'r') do |infile| while (line = infile.gets) body << line end end body end - def self.extract_companies_from_context context + # rubocop:disable Metrics/AbcSize + # rubocop:disable Metrics/MethodLength + def self.extract_companies_from_context(context) companies = [] list = context['IssuerDetails'].try(:[], 'Issuer') return [] if list.nil? list = [list] unless list.is_a? Array list.select { |c| c['issuerType'] == 'Corporate' }.each do |company| @@ -91,12 +93,13 @@ isin = securities.find { |security| security['idType'] == 'ISIN' } companies << { isin: isin['idValue'] } unless isin&.dig('idValue').nil? end companies end + # rubocop:enable Metrics/AbcSize + # rubocop:enable Metrics/MethodLength - def self.extract_sectors_from_context context - sectors = [] + def self.extract_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' }