Skip to content

Commit

Permalink
Merge branch 'release-4.8.18'
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiomarrocoli committed Jul 19, 2022
2 parents 683a540 + e307b79 commit 720e63a
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 12 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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`
Expand Down
2 changes: 1 addition & 1 deletion lib/modules/wdpa/release.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand Down
21 changes: 15 additions & 6 deletions lib/modules/wdpa/s3.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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"

Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/unit/wdpa/release_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
8 changes: 4 additions & 4 deletions test/unit/wdpa/s3_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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()
Expand All @@ -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
Expand Down

0 comments on commit 720e63a

Please sign in to comment.