Skip to content

Commit 2c47c4f

Browse files
committed
penguins: use POST
It seems that EDI disallows GET.
1 parent f6b6aa0 commit 2c47c4f

File tree

3 files changed

+48
-9
lines changed

3 files changed

+48
-9
lines changed

lib/datasets/dataset.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ def cache_path
3333
@cache_path ||= CachePath.new(@metadata.id)
3434
end
3535

36-
def download(output_path, url, *fallback_urls, &block)
37-
downloader = Downloader.new(url, *fallback_urls)
36+
def download(output_path, url, *fallback_urls, **options, &block)
37+
downloader = Downloader.new(url, *fallback_urls, **options)
3838
downloader.download(output_path, &block)
3939
end
4040

lib/datasets/downloader.rb

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@ module Datasets
1212
class Downloader
1313
class TooManyRedirects < Error; end
1414

15-
def initialize(url, *fallback_urls)
15+
def initialize(url, *fallback_urls, http_method: nil, http_parameters: nil)
1616
@url = normalize_url(url)
1717
@fallback_urls = fallback_urls.collect { |fallback_url| normalize_url(fallback_url) }
18+
@http_method = http_method
19+
@http_parameters = http_parameters
1820
end
1921

2022
def download(output_path, &block)
@@ -151,7 +153,21 @@ def download(output_path, &block)
151153
http.start do
152154
path = url.path
153155
path += "?#{url.query}" if url.query
154-
request = Net::HTTP::Get.new(path, headers)
156+
if @http_method == :post
157+
# TODO: We may want to add @http_content_type, @http_body
158+
# and so on.
159+
if @http_parameters
160+
body = URI.encode_www_form(@http_parameters)
161+
content_type = "application/x-www-form-urlencoded"
162+
headers = {"Content-Type" => content_type}.merge(headers)
163+
else
164+
body = ""
165+
end
166+
request = Net::HTTP::Post.new(path, headers)
167+
request.body = body
168+
else
169+
request = Net::HTTP::Get.new(path, headers)
170+
end
155171
http.request(request) do |response|
156172
case response
157173
when Net::HTTPSuccess, Net::HTTPPartialContent

lib/datasets/penguins.rb

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ def initialize
2626
super
2727
species = self.class.name.split("::").last.downcase
2828
@metadata.id = "palmerpenguins-#{species}"
29-
@metadata.url = self.class::URL
29+
package_id = http_parameters["packageid"]
30+
@metadata.url = "https://portal.edirepository.org/nis/mapbrowse" +
31+
"?packageid=#{package_id}"
3032
@metadata.licenses = ["CC0-1.0"]
3133
@data_path = cache_dir_path + "#{species}.csv"
3234
end
@@ -46,7 +48,10 @@ def each
4648
end
4749

4850
private def open_data
49-
download(data_path, metadata.url)
51+
download(data_path,
52+
"https://portal.edirepository.org/nis/dataviewer",
53+
http_method: :post,
54+
http_parameters: http_parameters)
5055
CSV.open(data_path, headers: :first_row, converters: :all) do |csv|
5156
yield csv
5257
end
@@ -56,19 +61,37 @@ def each
5661
# Adelie penguin data from: https://doi.org/10.6073/pasta/abc50eed9138b75f54eaada0841b9b86
5762
class Adelie < SpeciesBase
5863
DOI = "doi.org/10.6073/pasta/abc50eed9138b75f54eaada0841b9b86".freeze
59-
URL = "https://portal.edirepository.org/nis/dataviewer?packageid=knb-lter-pal.219.3&entityid=002f3893385f710df69eeebe893144ff".freeze
64+
65+
private def http_parameters
66+
{
67+
"packageid" => "knb-lter-pal.219.3",
68+
"entityid" => "002f3893385f710df69eeebe893144ff",
69+
}
70+
end
6071
end
6172

6273
# Chinstrap penguin data from: https://doi.org/10.6073/pasta/409c808f8fc9899d02401bdb04580af7
6374
class Chinstrap < SpeciesBase
6475
DOI = "doi.org/10.6073/pasta/409c808f8fc9899d02401bdb04580af7".freeze
65-
URL = "https://portal.edirepository.org/nis/dataviewer?packageid=knb-lter-pal.221.2&entityid=fe853aa8f7a59aa84cdd3197619ef462".freeze
76+
77+
private def http_parameters
78+
{
79+
"packageid" => "knb-lter-pal.221.2",
80+
"entityid" => "fe853aa8f7a59aa84cdd3197619ef462",
81+
}
82+
end
6683
end
6784

6885
# Gentoo penguin data from: https://doi.org/10.6073/pasta/2b1cff60f81640f182433d23e68541ce
6986
class Gentoo < SpeciesBase
7087
DOI = "doi.org/10.6073/pasta/2b1cff60f81640f182433d23e68541ce".freeze
71-
URL = "https://portal.edirepository.org/nis/dataviewer?packageid=knb-lter-pal.220.3&entityid=e03b43c924f226486f2f0ab6709d2381".freeze
88+
89+
private def http_parameters
90+
{
91+
"packageid" => "knb-lter-pal.220.3",
92+
"entityid" => "e03b43c924f226486f2f0ab6709d2381",
93+
}
94+
end
7295
end
7396
end
7497

0 commit comments

Comments
 (0)