diff --git a/CHANGELOG.md b/CHANGELOG.md index 0206e333c..47149c1f7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +### 4.8.18 +- bug fix: use constants to choose bucket for user downloads, use latest bucket for releases + ### 4.8.17 - Chore: July 2022 WDPA Release - July statistics CSVs added: `country_statistics`, `global_statistics`, `pame_country`, `pame_data` diff --git a/lib/modules/wdpa/release.rb b/lib/modules/wdpa/release.rb index ce953593e..19244c7ae 100644 --- a/lib/modules/wdpa/release.rb +++ b/lib/modules/wdpa/release.rb @@ -14,7 +14,7 @@ def initialize end def download - Wdpa::S3.download_current_wdpa_to zip_path + Wdpa::S3.download_latest_wdpa_to zip_path system("unzip -j '#{zip_path}' '\*.gdb/\*' -d '#{gdb_path}'") import_tables.each do |original_table, std_table| diff --git a/lib/modules/wdpa/s3.rb b/lib/modules/wdpa/s3.rb index 02ceede7b..e41b5126c 100644 --- a/lib/modules/wdpa/s3.rb +++ b/lib/modules/wdpa/s3.rb @@ -7,8 +7,8 @@ def initialize }) end - def self.download_current_wdpa_to filename - self.new.tap{ |s3| s3.download_current_wdpa_to filename } + def self.download_latest_wdpa_to filename + self.new.tap{ |s3| s3.download_latest_wdpa_to filename } end def self.new_wdpa? since @@ -19,12 +19,12 @@ def self.current_wdpa_identifier new.current_wdpa_identifier end - def download_current_wdpa_to filename - current_wdpa.get(response_target: filename, response_content_encoding: 'ASCII_8BIT') + def download_latest_wdpa_to filename + latest_wdpa.get(response_target: filename, response_content_encoding: 'ASCII_8BIT') end def new_wdpa? since - current_wdpa.last_modified > since + latest_wdpa.last_modified > since end def current_wdpa_identifier @@ -34,7 +34,7 @@ def current_wdpa_identifier private - def current_wdpa + def latest_wdpa latest = available_wdpa_databases.max_by do |object| filename = object.key # e.g. "WDPA_WDOECM_Sep2020_Public.gdb.zip" @@ -46,6 +46,15 @@ def current_wdpa latest.object end + def current_wdpa + wdpa_from_constants = available_wdpa_databases.find do |object| + object.key.include?("#{WDPA_UPDATE_MONTH.first(3)}#{WDPA_UPDATE_YEAR}") + end + wdpa_from_constants ? wdpa_from_constants.object : latest_wdpa + rescue + latest_wdpa + end + def available_wdpa_databases bucket_name = Rails.application.secrets.aws_bucket @s3.bucket(bucket_name).objects diff --git a/test/unit/wdpa/release_test.rb b/test/unit/wdpa/release_test.rb index 6959fdc0b..08daf701b 100644 --- a/test/unit/wdpa/release_test.rb +++ b/test/unit/wdpa/release_test.rb @@ -8,7 +8,7 @@ class TestWdpaRelease < ActiveSupport::TestCase gdb_path = "gdb_path" Wdpa::Release.any_instance.expects(:gdb_path).returns(gdb_path).at_least_once - Wdpa::S3.expects(:download_current_wdpa_to).with(zip_path) + Wdpa::S3.expects(:download_latest_wdpa_to).with(zip_path) Wdpa::Release.any_instance. expects(:system). diff --git a/test/unit/wdpa/s3_test.rb b/test/unit/wdpa/s3_test.rb index 4e7b8c7f7..252a34c57 100644 --- a/test/unit/wdpa/s3_test.rb +++ b/test/unit/wdpa/s3_test.rb @@ -22,7 +22,7 @@ def setup Wdpa::S3.new() end - test '.download_current_wdpa_to handles encoding correctly' do + test '.download_latest_wdpa_to handles encoding correctly' do filename = File.join(Rails.root, 'tmp', 'hey_this_is_a_filename.zip') file_contents = "\x9B".force_encoding(Encoding::ASCII_8BIT) @@ -34,10 +34,10 @@ def setup @bucket_mock.stubs(:objects).returns([latest_file_mock]) Aws::S3::Resource.stubs(:new).returns(@s3_mock) - Wdpa::S3.download_current_wdpa_to filename + Wdpa::S3.download_latest_wdpa_to filename end - test '.download_current_wdpa_to retrieves the latest WDPA from S3, and saves it to the + test '.download_latest_wdpa_to retrieves the latest WDPA from S3, and saves it to the given filename' do filename = 'hey_this_is_a_filename.zip' latest_file_mock = mock() @@ -56,7 +56,7 @@ def setup Aws::S3::Resource.stubs(:new).returns(@s3_mock) - Wdpa::S3.download_current_wdpa_to filename + Wdpa::S3.download_latest_wdpa_to filename end test '.new_wdpa? compares the current wdpa last modified time with the given