From b73e6fade09beea653ec83452ed99dd0de2f55cb Mon Sep 17 00:00:00 2001 From: Pavel R Date: Thu, 18 Apr 2024 12:30:03 +0200 Subject: [PATCH] Add .rubocop config and fix ig_download script --- .rubocop.yml | 42 ++++++++++++ lib/au_core_test_kit/generator/ig_download.rb | 68 +++++++++++-------- 2 files changed, 80 insertions(+), 30 deletions(-) create mode 100644 .rubocop.yml diff --git a/.rubocop.yml b/.rubocop.yml new file mode 100644 index 00000000..5e22c513 --- /dev/null +++ b/.rubocop.yml @@ -0,0 +1,42 @@ +AllCops: + Exclude: + - 'worker.rb' + - 'spec/au_core/search_test_spec.rb' + - 'lib/au_core_test_kit/search_test.rb' + - 'lib/au_core_test_kit/search_test_properties.rb' + - 'spec/spec_helper.rb' + - 'spec/au_core/provenance_validator_spec.rb' + - 'spec/au_core/must_support_test_spec.rb' + - 'spec/au_core/generator/must_support_metadata_extractor_spec.rb' + - 'spec/au_core/date_search_validation_test_spec.rb' + - 'lib/au_core_test_kit/validation_test.rb' + - 'lib/au_core_test_kit/request_logger.rb' + - 'lib/au_core_test_kit/reference_resolution_test.rb' + - 'lib/au_core_test_kit/read_test.rb' + - 'lib/au_core_test_kit/provenance_validator.rb' + - 'lib/au_core_test_kit/must_support_test.rb' + - 'lib/au_core_test_kit/helpers.rb' + - 'lib/au_core_test_kit/generator/value_extractor.rb' + - 'lib/au_core_test_kit/generator/validation_test_generator.rb' + - 'lib/au_core_test_kit/generator/terminology_binding_metadata_extractor.rb' + - 'lib/au_core_test_kit/generator/summary_generator.rb' + - 'lib/au_core_test_kit/generated/**/*' + - 'lib/au_core_test_kit/generator/search_test_generator.rb' + - 'lib/au_core_test_kit/generator/search_metadata_extractor.rb' + - 'lib/au_core_test_kit/generator/search_definition_metadata_extractor.rb' + - 'lib/au_core_test_kit/generator/must_support_test_generator.rb' + - 'lib/au_core_test_kit/custom_groups/**/*' + - 'lib/au_core_test_kit/date_search_validation.rb' + - 'lib/au_core_test_kit/fhir_resource_navigation.rb' + - 'lib/au_core_test_kit/generator/provenance_revinclude_search_test_generator.rb' + - 'lib/au_core_test_kit/generator/must_support_metadata_extractor_au_core_3.rb' + - 'lib/au_core_test_kit/generator/must_support_metadata_extractor.rb' + - 'lib/au_core_test_kit/generator/ig_resources.rb' + - 'lib/au_core_test_kit/generator/ig_metadata_extractor.rb' + - 'lib/au_core_test_kit/generator/ig_loader.rb' + - 'lib/au_core_test_kit/generator/group_metadata_extractor.rb' + - 'lib/au_core_test_kit/generator/group_generator.rb' + - 'lib/au_core_test_kit/generator/group_metadata.rb' + - 'lib/au_core_test_kit/generator.rb' +Style/Documentation: + Enabled: false diff --git a/lib/au_core_test_kit/generator/ig_download.rb b/lib/au_core_test_kit/generator/ig_download.rb index 1bbf6884..f4237f0d 100644 --- a/lib/au_core_test_kit/generator/ig_download.rb +++ b/lib/au_core_test_kit/generator/ig_download.rb @@ -1,51 +1,59 @@ # frozen_string_literal: true require 'nokogiri' -require 'open-uri' -require 'uri' require 'json' require 'net/http' url = 'http://hl7.org.au/fhir/core/history.html' -html_content = URI.open(url).read +begin + uri = URI.parse(url) + response = Net::HTTP.get_response(uri) -doc = Nokogiri::HTML(html_content) + unless response.is_a?(Net::HTTPSuccess) + puts "Failed to fetch HTML content from #{url}. HTTP Status: #{response.code} #{response.message}" + exit + end + + html_content = response.body + + doc = Nokogiri::HTML(html_content) -script_tag = doc.at('script:contains("var pageJSON")') + script_tag = doc.at('script:contains("var pageJSON")') -ig_versions_to_skip = ['0.1.0-draft', '0.2.0-preview', '0.2.1-preview', '0.2.2-preview'] + ig_versions_to_skip = ['0.1.0-draft', '0.2.0-preview', '0.2.1-preview', '0.2.2-preview'] -if script_tag&.content - json_data = script_tag.content[/\{.*\}/m] - if json_data - data_hash = JSON.parse(json_data) - data_hash['list'].each do |item| - version = item['version'] - next if ig_versions_to_skip.include? version + if script_tag&.content + json_data = script_tag.content[/\{.*\}/m] + if json_data + data_hash = JSON.parse(json_data) + data_hash['list'].each do |item| + version = item['version'] + next if ig_versions_to_skip.include? version - package_url = "#{item['path']}/package.tgz" - file_name = "#{version}.tgz" - output_directory = 'lib/au_core_test_kit/igs' + package_url = "#{item['path']}/package.tgz" + file_name = "#{version}.tgz" + output_directory = 'lib/au_core_test_kit/igs' - Dir.mkdir(output_directory) unless File.directory?(output_directory) + Dir.mkdir(output_directory) unless File.directory?(output_directory) - File.open(File.join(output_directory, file_name), 'wb') do |file| - uri = URI.parse(package_url) - response = Net::HTTP.get_response(uri) - if response.is_a?(Net::HTTPSuccess) - file.write(response.body) - puts "Successfully downloaded #{file_name}" - else - puts "Failed to download #{file_name}. HTTP Status: #{response.code} #{response.message}" + File.open(File.join(output_directory, file_name), 'wb') do |file| + uri = URI.parse(package_url) + response = Net::HTTP.get_response(uri) + if response.is_a?(Net::HTTPSuccess) + file.write(response.body) + puts "Successfully downloaded #{file_name}" + else + puts "Failed to download #{file_name}. HTTP Status: #{response.code} #{response.message}" + end end - rescue StandardError => e - puts "An error occurred while downloading #{file_name}: #{e.message}" end + else + puts 'Error: Unable to extract JSON data from script tag.' end else - puts 'Error: Unable to extract JSON data from script tag.' + puts 'Error: Script tag containing pageJSON variable not found.' end -else - puts 'Error: Script tag containing pageJSON variable not found.' +rescue StandardError => e + puts "An error occurred: #{e.message}" end